-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathssh-openssh.test.ts
More file actions
377 lines (374 loc) · 14.2 KB
/
Copy pathssh-openssh.test.ts
File metadata and controls
377 lines (374 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { describe, should } from '@paulmillr/jsbt/test.js';
import { concatBytes } from '@noble/hashes/utils.js';
import { base64, hex } from '@scure/base';
import { execFileSync, spawnSync } from 'node:child_process';
import { deepStrictEqual, throws } from 'node:assert';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as ssh from '../src/ssh.ts';
// Run directly; this is intentionally not imported by test/index.ts because it needs local OpenSSH ssh-keygen.
const seed = hex.decode('71e722b077c007d4ae263287878a0bff1816c99f93cf8dcddd995bccefd1d7a3');
const check = hex.decode('c346f14a');
const tmp = <T>(fn: (dir: string) => T): T => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mkp-ssh-'));
fs.chmodSync(dir, 0o700);
try {
return fn(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
};
const sshEnv = (home: string) => {
const { SSH_AUTH_SOCK, SSH_AGENT_PID, ...env } = process.env;
return { ...env, HOME: home };
};
const sshKeygen = (home: string, args: string[]): string =>
execFileSync('ssh-keygen', args, {
encoding: 'utf8',
env: sshEnv(home),
stdio: ['ignore', 'pipe', 'pipe'],
});
const U32BE = (n: number) => Uint8Array.of(n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff);
const readU32BE = (bytes: Uint8Array, pos: number) =>
bytes[pos]! * 2 ** 24 + bytes[pos + 1]! * 2 ** 16 + bytes[pos + 2]! * 2 ** 8 + bytes[pos + 3]!;
const readSSHField = (bytes: Uint8Array, pos: number): [Uint8Array, number] => {
const len = readU32BE(bytes, pos);
const end = pos + 4 + len;
return [bytes.subarray(pos, end), end];
};
const replaceSSHField = (bytes: Uint8Array, pos: number, value: Uint8Array): Uint8Array => {
const [, end] = readSSHField(bytes, pos);
return concatBytes(bytes.subarray(0, pos), U32BE(value.length), value, bytes.subarray(end));
};
const rawArmor = (armor: string): Uint8Array =>
base64.decode(
armor
.trim()
.split('\n')
.filter((line) => line && !line.startsWith('-----'))
.join('')
);
const wrapArmor = (raw: Uint8Array): string => {
const body = base64.encode(raw);
const lines = [];
for (let i = 0; i < body.length; i += 70) lines.push(body.slice(i, i + 70));
return `-----BEGIN OPENSSH PRIVATE KEY-----\n\n${lines.join('\n')}\n-----END OPENSSH PRIVATE KEY-----\n`;
};
const envelopeParts = (armor: string) => {
const raw = rawArmor(armor);
let pos = 'openssh-key-v1\0'.length;
const [, afterCipher] = readSSHField(raw, pos);
const [, afterKdf] = readSSHField(raw, afterCipher);
const [, afterKdfopts] = readSSHField(raw, afterKdf);
pos = afterKdfopts + 4;
const [pub, afterPub] = readSSHField(raw, pos);
const [priv, end] = readSSHField(raw, afterPub);
if (end !== raw.length)
throw new Error('OpenSSH test fixture expected one public key and one private block');
return { raw, afterKdfopts, pub, priv, afterPub, end };
};
const duplicatePrivateEnvelope = (armor: string): string => {
const { raw, afterKdfopts, pub, priv } = envelopeParts(armor);
return wrapArmor(concatBytes(raw.subarray(0, afterKdfopts), U32BE(2), pub, priv, pub, priv));
};
type PrivateBlock = {
check1: Uint8Array;
check2: Uint8Array;
pubKey: Uint8Array;
privKey: Uint8Array;
comment: string;
};
const padding = (len: number): Uint8Array => {
const padLen = (8 - (len % 8)) % 8;
return Uint8Array.from({ length: padLen }, (_, i) => i + 1);
};
const privateBlock = (armor: string, mutate: (block: PrivateBlock) => void): Uint8Array => {
const data = ssh.PrivateExport.decode(armor);
const key = data.keys[0]!;
const block: PrivateBlock = {
check1: Uint8Array.from(key.privKey.check1),
check2: Uint8Array.from(key.privKey.check2),
pubKey: Uint8Array.from(key.privKey.pubKey),
privKey: Uint8Array.from(key.privKey.privKey),
comment: key.privKey.comment,
};
mutate(block);
const body = concatBytes(
block.check1,
block.check2,
ssh.SSHKeyType.encode(undefined),
ssh.SSHBuf.encode(block.pubKey),
ssh.SSHBuf.encode(block.privKey),
ssh.SSHString.encode(block.comment)
);
return concatBytes(body, padding(body.length));
};
const mutatePrivateBlock = (armor: string, mutate: (block: PrivateBlock) => void): string => {
const { raw, afterPub } = envelopeParts(armor);
const block = privateBlock(armor, mutate);
return wrapArmor(replaceSSHField(raw, afterPub, block));
};
const mutatePrivateBlockRaw = (armor: string, mutate: (block: Uint8Array) => void): string => {
const { raw, afterPub } = envelopeParts(armor);
const [field] = readSSHField(raw, afterPub);
const block = Uint8Array.from(field.subarray(4));
mutate(block);
return wrapArmor(replaceSSHField(raw, afterPub, block));
};
const mutateOuterPublicKey = (armor: string, mutate: (pubKey: Uint8Array) => void): string => {
const { raw, afterKdfopts } = envelopeParts(armor);
const pos = afterKdfopts + 4;
const [field] = readSSHField(raw, pos);
const pubKey = Uint8Array.from(ssh.PublicKey.decode(field.subarray(4)).pubKey);
mutate(pubKey);
return wrapArmor(replaceSSHField(raw, pos, ssh.PublicKey.encode({ pubKey })));
};
const sshKeygenPrivate = (armor: string) =>
tmp((dir) => {
const file = path.join(dir, 'key');
fs.writeFileSync(file, armor, { mode: 0o600 });
// -P keeps malformed-key checks non-interactive even when OpenSSH guesses encryption.
const res = spawnSync('ssh-keygen', ['-y', '-P', '', '-f', file], {
encoding: 'utf8',
env: sshEnv(dir),
stdio: ['ignore', 'pipe', 'pipe'],
});
return {
status: res.status,
stdout: res.stdout,
stderr: res.stderr.replaceAll(file, '<key>'),
};
});
const publicBlob = (pubKey: Uint8Array): Uint8Array =>
concatBytes(ssh.SSHKeyType.encode(undefined), ssh.SSHBuf.encode(pubKey));
const sshKeygenPublic = (pubKey: Uint8Array) =>
tmp((dir) => {
const file = path.join(dir, 'key.pub');
fs.writeFileSync(file, `ssh-ed25519 ${base64.encode(publicBlob(pubKey))} user@pc\n`);
const res = spawnSync('ssh-keygen', ['-l', '-E', 'sha256', '-f', file], {
encoding: 'utf8',
env: sshEnv(dir),
stdio: ['ignore', 'pipe', 'pipe'],
});
return {
status: res.status,
stdout: res.stdout,
stderr: res.stderr.replaceAll(file, '<pub>'),
};
});
describe('ssh openssh', () => {
should('OpenSSH accepts locally generated private and public keys', () => {
const keys = ssh.getKeys(seed, 'user@pc', check);
tmp((dir) => {
const key = path.join(dir, 'id_ed25519');
const pub = `${key}.pub`;
fs.writeFileSync(key, keys.privateKey);
fs.chmodSync(key, 0o600);
fs.writeFileSync(pub, `${keys.publicKey}\n`);
const fingerprint = `256 ${keys.fingerprint} user@pc (ED25519)\n`;
deepStrictEqual(
{
publicFromPrivate: sshKeygen(dir, ['-y', '-P', '', '-f', key]),
privateFingerprint: sshKeygen(dir, ['-l', '-E', 'sha256', '-f', key]),
publicFingerprint: sshKeygen(dir, ['-l', '-E', 'sha256', '-f', pub]),
},
{
publicFromPrivate: `${keys.publicKey}\n`,
privateFingerprint: fingerprint,
publicFingerprint: fingerprint,
}
);
});
});
should('decodes OpenSSH-generated ed25519 private keys', () => {
tmp((dir) => {
const key = path.join(dir, 'id_ed25519');
sshKeygen(dir, ['-q', '-t', 'ed25519', '-N', '', '-C', 'openssh@example.com', '-f', key]);
const decoded = ssh.PrivateExport.decode(fs.readFileSync(key, 'utf8'));
const publicKey = fs.readFileSync(`${key}.pub`, 'utf8');
const first = decoded.keys[0]!;
const fingerprint = sshKeygen(dir, ['-l', '-E', 'sha256', '-f', key]).split(' ')[1]!;
deepStrictEqual(
{
keyCount: decoded.keys.length,
privateBlobLength: first.privKey.privKey.length,
comment: first.privKey.comment,
publicKey: `${ssh.formatPublicKey(first.pubKey.pubKey, first.privKey.comment)}\n`,
fingerprint: ssh.getFingerprint(first.pubKey.pubKey),
},
{
keyCount: 1,
privateBlobLength: 64,
comment: 'openssh@example.com',
publicKey,
fingerprint,
}
);
});
});
should('decodes OpenSSH-generated UTF-8 private-key comments', () => {
tmp((dir) => {
const key = path.join(dir, 'id_ed25519');
const comment = 'openssh-\u00e9@example.com';
sshKeygen(dir, ['-q', '-t', 'ed25519', '-N', '', '-C', comment, '-f', key]);
const decoded = ssh.PrivateExport.decode(fs.readFileSync(key, 'utf8'));
const first = decoded.keys[0]!;
deepStrictEqual(
{
comment: first.privKey.comment,
publicKey: `${ssh.formatPublicKey(first.pubKey.pubKey, first.privKey.comment)}\n`,
},
{
comment,
publicKey: fs.readFileSync(`${key}.pub`, 'utf8'),
}
);
});
});
should('OpenSSH encrypted private keys stay explicitly unsupported locally', () => {
tmp((dir) => {
const key = path.join(dir, 'id_ed25519');
sshKeygen(dir, ['-q', '-t', 'ed25519', '-N', 'passphrase', '-C', 'encrypted', '-f', key]);
const pub = fs.readFileSync(`${key}.pub`, 'utf8');
deepStrictEqual(sshKeygen(dir, ['-y', '-P', 'passphrase', '-f', key]), pub);
// OpenSSH's private-key envelope encryption format is outside this
// package's current no-decrypt Ed25519 profile; keep this as an explicit
// compatibility boundary for generated encrypted keys.
throws(() => ssh.PrivateExport.decode(fs.readFileSync(key, 'utf8')));
});
});
should('OpenSSH and local decoder reject multi-key private envelopes', () => {
const keys = ssh.getKeys(seed, 'user@pc', check);
const multi = duplicatePrivateEnvelope(keys.privateKey);
throws(() => ssh.PrivateExport.decode(multi));
deepStrictEqual(sshKeygenPrivate(multi), {
status: 255,
stdout: '',
stderr: 'Load key "<key>": error in libcrypto\r\n',
});
});
should('OpenSSH and local decoder reject mismatched outer private-envelope public key', () => {
const keys = ssh.getKeys(seed, 'user@pc', check);
const mismatch = mutateOuterPublicKey(keys.privateKey, (pubKey) => {
pubKey[0] ^= 1;
});
throws(() => ssh.PrivateExport.decode(mismatch));
deepStrictEqual(sshKeygenPrivate(mismatch), {
status: 255,
stdout: '',
stderr: 'Load key "<key>": error in libcrypto\r\n',
});
});
should('compares malformed private block handling with OpenSSH', () => {
const keys = ssh.getKeys(seed, 'user@pc', check);
const cases = {
mismatchedCheck: mutatePrivateBlock(keys.privateKey, (block) => {
block.check2[0] ^= 1;
}),
shortPub: mutatePrivateBlock(keys.privateKey, (block) => {
block.pubKey = block.pubKey.slice(0, 31);
}),
shortPriv: mutatePrivateBlock(keys.privateKey, (block) => {
block.privKey = block.privKey.slice(0, 63);
}),
mismatchedAppendedPub: mutatePrivateBlock(keys.privateKey, (block) => {
block.privKey[63] ^= 1;
}),
wrongSeed: mutatePrivateBlock(keys.privateKey, (block) => {
block.privKey[0] ^= 1;
}),
badPadding: mutatePrivateBlockRaw(keys.privateKey, (block) => {
block[block.length - 1] ^= 0xff;
}),
};
const res: Record<string, { decThrows: boolean; ssh: ReturnType<typeof sshKeygenPrivate> }> =
{};
for (const name in cases) {
try {
ssh.PrivateExport.decode(cases[name as keyof typeof cases]);
res[name] = { decThrows: false, ssh: sshKeygenPrivate(cases[name as keyof typeof cases]) };
} catch {
res[name] = { decThrows: true, ssh: sshKeygenPrivate(cases[name as keyof typeof cases]) };
}
}
deepStrictEqual(res, {
mismatchedCheck: {
decThrows: true,
ssh: {
status: 255,
stdout: '',
stderr: 'Load key "<key>": incorrect passphrase supplied to decrypt private key\r\n',
},
},
shortPub: {
decThrows: true,
ssh: { status: 255, stdout: '', stderr: 'Load key "<key>": error in libcrypto\r\n' },
},
shortPriv: {
decThrows: true,
ssh: { status: 255, stdout: '', stderr: 'Load key "<key>": error in libcrypto\r\n' },
},
mismatchedAppendedPub: {
decThrows: true,
ssh: { status: 0, stdout: `${keys.publicKey}\n`, stderr: '' },
},
wrongSeed: {
decThrows: true,
ssh: { status: 0, stdout: `${keys.publicKey}\n`, stderr: '' },
},
badPadding: {
decThrows: true,
ssh: { status: 255, stdout: '', stderr: 'Load key "<key>": error in libcrypto\r\n' },
},
});
});
should('OpenSSH and local decoder reject non-32-byte public-key blobs', () => {
const cases = {
short: seed.slice(0, 31),
long: concatBytes(seed, Uint8Array.of(0)),
};
const res: Record<string, { decThrows: boolean; ssh: ReturnType<typeof sshKeygenPublic> }> = {};
for (const name in cases) {
const pubKey = cases[name as keyof typeof cases];
try {
ssh.PublicKey.decode(publicBlob(pubKey));
res[name] = { decThrows: false, ssh: sshKeygenPublic(pubKey) };
} catch {
res[name] = { decThrows: true, ssh: sshKeygenPublic(pubKey) };
}
}
deepStrictEqual(res, {
short: {
decThrows: true,
ssh: { status: 255, stdout: '', stderr: '<pub> is not a public key file.\r\n' },
},
long: {
decThrows: true,
ssh: { status: 255, stdout: '', stderr: '<pub> is not a public key file.\r\n' },
},
});
});
should('OpenSSH truncates one-line public-key comments at CR and LF', () => {
const keys = ssh.getKeys(seed, 'user@pc', check);
const [type, blob] = keys.publicKey.split(' ');
tmp((dir) => {
const lf = path.join(dir, 'lf.pub');
const cr = path.join(dir, 'cr.pub');
fs.writeFileSync(lf, `${type} ${blob} user\nother\n`);
fs.writeFileSync(cr, `${type} ${blob} user\rother\n`);
const truncated = `256 ${keys.fingerprint} user (ED25519)\n`;
deepStrictEqual(
{
lf: sshKeygen(dir, ['-l', '-E', 'sha256', '-f', lf]),
cr: sshKeygen(dir, ['-l', '-E', 'sha256', '-f', cr]),
},
{
lf: truncated,
cr: truncated,
}
);
});
});
});
should.runWhen(import.meta.url);