sshpk-sign 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 getPassword = require('getpass').getPass;
  10. var options = [
  11. {
  12. names: ['hash', 'H'],
  13. type: 'string',
  14. help: 'Hash algorithm (sha1, sha256, sha384, sha512)'
  15. },
  16. {
  17. names: ['verbose', 'v'],
  18. type: 'bool',
  19. help: 'Display verbose info about key and hash used'
  20. },
  21. {
  22. names: ['identity', 'i'],
  23. type: 'string',
  24. help: 'Path to key to use'
  25. },
  26. {
  27. names: ['file', 'f'],
  28. type: 'string',
  29. help: 'Input filename'
  30. },
  31. {
  32. names: ['out', 'o'],
  33. type: 'string',
  34. help: 'Output filename'
  35. },
  36. {
  37. names: ['format', 't'],
  38. type: 'string',
  39. help: 'Signature format (asn1, ssh, raw)'
  40. },
  41. {
  42. names: ['binary', 'b'],
  43. type: 'bool',
  44. help: 'Output raw binary instead of base64'
  45. },
  46. {
  47. names: ['help', 'h'],
  48. type: 'bool',
  49. help: 'Shows this help text'
  50. }
  51. ];
  52. var parseOpts = {};
  53. if (require.main === module) {
  54. var parser = dashdash.createParser({
  55. options: options
  56. });
  57. try {
  58. var opts = parser.parse(process.argv);
  59. } catch (e) {
  60. console.error('sshpk-sign: error: %s', e.message);
  61. process.exit(1);
  62. }
  63. if (opts.help || opts._args.length > 1) {
  64. var help = parser.help({}).trimRight();
  65. console.error('sshpk-sign: sign data using an SSH key\n');
  66. console.error(help);
  67. process.exit(1);
  68. }
  69. if (!opts.identity) {
  70. var help = parser.help({}).trimRight();
  71. console.error('sshpk-sign: the -i or --identity option ' +
  72. 'is required\n');
  73. console.error(help);
  74. process.exit(1);
  75. }
  76. var keyData = fs.readFileSync(opts.identity);
  77. parseOpts.filename = opts.identity;
  78. run();
  79. }
  80. function run() {
  81. var key;
  82. try {
  83. key = sshpk.parsePrivateKey(keyData, 'auto', parseOpts);
  84. } catch (e) {
  85. if (e.name === 'KeyEncryptedError') {
  86. getPassword(function (err, pw) {
  87. parseOpts.passphrase = pw;
  88. run();
  89. });
  90. return;
  91. }
  92. console.error('sshpk-sign: error loading private key "' +
  93. opts.identity + '": ' + e.name + ': ' + e.message);
  94. process.exit(1);
  95. }
  96. var hash = opts.hash || key.defaultHashAlgorithm();
  97. var signer;
  98. try {
  99. signer = key.createSign(hash);
  100. } catch (e) {
  101. console.error('sshpk-sign: error creating signer: ' +
  102. e.name + ': ' + e.message);
  103. process.exit(1);
  104. }
  105. if (opts.verbose) {
  106. console.error('sshpk-sign: using %s-%s with a %d bit key',
  107. key.type, hash, key.size);
  108. }
  109. var inFile = process.stdin;
  110. var inFileName = 'stdin';
  111. var inFilePath;
  112. if (opts.file) {
  113. inFilePath = opts.file;
  114. } else if (opts._args.length === 1) {
  115. inFilePath = opts._args[0];
  116. }
  117. if (inFilePath)
  118. inFileName = path.basename(inFilePath);
  119. try {
  120. if (inFilePath) {
  121. fs.accessSync(inFilePath, fs.R_OK);
  122. inFile = fs.createReadStream(inFilePath);
  123. }
  124. } catch (e) {
  125. console.error('sshpk-sign: error opening input file' +
  126. ': ' + e.name + ': ' + e.message);
  127. process.exit(1);
  128. }
  129. var outFile = process.stdout;
  130. try {
  131. if (opts.out && !opts.identify) {
  132. fs.accessSync(path.dirname(opts.out), fs.W_OK);
  133. outFile = fs.createWriteStream(opts.out);
  134. }
  135. } catch (e) {
  136. console.error('sshpk-sign: error opening output file' +
  137. ': ' + e.name + ': ' + e.message);
  138. process.exit(1);
  139. }
  140. inFile.pipe(signer);
  141. inFile.on('end', function () {
  142. var sig;
  143. try {
  144. sig = signer.sign();
  145. } catch (e) {
  146. console.error('sshpk-sign: error signing data: ' +
  147. e.name + ': ' + e.message);
  148. process.exit(1);
  149. }
  150. var fmt = opts.format || 'asn1';
  151. var output;
  152. try {
  153. output = sig.toBuffer(fmt);
  154. if (!opts.binary)
  155. output = output.toString('base64');
  156. } catch (e) {
  157. console.error('sshpk-sign: error converting signature' +
  158. ' to ' + fmt + ' format: ' + e.name + ': ' +
  159. e.message);
  160. process.exit(1);
  161. }
  162. outFile.write(output);
  163. if (!opts.binary)
  164. outFile.write('\n');
  165. outFile.once('drain', function () {
  166. process.exit(0);
  167. });
  168. });
  169. }