Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

sshpk-conv 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env node
  2. // -*- mode: js -*-
  3. // vim: set filetype=javascript :
  4. // Copyright 2018 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: ['fingerprint', 'F'],
  50. type: 'bool',
  51. help: 'Output key fingerprint'
  52. },
  53. {
  54. names: ['hash', 'H'],
  55. type: 'string',
  56. help: 'Hash function to use for key fingeprint with -F'
  57. },
  58. {
  59. names: ['spki', 's'],
  60. type: 'bool',
  61. help: 'With -F, generates an SPKI fingerprint instead of SSH'
  62. },
  63. {
  64. names: ['comment', 'c'],
  65. type: 'string',
  66. help: 'Set key comment, if output format supports'
  67. },
  68. {
  69. names: ['help', 'h'],
  70. type: 'bool',
  71. help: 'Shows this help text'
  72. }
  73. ];
  74. if (require.main === module) {
  75. var parser = dashdash.createParser({
  76. options: options
  77. });
  78. try {
  79. var opts = parser.parse(process.argv);
  80. } catch (e) {
  81. console.error('sshpk-conv: error: %s', e.message);
  82. process.exit(1);
  83. }
  84. if (opts.help || opts._args.length > 1) {
  85. var help = parser.help({}).trimRight();
  86. console.error('sshpk-conv: converts between SSH key formats\n');
  87. console.error(help);
  88. console.error('\navailable key formats:');
  89. console.error(' - pem, pkcs1 eg id_rsa');
  90. console.error(' - ssh eg id_rsa.pub');
  91. console.error(' - pkcs8 format you want for openssl');
  92. console.error(' - openssh like output of ssh-keygen -o');
  93. console.error(' - rfc4253 raw OpenSSH wire format');
  94. console.error(' - dnssec dnssec-keygen format');
  95. console.error(' - putty PuTTY ppk format');
  96. console.error('\navailable fingerprint formats:');
  97. console.error(' - hex colon-separated hex for SSH');
  98. console.error(' straight hex for SPKI');
  99. console.error(' - base64 SHA256:* format from OpenSSH');
  100. process.exit(1);
  101. }
  102. /*
  103. * Key derivation can only be done on private keys, so use of the -d
  104. * option necessarily implies -p.
  105. */
  106. if (opts.derive)
  107. opts.private = true;
  108. var inFile = process.stdin;
  109. var inFileName = 'stdin';
  110. var inFilePath;
  111. if (opts.file) {
  112. inFilePath = opts.file;
  113. } else if (opts._args.length === 1) {
  114. inFilePath = opts._args[0];
  115. }
  116. if (inFilePath)
  117. inFileName = path.basename(inFilePath);
  118. try {
  119. if (inFilePath) {
  120. fs.accessSync(inFilePath, fs.R_OK);
  121. inFile = fs.createReadStream(inFilePath);
  122. }
  123. } catch (e) {
  124. ifError(e, 'error opening input file');
  125. }
  126. var outFile = process.stdout;
  127. try {
  128. if (opts.out && !opts.identify) {
  129. fs.accessSync(path.dirname(opts.out), fs.W_OK);
  130. outFile = fs.createWriteStream(opts.out);
  131. }
  132. } catch (e) {
  133. ifError(e, 'error opening output file');
  134. }
  135. var bufs = [];
  136. inFile.on('readable', function () {
  137. var data;
  138. while ((data = inFile.read()))
  139. bufs.push(data);
  140. });
  141. var parseOpts = {};
  142. parseOpts.filename = inFileName;
  143. inFile.on('end', function processKey() {
  144. var buf = Buffer.concat(bufs);
  145. var fmt = 'auto';
  146. if (opts.informat)
  147. fmt = opts.informat;
  148. var f = sshpk.parseKey;
  149. if (opts.private)
  150. f = sshpk.parsePrivateKey;
  151. try {
  152. var key = f(buf, fmt, parseOpts);
  153. } catch (e) {
  154. if (e.name === 'KeyEncryptedError') {
  155. getPassword(function (err, pw) {
  156. if (err)
  157. ifError(err);
  158. parseOpts.passphrase = pw;
  159. processKey();
  160. });
  161. return;
  162. }
  163. ifError(e);
  164. }
  165. if (opts.derive)
  166. key = key.derive(opts.derive);
  167. if (opts.comment)
  168. key.comment = opts.comment;
  169. if (opts.identify) {
  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('SHA256 fingerprint: ' +
  180. key.fingerprint('sha256').toString());
  181. console.log('MD5 fingerprint: ' +
  182. key.fingerprint('md5').toString());
  183. console.log('SPKI-SHA256 fingerprint: ' +
  184. key.fingerprint('sha256', 'spki').toString());
  185. process.exit(0);
  186. return;
  187. }
  188. if (opts.fingerprint) {
  189. var hash = opts.hash;
  190. var type = opts.spki ? 'spki' : 'ssh';
  191. var format = opts.outformat;
  192. var fp = key.fingerprint(hash, type).toString(format);
  193. outFile.write(fp);
  194. outFile.write('\n');
  195. outFile.once('drain', function () {
  196. process.exit(0);
  197. });
  198. return;
  199. }
  200. fmt = undefined;
  201. if (opts.outformat)
  202. fmt = opts.outformat;
  203. outFile.write(key.toBuffer(fmt));
  204. if (fmt === 'ssh' ||
  205. (!opts.private && fmt === undefined))
  206. outFile.write('\n');
  207. outFile.once('drain', function () {
  208. process.exit(0);
  209. });
  210. });
  211. }
  212. function ifError(e, txt) {
  213. if (txt)
  214. txt = txt + ': ';
  215. else
  216. txt = '';
  217. console.error('sshpk-conv: ' + txt + e.name + ': ' + e.message);
  218. if (process.env['DEBUG'] || process.env['V']) {
  219. console.error(e.stack);
  220. if (e.innerErr)
  221. console.error(e.innerErr.stack);
  222. }
  223. process.exit(1);
  224. }