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. console.error(' - dnssec dnssec-keygen format');
  80. process.exit(1);
  81. }
  82. /*
  83. * Key derivation can only be done on private keys, so use of the -d
  84. * option necessarily implies -p.
  85. */
  86. if (opts.derive)
  87. opts.private = true;
  88. var inFile = process.stdin;
  89. var inFileName = 'stdin';
  90. var inFilePath;
  91. if (opts.file) {
  92. inFilePath = opts.file;
  93. } else if (opts._args.length === 1) {
  94. inFilePath = opts._args[0];
  95. }
  96. if (inFilePath)
  97. inFileName = path.basename(inFilePath);
  98. try {
  99. if (inFilePath) {
  100. fs.accessSync(inFilePath, fs.R_OK);
  101. inFile = fs.createReadStream(inFilePath);
  102. }
  103. } catch (e) {
  104. console.error('sshpk-conv: error opening input file' +
  105. ': ' + e.name + ': ' + e.message);
  106. process.exit(1);
  107. }
  108. var outFile = process.stdout;
  109. try {
  110. if (opts.out && !opts.identify) {
  111. fs.accessSync(path.dirname(opts.out), fs.W_OK);
  112. outFile = fs.createWriteStream(opts.out);
  113. }
  114. } catch (e) {
  115. console.error('sshpk-conv: error opening output file' +
  116. ': ' + e.name + ': ' + e.message);
  117. process.exit(1);
  118. }
  119. var bufs = [];
  120. inFile.on('readable', function () {
  121. var data;
  122. while ((data = inFile.read()))
  123. bufs.push(data);
  124. });
  125. var parseOpts = {};
  126. parseOpts.filename = inFileName;
  127. inFile.on('end', function processKey() {
  128. var buf = Buffer.concat(bufs);
  129. var fmt = 'auto';
  130. if (opts.informat)
  131. fmt = opts.informat;
  132. var f = sshpk.parseKey;
  133. if (opts.private)
  134. f = sshpk.parsePrivateKey;
  135. try {
  136. var key = f(buf, fmt, parseOpts);
  137. } catch (e) {
  138. if (e.name === 'KeyEncryptedError') {
  139. getPassword(function (err, pw) {
  140. if (err) {
  141. console.log('sshpk-conv: ' +
  142. err.name + ': ' +
  143. err.message);
  144. process.exit(1);
  145. }
  146. parseOpts.passphrase = pw;
  147. processKey();
  148. });
  149. return;
  150. }
  151. console.error('sshpk-conv: ' +
  152. e.name + ': ' + e.message);
  153. process.exit(1);
  154. }
  155. if (opts.derive)
  156. key = key.derive(opts.derive);
  157. if (opts.comment)
  158. key.comment = opts.comment;
  159. if (!opts.identify) {
  160. fmt = undefined;
  161. if (opts.outformat)
  162. fmt = opts.outformat;
  163. outFile.write(key.toBuffer(fmt));
  164. if (fmt === 'ssh' ||
  165. (!opts.private && fmt === undefined))
  166. outFile.write('\n');
  167. outFile.once('drain', function () {
  168. process.exit(0);
  169. });
  170. } else {
  171. var kind = 'public';
  172. if (sshpk.PrivateKey.isPrivateKey(key))
  173. kind = 'private';
  174. console.log('%s: a %d bit %s %s key', inFileName,
  175. key.size, key.type.toUpperCase(), kind);
  176. if (key.type === 'ecdsa')
  177. console.log('ECDSA curve: %s', key.curve);
  178. if (key.comment)
  179. console.log('Comment: %s', key.comment);
  180. console.log('Fingerprint:');
  181. console.log(' ' + key.fingerprint().toString());
  182. console.log(' ' + key.fingerprint('md5').toString());
  183. process.exit(0);
  184. }
  185. });
  186. }