@lethevimlet/sshift 1.1.7 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -22,7 +22,7 @@ function registerUtilsEndpoints(app, io) {
|
|
|
22
22
|
const info = detectKeyFormat(content);
|
|
23
23
|
if (info.format === 'ppk') {
|
|
24
24
|
try {
|
|
25
|
-
const converted = await convertPPKToOpenSSH(content, passphrase);
|
|
25
|
+
const converted = await convertPPKToOpenSSH(content, passphrase || undefined);
|
|
26
26
|
res.json({ success: true, key: converted, format: 'openssh' });
|
|
27
27
|
} catch (e) {
|
|
28
28
|
res.status(400).json({ error: e.message });
|
|
@@ -57,98 +57,70 @@ function convertPPKViaPuttygen(ppkContent, passphrase) {
|
|
|
57
57
|
const inputFile = path.join(tmpDir, `sshift-ppk-${Date.now()}-${Math.random().toString(36).slice(2)}.ppk`);
|
|
58
58
|
const outputFile = inputFile + '.openssh';
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
if (writeErr) {
|
|
62
|
-
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
63
|
-
return reject(new Error('Failed to write temporary PPK file: ' + writeErr.message));
|
|
64
|
-
}
|
|
65
|
-
|
|
60
|
+
const doConvert = (passphraseFile) => {
|
|
66
61
|
const args = [inputFile, '-O', 'private-openssh-new', '-o', outputFile];
|
|
67
|
-
if (
|
|
68
|
-
args.
|
|
62
|
+
if (passphraseFile) {
|
|
63
|
+
args.push('--old-passphrase', passphraseFile);
|
|
69
64
|
}
|
|
70
65
|
|
|
71
66
|
execFile('puttygen', args, (err, stdout, stderr) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
67
|
+
const filesToClean = passphraseFile
|
|
68
|
+
? [inputFile, outputFile, passphraseFile]
|
|
69
|
+
: [inputFile, outputFile];
|
|
70
|
+
|
|
71
|
+
if (err) {
|
|
72
|
+
for (const f of filesToClean) { try { fs.unlinkSync(f); } catch (e) { /* ignore */ } }
|
|
73
|
+
const stderrStr = (stderr || '').trim();
|
|
74
|
+
const errMsg = stderrStr || err.message;
|
|
75
|
+
|
|
76
|
+
if (stderrStr.toLowerCase().includes('wrong passphrase') || stderrStr.toLowerCase().includes('unable to read')) {
|
|
77
|
+
return reject(new Error(
|
|
78
|
+
'PPK passphrase is incorrect. Please check the passphrase and try again.'
|
|
79
|
+
));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return reject(new Error('puttygen conversion failed: ' + errMsg));
|
|
75
83
|
}
|
|
76
84
|
|
|
77
|
-
|
|
85
|
+
fs.readFile(outputFile, 'utf8', (readErr, data) => {
|
|
86
|
+
if (passphraseFile) {
|
|
87
|
+
try { fs.unlinkSync(passphraseFile); } catch (e) { /* ignore */ }
|
|
88
|
+
}
|
|
89
|
+
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
90
|
+
try { fs.unlinkSync(outputFile); } catch (e) { /* ignore */ }
|
|
91
|
+
if (readErr) {
|
|
92
|
+
return reject(new Error('Failed to read converted key: ' + readErr.message));
|
|
93
|
+
}
|
|
94
|
+
resolve(data);
|
|
95
|
+
});
|
|
78
96
|
});
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function tryConvertPPKWithPassphraseFile(inputFile, outputFile, passphrase, resolve, reject) {
|
|
84
|
-
const tmpDir = os.tmpdir();
|
|
85
|
-
const passphraseFile = path.join(tmpDir, `sshift-ppk-pass-${Date.now()}-${Math.random().toString(36).slice(2)}.txt`);
|
|
86
|
-
|
|
87
|
-
fs.writeFile(passphraseFile, passphrase, { mode: 0o600 }, (writeErr) => {
|
|
88
|
-
if (writeErr) {
|
|
89
|
-
cleanupTempFiles([inputFile, outputFile]);
|
|
90
|
-
return reject(new Error(
|
|
91
|
-
'Failed to write passphrase file for PPK conversion: ' + writeErr.message
|
|
92
|
-
));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const args = [inputFile, '-O', 'private-openssh-new', '-o', outputFile, `--old-passphrase-file=${passphraseFile}`];
|
|
97
|
+
};
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
cleanupTempFiles([inputFile, outputFile]);
|
|
102
|
-
return reject(new Error(
|
|
103
|
-
'puttygen conversion failed: ' + (stderr || err.message) +
|
|
104
|
-
'. Ensure puttygen is installed and the passphrase is correct.'
|
|
105
|
-
));
|
|
99
|
+
fs.writeFile(inputFile, ppkContent, (writeErr) => {
|
|
100
|
+
if (writeErr) {
|
|
101
|
+
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
102
|
+
return reject(new Error('Failed to write temporary PPK file: ' + writeErr.message));
|
|
106
103
|
}
|
|
107
104
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return reject(new Error('Failed to
|
|
105
|
+
const passphraseFile = path.join(tmpDir, `sshift-ppk-pass-${Date.now()}-${Math.random().toString(36).slice(2)}.txt`);
|
|
106
|
+
fs.writeFile(passphraseFile, passphrase || '', { mode: 0o600 }, (passWriteErr) => {
|
|
107
|
+
if (passWriteErr) {
|
|
108
|
+
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
109
|
+
return reject(new Error('Failed to write passphrase file: ' + passWriteErr.message));
|
|
113
110
|
}
|
|
114
|
-
|
|
111
|
+
doConvert(passphraseFile);
|
|
115
112
|
});
|
|
116
113
|
});
|
|
117
114
|
});
|
|
118
115
|
}
|
|
119
116
|
|
|
120
|
-
function
|
|
121
|
-
for (const f of files) {
|
|
122
|
-
try { fs.unlinkSync(f); } catch (e) { /* ignore */ }
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function cleanupAndResolve(err, stdout, stderr, inputFile, outputFile, reject, resolve) {
|
|
127
|
-
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
128
|
-
|
|
129
|
-
if (err) {
|
|
130
|
-
try { fs.unlinkSync(outputFile); } catch (e) { /* ignore */ }
|
|
131
|
-
return reject(new Error(
|
|
132
|
-
'puttygen conversion failed: ' + (stderr || err.message) +
|
|
133
|
-
'. If the PPK file is encrypted, provide the correct passphrase.'
|
|
134
|
-
));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
fs.readFile(outputFile, 'utf8', (readErr, data) => {
|
|
138
|
-
try { fs.unlinkSync(outputFile); } catch (e) { /* ignore */ }
|
|
139
|
-
if (readErr) {
|
|
140
|
-
return reject(new Error('Failed to read converted key: ' + readErr.message));
|
|
141
|
-
}
|
|
142
|
-
resolve(data);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function convertPPKV2ToOpenSSH(ppkContent) {
|
|
117
|
+
function convertPPKV2ToOpenSSH(ppkContent, passphrase) {
|
|
147
118
|
const lines = ppkContent.split('\n');
|
|
148
119
|
|
|
149
120
|
const headers = {};
|
|
150
121
|
const publicLines = [];
|
|
151
122
|
const privateLines = [];
|
|
123
|
+
let macLine = null;
|
|
152
124
|
let section = 'header';
|
|
153
125
|
|
|
154
126
|
for (const rawLine of lines) {
|
|
@@ -165,56 +137,94 @@ function convertPPKV2ToOpenSSH(ppkContent) {
|
|
|
165
137
|
continue;
|
|
166
138
|
}
|
|
167
139
|
if (line.startsWith('Private-MAC:')) {
|
|
168
|
-
|
|
140
|
+
macLine = line.substring('Private-MAC:'.length).trim();
|
|
169
141
|
continue;
|
|
170
142
|
}
|
|
171
143
|
|
|
144
|
+
const colonIdx = line.indexOf(':');
|
|
145
|
+
if (colonIdx !== -1) {
|
|
146
|
+
const key = line.substring(0, colonIdx).trim();
|
|
147
|
+
const value = line.substring(colonIdx + 1).trim();
|
|
148
|
+
if (key.match(/^PuTTY-User-Key-File-/i) ||
|
|
149
|
+
key === 'Encryption' || key === 'Comment' || key === 'Key-Derivation' ||
|
|
150
|
+
key === 'Argon2-Memory' || key === 'Argon2-Passes' ||
|
|
151
|
+
key === 'Argon2-Parallelism' || key === 'Argon2-Salt') {
|
|
152
|
+
headers[key] = value;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
172
157
|
if (section === 'public') {
|
|
173
158
|
publicLines.push(line);
|
|
174
159
|
} else if (section === 'private') {
|
|
175
160
|
privateLines.push(line);
|
|
176
|
-
} else if (section === 'header') {
|
|
177
|
-
const colonIdx = line.indexOf(':');
|
|
178
|
-
if (colonIdx !== -1) {
|
|
179
|
-
headers[line.substring(0, colonIdx).trim()] = line.substring(colonIdx + 1).trim();
|
|
180
|
-
}
|
|
181
161
|
}
|
|
182
162
|
}
|
|
183
163
|
|
|
184
164
|
const encType = headers['Encryption'] || 'none';
|
|
185
|
-
if (encType !== 'none') {
|
|
186
|
-
throw new Error(
|
|
187
|
-
`PPK key is encrypted with "${encType}". Please decrypt it first using PuTTYgen: ` +
|
|
188
|
-
`load the PPK, enter the passphrase, then export as OpenSSH key.`
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
165
|
const ppkVersion = Object.keys(headers).find(k => k.match(/^PuTTY-User-Key-File-/i));
|
|
193
166
|
const keyType = ppkVersion ? headers[ppkVersion] : null;
|
|
194
167
|
if (!keyType) {
|
|
195
168
|
throw new Error('Could not determine PPK key type. Is this a valid PPK file?');
|
|
196
169
|
}
|
|
197
170
|
|
|
198
|
-
if (ppkVersion && ppkVersion.match(/^PuTTY-User-Key-File-
|
|
171
|
+
if (ppkVersion && !ppkVersion.match(/^PuTTY-User-Key-File-2$/i)) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`PPK ${ppkVersion.replace('PuTTY-User-Key-File-', 'v')} format is not supported by the built-in converter. ` +
|
|
174
|
+
`Only PPK v2 is supported. Install puttygen (PuTTY 0.71+) to convert this key.`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const keyDerivation = headers['Key-Derivation'] || '';
|
|
179
|
+
if (keyDerivation && keyDerivation.toLowerCase() !== 'sha1') {
|
|
199
180
|
throw new Error(
|
|
200
|
-
|
|
201
|
-
|
|
181
|
+
`PPK v2 key derivation "${keyDerivation}" is not supported by the built-in converter. ` +
|
|
182
|
+
`Only SHA-1 key derivation is supported. Install puttygen to convert this key.`
|
|
202
183
|
);
|
|
203
184
|
}
|
|
204
185
|
|
|
205
|
-
|
|
206
|
-
|
|
186
|
+
let privateData = Buffer.from(privateLines.join(''), 'base64');
|
|
187
|
+
|
|
188
|
+
if (encType === 'aes256-cbc') {
|
|
189
|
+
if (!passphrase) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
'This PPK key is encrypted. Please enter the passphrase for this key.'
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
privateData = decryptPPKV2Private(privateData, passphrase, encType);
|
|
196
|
+
} catch (e) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
'Failed to decrypt PPK key. The passphrase may be incorrect. ' +
|
|
199
|
+
'Please verify the passphrase and try again.'
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
} else if (encType !== 'none') {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`PPK v2 encryption "${encType}" is not supported by the built-in converter. ` +
|
|
205
|
+
`Supported: none, aes256-cbc. Try installing puttygen for broader format support.`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
207
208
|
|
|
208
209
|
if (privateData.length < 8) {
|
|
209
|
-
throw new Error('PPK private data is too short. The key file may be corrupt.');
|
|
210
|
+
throw new Error('PPK private data is too short. The key file may be corrupt or the passphrase may be wrong.');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (encType !== 'none' && macLine) {
|
|
214
|
+
const publicData = Buffer.from(publicLines.join(''), 'base64');
|
|
215
|
+
if (!verifyPPKV2MAC(headers, publicData, privateData, passphrase, macLine)) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
'The passphrase is incorrect or the key is corrupted. ' +
|
|
218
|
+
'Please verify the passphrase and try again.'
|
|
219
|
+
);
|
|
220
|
+
}
|
|
210
221
|
}
|
|
211
222
|
|
|
212
223
|
const check1 = privateData.readUInt32BE(0);
|
|
213
224
|
const check2 = privateData.readUInt32BE(4);
|
|
214
225
|
if (check1 !== check2) {
|
|
215
226
|
throw new Error(
|
|
216
|
-
'PPK integrity check failed. The key may be
|
|
217
|
-
'If it has a passphrase, convert using PuTTYgen first.'
|
|
227
|
+
'PPK integrity check failed. The key may be corrupted or the passphrase may be wrong.'
|
|
218
228
|
);
|
|
219
229
|
}
|
|
220
230
|
|
|
@@ -271,6 +281,91 @@ function convertPPKV2ToOpenSSH(ppkContent) {
|
|
|
271
281
|
return encodeOpenSSHPrivateKey(keyParts);
|
|
272
282
|
}
|
|
273
283
|
|
|
284
|
+
function decryptPPKV2Private(encData, passphrase, encType) {
|
|
285
|
+
if (encType === 'aes256-cbc') {
|
|
286
|
+
const keyIv = derivePPKV2Key(passphrase, 48);
|
|
287
|
+
const aesKey = keyIv.slice(0, 32);
|
|
288
|
+
const iv = keyIv.slice(32, 48);
|
|
289
|
+
|
|
290
|
+
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
|
|
291
|
+
decipher.setAutoPadding(false);
|
|
292
|
+
let decrypted = decipher.update(encData);
|
|
293
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
294
|
+
|
|
295
|
+
const padLen = decrypted[decrypted.length - 1];
|
|
296
|
+
if (padLen > 0 && padLen <= 16) {
|
|
297
|
+
let valid = true;
|
|
298
|
+
for (let i = 1; i <= padLen; i++) {
|
|
299
|
+
if (decrypted[decrypted.length - i] !== padLen) {
|
|
300
|
+
valid = false;
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (valid) {
|
|
305
|
+
decrypted = decrypted.slice(0, decrypted.length - padLen);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return decrypted;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
throw new Error(`Unsupported PPK v2 encryption type: ${encType}`);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function derivePPKV2Key(passphrase, keyLen) {
|
|
316
|
+
const passBuf = Buffer.from(passphrase, 'utf-8');
|
|
317
|
+
const derived = Buffer.alloc(keyLen);
|
|
318
|
+
|
|
319
|
+
for (let i = 0; i < keyLen; i++) {
|
|
320
|
+
const hash = crypto.createHash('sha1');
|
|
321
|
+
hash.update(passBuf);
|
|
322
|
+
if (i > 0) {
|
|
323
|
+
hash.update(derived.slice(0, i));
|
|
324
|
+
}
|
|
325
|
+
derived[i] = hash.digest()[0];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return derived;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function verifyPPKV2MAC(headers, publicData, privateData, passphrase, macLine) {
|
|
332
|
+
const macKeyBase = crypto.createHash('sha1');
|
|
333
|
+
macKeyBase.update(Buffer.from('putty-private-key-file-mac-key', 'ascii'));
|
|
334
|
+
if (passphrase) {
|
|
335
|
+
macKeyBase.update(Buffer.from(passphrase, 'utf-8'));
|
|
336
|
+
}
|
|
337
|
+
const macKey = macKeyBase.digest();
|
|
338
|
+
|
|
339
|
+
const keyType = Object.keys(headers).find(k => k.match(/^PuTTY-User-Key-File-/i));
|
|
340
|
+
const keyTypeValue = keyType ? headers[keyType] : '';
|
|
341
|
+
const encType = headers['Encryption'] || 'none';
|
|
342
|
+
const comment = headers['Comment'] || '';
|
|
343
|
+
|
|
344
|
+
const macData = Buffer.concat([
|
|
345
|
+
sshString(keyTypeValue),
|
|
346
|
+
sshString(encType),
|
|
347
|
+
sshString(comment),
|
|
348
|
+
sshBuffer(publicData),
|
|
349
|
+
sshBuffer(privateData)
|
|
350
|
+
]);
|
|
351
|
+
|
|
352
|
+
const computed = crypto.createHmac('sha1', macKey).update(macData).digest('hex');
|
|
353
|
+
return computed === macLine.toLowerCase();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function sshString(str) {
|
|
357
|
+
const buf = Buffer.from(str, 'utf-8');
|
|
358
|
+
const len = Buffer.alloc(4);
|
|
359
|
+
len.writeUInt32BE(buf.length);
|
|
360
|
+
return Buffer.concat([len, buf]);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function sshBuffer(data) {
|
|
364
|
+
const len = Buffer.alloc(4);
|
|
365
|
+
len.writeUInt32BE(data.length);
|
|
366
|
+
return Buffer.concat([len, data]);
|
|
367
|
+
}
|
|
368
|
+
|
|
274
369
|
class SSHBufferReader {
|
|
275
370
|
constructor(buffer) {
|
|
276
371
|
this.buffer = buffer;
|
|
@@ -388,39 +483,30 @@ async function convertPPKToOpenSSH(ppkContent, passphrase) {
|
|
|
388
483
|
throw new Error('Not a PPK key');
|
|
389
484
|
}
|
|
390
485
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
);
|
|
486
|
+
// Try built-in converter first (works for v2 encrypted/unencrypted, no puttygen dependency)
|
|
487
|
+
try {
|
|
488
|
+
const result = convertPPKV2ToOpenSSH(ppkContent, passphrase);
|
|
489
|
+
if (result) return result;
|
|
490
|
+
} catch (e) {
|
|
491
|
+
if (!e.message.includes('not supported by the built-in converter')) {
|
|
492
|
+
throw e;
|
|
493
|
+
}
|
|
395
494
|
}
|
|
396
495
|
|
|
397
|
-
//
|
|
496
|
+
// Fall back to puttygen (handles v3 and encryption types the built-in converter doesn't support)
|
|
398
497
|
try {
|
|
399
498
|
const result = await convertPPKViaPuttygen(ppkContent, passphrase);
|
|
400
499
|
if (result && result.includes('BEGIN')) {
|
|
401
500
|
return result;
|
|
402
501
|
}
|
|
403
|
-
} catch (e) {
|
|
404
|
-
// If puttygen failed and key is encrypted, don't try built-in (it can't handle encryption)
|
|
405
|
-
if (info.encrypted) {
|
|
406
|
-
throw new Error(
|
|
407
|
-
'Could not convert encrypted PPK key: ' + e.message +
|
|
408
|
-
'. Ensure puttygen is installed and the passphrase is correct.'
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
// puttygen not available, try built-in v2 parser for unencrypted keys
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// Fall back to built-in PPK v2 converter (unencrypted only)
|
|
415
|
-
try {
|
|
416
|
-
return convertPPKV2ToOpenSSH(ppkContent);
|
|
417
502
|
} catch (e) {
|
|
418
503
|
throw new Error(
|
|
419
504
|
'Could not convert PPK key: ' + e.message +
|
|
420
|
-
'.
|
|
421
|
-
'load the PPK file, then export as OpenSSH key (Conversions > Export OpenSSH key).'
|
|
505
|
+
'. If puttygen is not available, ensure the key uses PPK v2 format with AES-256-CBC encryption (or no encryption).'
|
|
422
506
|
);
|
|
423
507
|
}
|
|
508
|
+
|
|
509
|
+
throw new Error('Failed to convert PPK key: no conversion method available.');
|
|
424
510
|
}
|
|
425
511
|
|
|
426
512
|
async function convertKeyIfNeeded(keyContent, passphrase) {
|