test-server-hostkeys.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. const assert = require('assert');
  3. const {
  4. fixtureKey,
  5. mustCall,
  6. setup,
  7. } = require('./common.js');
  8. const debug = false;
  9. [
  10. { desc: 'RSA user key (old OpenSSH)',
  11. hostKey: fixtureKey('id_rsa') },
  12. { desc: 'RSA user key (new OpenSSH)',
  13. hostKey: fixtureKey('openssh_new_rsa') },
  14. { desc: 'DSA host key',
  15. hostKey: fixtureKey('ssh_host_dsa_key') },
  16. { desc: 'ECDSA host key',
  17. hostKey: fixtureKey('ssh_host_ecdsa_key') },
  18. { desc: 'PPK',
  19. hostKey: fixtureKey('id_rsa.ppk') },
  20. ].forEach((test) => {
  21. const { desc, hostKey } = test;
  22. const clientKey = fixtureKey('openssh_new_rsa');
  23. const username = 'KeyUser';
  24. const { server } = setup(
  25. desc,
  26. {
  27. client: {
  28. username,
  29. privateKey: clientKey.raw,
  30. algorithms: {
  31. serverHostKey: [ hostKey.key.type ],
  32. }
  33. },
  34. server: { hostKeys: [ hostKey.raw ] },
  35. debug,
  36. }
  37. );
  38. server.on('connection', mustCall((conn) => {
  39. let authAttempt = 0;
  40. conn.on('authentication', mustCall((ctx) => {
  41. assert(ctx.username === username,
  42. `Wrong username: ${ctx.username}`);
  43. switch (++authAttempt) {
  44. case 1:
  45. assert(ctx.method === 'none',
  46. `Wrong auth method: ${ctx.method}`);
  47. return ctx.reject();
  48. case 3:
  49. assert(ctx.signature, 'Missing publickey signature');
  50. // FALLTHROUGH
  51. case 2:
  52. assert(ctx.method === 'publickey',
  53. `Wrong auth method: ${ctx.method}`);
  54. assert(ctx.key.algo === clientKey.key.type,
  55. `Wrong key algo: ${ctx.key.algo}`);
  56. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  57. ctx.key.data,
  58. 'Public key mismatch');
  59. break;
  60. }
  61. if (ctx.signature) {
  62. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  63. 'Could not verify publickey signature');
  64. }
  65. ctx.accept();
  66. }, 3)).on('ready', mustCall(() => {
  67. conn.end();
  68. }));
  69. }));
  70. });
  71. {
  72. const RSA_KEY = fixtureKey('ssh_host_rsa_key');
  73. const ECDSA_KEY = fixtureKey('ssh_host_ecdsa_key');
  74. [ RSA_KEY, ECDSA_KEY ].forEach((key) => {
  75. const selKeyType = key.key.type;
  76. const clientKey = fixtureKey('openssh_new_rsa');
  77. const username = 'KeyUser';
  78. const { client, server } = setup(
  79. `Multiple host key types (${key.type} selected)`,
  80. {
  81. client: {
  82. username,
  83. privateKey: clientKey.raw,
  84. algorithms: {
  85. serverHostKey: [ selKeyType ],
  86. }
  87. },
  88. server: { hostKeys: [ RSA_KEY.raw, ECDSA_KEY.raw ] },
  89. debug,
  90. }
  91. );
  92. server.on('connection', mustCall((conn) => {
  93. let authAttempt = 0;
  94. conn.on('authentication', mustCall((ctx) => {
  95. assert(ctx.username === username,
  96. `Wrong username: ${ctx.username}`);
  97. switch (++authAttempt) {
  98. case 1:
  99. assert(ctx.method === 'none',
  100. `Wrong auth method: ${ctx.method}`);
  101. return ctx.reject();
  102. case 3:
  103. assert(ctx.signature, 'Missing publickey signature');
  104. // FALLTHROUGH
  105. case 2:
  106. assert(ctx.method === 'publickey',
  107. `Wrong auth method: ${ctx.method}`);
  108. assert(ctx.key.algo === clientKey.key.type,
  109. `Wrong key algo: ${ctx.key.algo}`);
  110. assert.deepStrictEqual(clientKey.key.getPublicSSH(),
  111. ctx.key.data,
  112. 'Public key mismatch');
  113. break;
  114. }
  115. if (ctx.signature) {
  116. assert(clientKey.key.verify(ctx.blob, ctx.signature) === true,
  117. 'Could not verify publickey signature');
  118. }
  119. ctx.accept();
  120. }, 3)).on('ready', mustCall(() => {
  121. conn.end();
  122. }));
  123. }));
  124. client.on('handshake', mustCall((info) => {
  125. assert(info.serverHostKey === selKeyType, 'Wrong host key selected');
  126. }));
  127. });
  128. }