fingerprint.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015 Joyent, Inc.
  2. module.exports = Fingerprint;
  3. var assert = require('assert-plus');
  4. var algs = require('./algs');
  5. var crypto = require('crypto');
  6. var errs = require('./errors');
  7. var Key = require('./key');
  8. var Certificate = require('./certificate');
  9. var utils = require('./utils');
  10. var FingerprintFormatError = errs.FingerprintFormatError;
  11. var InvalidAlgorithmError = errs.InvalidAlgorithmError;
  12. function Fingerprint(opts) {
  13. assert.object(opts, 'options');
  14. assert.string(opts.type, 'options.type');
  15. assert.buffer(opts.hash, 'options.hash');
  16. assert.string(opts.algorithm, 'options.algorithm');
  17. this.algorithm = opts.algorithm.toLowerCase();
  18. if (algs.hashAlgs[this.algorithm] !== true)
  19. throw (new InvalidAlgorithmError(this.algorithm));
  20. this.hash = opts.hash;
  21. this.type = opts.type;
  22. }
  23. Fingerprint.prototype.toString = function (format) {
  24. if (format === undefined) {
  25. if (this.algorithm === 'md5')
  26. format = 'hex';
  27. else
  28. format = 'base64';
  29. }
  30. assert.string(format);
  31. switch (format) {
  32. case 'hex':
  33. return (addColons(this.hash.toString('hex')));
  34. case 'base64':
  35. return (sshBase64Format(this.algorithm,
  36. this.hash.toString('base64')));
  37. default:
  38. throw (new FingerprintFormatError(undefined, format));
  39. }
  40. };
  41. Fingerprint.prototype.matches = function (other) {
  42. assert.object(other, 'key or certificate');
  43. if (this.type === 'key') {
  44. utils.assertCompatible(other, Key, [1, 0], 'key');
  45. } else {
  46. utils.assertCompatible(other, Certificate, [1, 0],
  47. 'certificate');
  48. }
  49. var theirHash = other.hash(this.algorithm);
  50. var theirHash2 = crypto.createHash(this.algorithm).
  51. update(theirHash).digest('base64');
  52. if (this.hash2 === undefined)
  53. this.hash2 = crypto.createHash(this.algorithm).
  54. update(this.hash).digest('base64');
  55. return (this.hash2 === theirHash2);
  56. };
  57. Fingerprint.parse = function (fp, options) {
  58. assert.string(fp, 'fingerprint');
  59. var alg, hash, enAlgs;
  60. if (Array.isArray(options)) {
  61. enAlgs = options;
  62. options = {};
  63. }
  64. assert.optionalObject(options, 'options');
  65. if (options === undefined)
  66. options = {};
  67. if (options.enAlgs !== undefined)
  68. enAlgs = options.enAlgs;
  69. assert.optionalArrayOfString(enAlgs, 'algorithms');
  70. var parts = fp.split(':');
  71. if (parts.length == 2) {
  72. alg = parts[0].toLowerCase();
  73. /*JSSTYLED*/
  74. var base64RE = /^[A-Za-z0-9+\/=]+$/;
  75. if (!base64RE.test(parts[1]))
  76. throw (new FingerprintFormatError(fp));
  77. try {
  78. hash = new Buffer(parts[1], 'base64');
  79. } catch (e) {
  80. throw (new FingerprintFormatError(fp));
  81. }
  82. } else if (parts.length > 2) {
  83. alg = 'md5';
  84. if (parts[0].toLowerCase() === 'md5')
  85. parts = parts.slice(1);
  86. parts = parts.join('');
  87. /*JSSTYLED*/
  88. var md5RE = /^[a-fA-F0-9]+$/;
  89. if (!md5RE.test(parts))
  90. throw (new FingerprintFormatError(fp));
  91. try {
  92. hash = new Buffer(parts, 'hex');
  93. } catch (e) {
  94. throw (new FingerprintFormatError(fp));
  95. }
  96. }
  97. if (alg === undefined)
  98. throw (new FingerprintFormatError(fp));
  99. if (algs.hashAlgs[alg] === undefined)
  100. throw (new InvalidAlgorithmError(alg));
  101. if (enAlgs !== undefined) {
  102. enAlgs = enAlgs.map(function (a) { return a.toLowerCase(); });
  103. if (enAlgs.indexOf(alg) === -1)
  104. throw (new InvalidAlgorithmError(alg));
  105. }
  106. return (new Fingerprint({
  107. algorithm: alg,
  108. hash: hash,
  109. type: options.type || 'key'
  110. }));
  111. };
  112. function addColons(s) {
  113. /*JSSTYLED*/
  114. return (s.replace(/(.{2})(?=.)/g, '$1:'));
  115. }
  116. function base64Strip(s) {
  117. /*JSSTYLED*/
  118. return (s.replace(/=*$/, ''));
  119. }
  120. function sshBase64Format(alg, h) {
  121. return (alg.toUpperCase() + ':' + base64Strip(h));
  122. }
  123. Fingerprint.isFingerprint = function (obj, ver) {
  124. return (utils.isCompatible(obj, Fingerprint, ver));
  125. };
  126. /*
  127. * API versions for Fingerprint:
  128. * [1,0] -- initial ver
  129. * [1,1] -- first tagged ver
  130. */
  131. Fingerprint.prototype._sshpkApiVersion = [1, 1];
  132. Fingerprint._oldVersionDetect = function (obj) {
  133. assert.func(obj.toString);
  134. assert.func(obj.matches);
  135. return ([1, 0]);
  136. };