| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, readFileSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import ssh2, { type |
| 6 | import { afterAll, describe |
| 7 | |
| 8 | import { |
| 9 | fingerprintOf, |
| 10 | keyTypeOf, |
| 11 | parseStoredKey, |
| 12 | readPublicKey, |
| 13 | } from './public-key.js'; |
| 14 | |
| 15 | const directory = mkdtempSync |
| 16 | let generated = 0 |
| 17 | |
| 18 | function generate(type: |
| 19 | const file = |
| 20 | const args = |
| 21 | execFileSync('ssh-keygen', |
| 22 | return { |
| 23 | line: readFileSync(`${ |
| 24 | private: readFileSync(file |
| 25 | // `ssh-keygen -lf` prints "<bits> SHA256:<fingerprint> <comment> (<TYPE>)". |
| 26 | fingerprint: execFileSync('ssh-keygen |
| 27 | encoding: 'utf8', |
| 28 | }) |
| 29 | .split(' ')[ |
| 30 | .replace('SHA256:', |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | afterAll(() => rmSync |
| 35 | |
| 36 | describe('readPublicKey', () |
| 37 | it('agrees with ssh-keygen about the fingerprint', |
| 38 | const generated = |
| 39 | const key = |
| 40 | |
| 41 | expect(key.type |
| 42 | expect(key.fingerprint |
| 43 | expect(key.comment |
| 44 | }); |
| 45 | |
| 46 | it('stores the blob so its type can be read back without a second column', |
| 47 | const key = |
| 48 | |
| 49 | expect(keyTypeOf(key |
| 50 | expect(fingerprintOf(Buffer |
| 51 | key.fingerprint, |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | it('verifies a signature made by the matching private key', |
| 56 | const generated = |
| 57 | const stored = |
| 58 | const data = |
| 59 | 'the session identifier and the rest of the auth request', |
| 60 | ); |
| 61 | |
| 62 | const signature = |
| 63 | ssh2.utils.parseKey |
| 64 | ).sign(data) |
| 65 | expect(parseStoredKey(stored |
| 66 | expect( |
| 67 | parseStoredKey(stored.blob |
| 68 | ).toBe(false); |
| 69 | }); |
| 70 | |
| 71 | it('refuses a private key', |
| 72 | expect(() => |
| 73 | /private key/, |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | it('refuses an RSA key below 2048 bits', |
| 78 | expect(() => |
| 79 | /2048 bits/, |
| 80 | ); |
| 81 | }); |
| 82 | |
| 83 | it('refuses a certificate', |
| 84 | expect(() => |
| 85 | readPublicKey('ssh-ed25519-cert-v01@openssh.com AAAA nobody@nowhere'), |
| 86 | ).toThrow(/certificates/ |
| 87 | }); |
| 88 | |
| 89 | it.each(['', |
| 90 | 'refuses %j', |
| 91 | (line) => |
| 92 | expect(() => |
| 93 | }, |
| 94 | ); |
| 95 | }); |