forked from mscdex/ssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-protocol-keyparser.js
161 lines (149 loc) · 5.01 KB
/
test-protocol-keyparser.js
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
'use strict';
const assert = require('assert');
const { readdirSync, readFileSync } = require('fs');
const { inspect } = require('util');
const { parseKey } = require('../lib/protocol/keyParser.js');
const { EDDSA_SUPPORTED } = require('../lib/protocol/constants.js');
const BASE_PATH = `${__dirname}/fixtures/keyParser`;
function failMsg(name, message, exit) {
const msg = `[${name}] ${message}`;
if (!exit)
return msg;
console.error(msg);
process.exit(1);
}
readdirSync(BASE_PATH).forEach((name) => {
if (/\.result$/i.test(name))
return;
if (/ed25519/i.test(name) && !EDDSA_SUPPORTED)
return;
const isPublic = /\.pub$/i.test(name);
const isEncrypted = /_enc/i.test(name);
const isPPK = /^ppk_/i.test(name);
const key = readFileSync(`${BASE_PATH}/${name}`);
let res;
if (isEncrypted)
res = parseKey(key, (isPPK ? 'node.js' : 'password'));
else
res = parseKey(key);
let expected = JSON.parse(
readFileSync(`${BASE_PATH}/${name}.result`, 'utf8')
);
if (typeof expected === 'string') {
if (!(res instanceof Error))
failMsg(name, `Expected error: ${expected}`, true);
assert.strictEqual(
expected,
res.message,
failMsg(name,
'Error message mismatch.\n'
+ `Expected: ${inspect(expected)}\n`
+ `Received: ${inspect(res.message)}`)
);
} else if (res instanceof Error) {
failMsg(name, `Unexpected error: ${res.stack}`, true);
} else {
if (Array.isArray(expected) && !Array.isArray(res))
failMsg(name, 'Expected array but did not receive one', true);
if (!Array.isArray(expected) && Array.isArray(res))
failMsg(name, 'Received array but did not expect one', true);
if (!Array.isArray(res)) {
res = [res];
expected = [expected];
} else if (res.length !== expected.length) {
failMsg(name,
`Expected ${expected.length} keys, but received ${res.length}`,
true);
}
res.forEach((curKey, i) => {
const details = {
type: curKey.type,
comment: curKey.comment,
public: curKey.getPublicPEM(),
publicSSH: curKey.getPublicSSH()
&& curKey.getPublicSSH().toString('base64'),
private: curKey.getPrivatePEM()
};
assert.deepStrictEqual(
details,
expected[i],
failMsg(name,
'Parser output mismatch.\n'
+ `Expected: ${inspect(expected[i])}\n\n`
+ `Received: ${inspect(details)}`)
);
});
}
if (isEncrypted && !isPublic) {
// Make sure parsing encrypted keys without a passphrase results in an
// appropriate error
const err = parseKey(key);
if (!(err instanceof Error))
failMsg(name, 'Expected error during parse without passphrase', true);
if (!/no passphrase/i.test(err.message)) {
failMsg(name,
`Error during parse without passphrase: ${err.message}`,
true);
}
// Make sure parsing encrypted keys with an incorrect passphrase results in
// an appropriate error
const errIncPass = parseKey(key, 'incorrectPassphrase');
if (!(errIncPass instanceof Error)) {
failMsg(name,
'Expected error during parse with an incorrect passphrase',
true);
}
if (!/bad passphrase\?|unable to authenticate data/i
.test(errIncPass.message)) {
failMsg(name,
'Error during parse with an incorrect passphrase: '
+ errIncPass.message,
true);
}
}
if (!isPublic) {
// Try signing and verifying to make sure the private/public key PEMs are
// correct
const data = Buffer.from('hello world');
res.forEach((curKey) => {
let result = curKey.sign(data);
if (result instanceof Error) {
failMsg(name,
`Error while signing data with key: ${result.message}`,
true);
}
result = curKey.verify(data, result);
if (result instanceof Error) {
failMsg(name,
`Error while verifying signed data with key: ${result.message}`,
true);
}
if (!result)
failMsg(name, 'Failed to verify signed data with key', true);
});
if (res.length === 1 && !isPPK) {
const pubFile = readFileSync(`${BASE_PATH}/${name}.pub`);
const pubParsed = parseKey(pubFile);
if (!(pubParsed instanceof Error)) {
let result = res[0].sign(data);
if (result instanceof Error) {
failMsg(name,
`Error while signing data with key: ${result.message}`,
true);
}
result = pubParsed.verify(data, result);
if (result instanceof Error) {
failMsg(name,
'Error while verifying signed data with separate public key: '
+ result.message,
true);
}
if (!result) {
failMsg(name,
'Failed to verify signed data with separate public key',
true);
}
}
}
}
});