sshpk-conv 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env node
  2. // -*- mode: js -*-
  3. // vim: set filetype=javascript :
  4. // Copyright 2015 Joyent, Inc. All rights reserved.
  5. var dashdash = require('dashdash');
  6. var sshpk = require('../lib/index');
  7. var fs = require('fs');
  8. var path = require('path');
  9. var tty = require('tty');
  10. var readline = require('readline');
  11. var getPassword = require('getpass').getPass;
  12. var options = [
  13. {
  14. names: ['outformat', 't'],
  15. type: 'string',
  16. help: 'Output format'
  17. },
  18. {
  19. names: ['informat', 'T'],
  20. type: 'string',
  21. help: 'Input format'
  22. },
  23. {
  24. names: ['file', 'f'],
  25. type: 'string',
  26. help: 'Input file name (default stdin)'
  27. },
  28. {
  29. names: ['out', 'o'],
  30. type: 'string',
  31. help: 'Output file name (default stdout)'
  32. },
  33. {
  34. names: ['private', 'p'],
  35. type: 'bool',
  36. help: 'Produce a private key as output'
  37. },
  38. {
  39. names: ['derive', 'd'],
  40. type: 'string',
  41. help: 'Output a new key derived from this one, with given algo'
  42. },
  43. {
  44. names: ['identify', 'i'],
  45. type: 'bool',
  46. help: 'Print key metadata instead of converting'
  47. },
  48. {
  49. names: ['comment', 'c'],
  50. type: 'string',
  51. help: 'Set key comment, if output format supports'
  52. },
  53. {
  54. names: ['help', 'h'],
  55. type: 'bool',
  56. help: 'Shows this help text'
  57. }
  58. ];
  59. if (require.main === module) {
  60. var parser = dashdash.createParser({
  61. options: options
  62. });
  63. try {
  64. var opts = parser.parse(process.argv);
  65. } catch (e) {
  66. console.error('sshpk-conv: error: %s', e.message);
  67. process.exit(1);
  68. }
  69. if (opts.help || opts._args.length > 1) {
  70. var help = parser.help({}).trimRight();
  71. console.error('sshpk-conv: converts between SSH key formats\n');
  72. console.error(help);
  73. console.error('\navailable formats:');
  74. console.error(' - pem, pkcs1 eg id_rsa');
  75. console.error(' - ssh eg id_rsa.pub');
  76. console.error(' - pkcs8 format you want for openssl');
  77. console.error(' - openssh like output of ssh-keygen -o');
  78. console.error(' - rfc4253 raw OpenSSH wire format');
  79. process.exit(1);
  80. }
  81. /*
  82. * Key derivation can only be done on private keys, so use of the -d
  83. * option necessarily implies -p.
  84. */
  85. if (opts.derive)
  86. opts.private = true;
  87. var inFile = process.stdin;
  88. var inFileName = 'stdin';
  89. var inFilePath;
  90. if (opts.file) {
  91. inFilePath = opts.file;
  92. } else if (opts._args.length === 1) {
  93. inFilePath = opts._args[0];
  94. }
  95. if (inFilePath)
  96. inFileName = path.basename(inFilePath);
  97. try {
  98. if (inFilePath) {
  99. fs.accessSync(inFilePath, fs.R_OK);
  100. inFile = fs.createReadStream(inFilePath);
  101. }
  102. } catch (e) {
  103. console.error('sshpk-conv: error opening input file' +
  104. ': ' + e.name + ': ' + e.message);
  105. process.exit(1);
  106. }
  107. var outFile = process.stdout;
  108. try {
  109. if (opts.out && !opts.identify) {
  110. fs.accessSync(path.dirname(opts.out), fs.W_OK);
  111. outFile = fs.createWriteStream(opts.out);
  112. }
  113. } catch (e) {
  114. console.error('sshpk-conv: error opening output file' +
  115. ': ' + e.name + ': ' + e.message);
  116. process.exit(1);
  117. }
  118. var bufs = [];
  119. inFile.on('readable', function () {
  120. var data;
  121. while ((data = inFile.read()))
  122. bufs.push(data);
  123. });
  124. var parseOpts = {};
  125. parseOpts.filename = inFileName;
  126. inFile.on('end', function processKey() {
  127. var buf = Buffer.concat(bufs);
  128. var fmt = 'auto';
  129. if (opts.informat)
  130. fmt = opts.informat;
  131. var f = sshpk.parseKey;
  132. if (opts.private)
  133. f = sshpk.parsePrivateKey;
  134. try {
  135. var key = f(buf, fmt, parseOpts);
  136. } catch (e) {
  137. if (e.name === 'KeyEncryptedError') {
  138. getPassword(function (err, pw) {
  139. if (err) {
  140. console.log('sshpk-conv: ' +
  141. err.name + ': ' +
  142. err.message);
  143. process.exit(1);
  144. }
  145. parseOpts.passphrase = pw;
  146. processKey();
  147. });
  148. return;
  149. }
  150. console.error('sshpk-conv: ' +
  151. e.name + ': ' + e.message);
  152. process.exit(1);
  153. }
  154. if (opts.derive)
  155. key = key.derive(opts.derive);
  156. if (opts.comment)
  157. key.comment = opts.comment;
  158. if (!opts.identify) {
  159. fmt = undefined;
  160. if (opts.outformat)
  161. fmt = opts.outformat;
  162. outFile.write(key.toBuffer(fmt));
  163. if (fmt === 'ssh' ||
  164. (!opts.private && fmt === undefined))
  165. outFile.write('\n');
  166. outFile.once('drain', function () {
  167. process.exit(0);
  168. });
  169. } else {
  170. var kind = 'public';
  171. if (sshpk.PrivateKey.isPrivateKey(key))
  172. kind = 'private';
  173. console.log('%s: a %d bit %s %s key', inFileName,
  174. key.size, key.type.toUpperCase(), kind);
  175. if (key.type === 'ecdsa')
  176. console.log('ECDSA curve: %s', key.curve);
  177. if (key.comment)
  178. console.log('Comment: %s', key.comment);
  179. console.log('Fingerprint:');
  180. console.log(' ' + key.fingerprint().toString());
  181. console.log(' ' + key.fingerprint('md5').toString());
  182. process.exit(0);
  183. }
  184. });
  185. }