@lethevimlet/sshift 1.1.8 → 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,77 +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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const doConvert = (passphraseFile) => {
|
|
61
|
+
const args = [inputFile, '-O', 'private-openssh-new', '-o', outputFile];
|
|
62
|
+
if (passphraseFile) {
|
|
63
|
+
args.push('--old-passphrase', passphraseFile);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
const args = passphrase
|
|
67
|
-
? [inputFile, '--old-passphrase=' + passphrase, '-O', 'private-openssh-new', '-o', outputFile]
|
|
68
|
-
: [inputFile, '-O', 'private-openssh-new', '-o', outputFile];
|
|
69
|
-
|
|
70
66
|
execFile('puttygen', args, (err, stdout, stderr) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
+
));
|
|
76
80
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
cleanupAndResolve(err2, stdout2, stderr2, inputFile, outputFile, reject, resolve);
|
|
80
|
-
});
|
|
81
|
-
return;
|
|
81
|
+
|
|
82
|
+
return reject(new Error('puttygen conversion failed: ' + errMsg));
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
|
|
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
|
+
});
|
|
85
96
|
});
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function cleanupAndResolve(err, stdout, stderr, inputFile, outputFile, reject, resolve) {
|
|
91
|
-
try { fs.unlinkSync(inputFile); } catch (e) { /* ignore */ }
|
|
97
|
+
};
|
|
92
98
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (stderrStr.includes('no such option') && stderrStr.includes('--old-passphrase')) {
|
|
99
|
-
return reject(new Error(
|
|
100
|
-
'puttygen on this system does not support the --old-passphrase option. ' +
|
|
101
|
-
'Please upgrade puttygen (PuTTY 0.71+) or decrypt the PPK key manually: ' +
|
|
102
|
-
'run "puttygen key.ppk -O private-openssh-new -o key.pem" with the passphrase entered interactively.'
|
|
103
|
-
));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (stderrStr.toLowerCase().includes('wrong passphrase') || stderrStr.toLowerCase().includes('unable to read')) {
|
|
107
|
-
return reject(new Error(
|
|
108
|
-
'PPK passphrase is incorrect. Please check the passphrase and try again. ' +
|
|
109
|
-
'(puttygen error: ' + stderrStr + ')'
|
|
110
|
-
));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return reject(new Error('puttygen conversion failed: ' + errMsg));
|
|
114
|
-
}
|
|
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));
|
|
103
|
+
}
|
|
115
104
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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));
|
|
110
|
+
}
|
|
111
|
+
doConvert(passphraseFile);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
122
114
|
});
|
|
123
115
|
}
|
|
124
116
|
|
|
125
|
-
function convertPPKV2ToOpenSSH(ppkContent) {
|
|
117
|
+
function convertPPKV2ToOpenSSH(ppkContent, passphrase) {
|
|
126
118
|
const lines = ppkContent.split('\n');
|
|
127
119
|
|
|
128
120
|
const headers = {};
|
|
129
121
|
const publicLines = [];
|
|
130
122
|
const privateLines = [];
|
|
123
|
+
let macLine = null;
|
|
131
124
|
let section = 'header';
|
|
132
125
|
|
|
133
126
|
for (const rawLine of lines) {
|
|
@@ -144,56 +137,94 @@ function convertPPKV2ToOpenSSH(ppkContent) {
|
|
|
144
137
|
continue;
|
|
145
138
|
}
|
|
146
139
|
if (line.startsWith('Private-MAC:')) {
|
|
147
|
-
|
|
140
|
+
macLine = line.substring('Private-MAC:'.length).trim();
|
|
148
141
|
continue;
|
|
149
142
|
}
|
|
150
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
|
+
|
|
151
157
|
if (section === 'public') {
|
|
152
158
|
publicLines.push(line);
|
|
153
159
|
} else if (section === 'private') {
|
|
154
160
|
privateLines.push(line);
|
|
155
|
-
} else if (section === 'header') {
|
|
156
|
-
const colonIdx = line.indexOf(':');
|
|
157
|
-
if (colonIdx !== -1) {
|
|
158
|
-
headers[line.substring(0, colonIdx).trim()] = line.substring(colonIdx + 1).trim();
|
|
159
|
-
}
|
|
160
161
|
}
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
const encType = headers['Encryption'] || 'none';
|
|
164
|
-
if (encType !== 'none') {
|
|
165
|
-
throw new Error(
|
|
166
|
-
`PPK key is encrypted with "${encType}". Please decrypt it first using PuTTYgen: ` +
|
|
167
|
-
`load the PPK, enter the passphrase, then export as OpenSSH key.`
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
165
|
const ppkVersion = Object.keys(headers).find(k => k.match(/^PuTTY-User-Key-File-/i));
|
|
172
166
|
const keyType = ppkVersion ? headers[ppkVersion] : null;
|
|
173
167
|
if (!keyType) {
|
|
174
168
|
throw new Error('Could not determine PPK key type. Is this a valid PPK file?');
|
|
175
169
|
}
|
|
176
170
|
|
|
177
|
-
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') {
|
|
178
180
|
throw new Error(
|
|
179
|
-
|
|
180
|
-
|
|
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.`
|
|
181
183
|
);
|
|
182
184
|
}
|
|
183
185
|
|
|
184
|
-
|
|
185
|
-
|
|
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
|
+
}
|
|
186
208
|
|
|
187
209
|
if (privateData.length < 8) {
|
|
188
|
-
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
|
+
}
|
|
189
221
|
}
|
|
190
222
|
|
|
191
223
|
const check1 = privateData.readUInt32BE(0);
|
|
192
224
|
const check2 = privateData.readUInt32BE(4);
|
|
193
225
|
if (check1 !== check2) {
|
|
194
226
|
throw new Error(
|
|
195
|
-
'PPK integrity check failed. The key may be
|
|
196
|
-
'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.'
|
|
197
228
|
);
|
|
198
229
|
}
|
|
199
230
|
|
|
@@ -250,6 +281,91 @@ function convertPPKV2ToOpenSSH(ppkContent) {
|
|
|
250
281
|
return encodeOpenSSHPrivateKey(keyParts);
|
|
251
282
|
}
|
|
252
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
|
+
|
|
253
369
|
class SSHBufferReader {
|
|
254
370
|
constructor(buffer) {
|
|
255
371
|
this.buffer = buffer;
|
|
@@ -367,39 +483,30 @@ async function convertPPKToOpenSSH(ppkContent, passphrase) {
|
|
|
367
483
|
throw new Error('Not a PPK key');
|
|
368
484
|
}
|
|
369
485
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
);
|
|
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
|
+
}
|
|
374
494
|
}
|
|
375
495
|
|
|
376
|
-
//
|
|
496
|
+
// Fall back to puttygen (handles v3 and encryption types the built-in converter doesn't support)
|
|
377
497
|
try {
|
|
378
498
|
const result = await convertPPKViaPuttygen(ppkContent, passphrase);
|
|
379
499
|
if (result && result.includes('BEGIN')) {
|
|
380
500
|
return result;
|
|
381
501
|
}
|
|
382
|
-
} catch (e) {
|
|
383
|
-
// If puttygen failed and key is encrypted, don't try built-in (it can't handle encryption)
|
|
384
|
-
if (info.encrypted) {
|
|
385
|
-
throw new Error(
|
|
386
|
-
'Could not convert encrypted PPK key: ' + e.message +
|
|
387
|
-
'. Ensure puttygen is installed and the passphrase is correct.'
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
// puttygen not available, try built-in v2 parser for unencrypted keys
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// Fall back to built-in PPK v2 converter (unencrypted only)
|
|
394
|
-
try {
|
|
395
|
-
return convertPPKV2ToOpenSSH(ppkContent);
|
|
396
502
|
} catch (e) {
|
|
397
503
|
throw new Error(
|
|
398
504
|
'Could not convert PPK key: ' + e.message +
|
|
399
|
-
'.
|
|
400
|
-
'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).'
|
|
401
506
|
);
|
|
402
507
|
}
|
|
508
|
+
|
|
509
|
+
throw new Error('Failed to convert PPK key: no conversion method available.');
|
|
403
510
|
}
|
|
404
511
|
|
|
405
512
|
async function convertKeyIfNeeded(keyContent, passphrase) {
|
package/src/webapp/js/app.js
CHANGED
|
@@ -5858,7 +5858,7 @@ const wheelHandler = (e) => {
|
|
|
5858
5858
|
|
|
5859
5859
|
const detectResult = await this.detectAndConvertKey(content, passphrase);
|
|
5860
5860
|
if (detectResult.error) {
|
|
5861
|
-
if (this._isPassphraseError(detectResult.error)) {
|
|
5861
|
+
if (this._isPassphraseError(detectResult.error) && !passphrase) {
|
|
5862
5862
|
const promptedPassphrase = await this._promptForKeyPassphrase();
|
|
5863
5863
|
if (promptedPassphrase !== null) {
|
|
5864
5864
|
passphrase = promptedPassphrase;
|