MCPcopy Create free account
hub / github.com/mscdex/ssh2 / parseKey

Function parseKey

lib/protocol/keyParser.js:1416–1477  ·  view source on GitHub ↗
(data, passphrase)

Source from the content-addressed store, hash-verified

1414}
1415
1416function parseKey(data, passphrase) {
1417 if (isParsedKey(data))
1418 return data;
1419
1420 let origBuffer;
1421 if (Buffer.isBuffer(data)) {
1422 origBuffer = data;
1423 data = data.utf8Slice(0, data.length).trim();
1424 } else if (typeof data === 'string') {
1425 data = data.trim();
1426 } else {
1427 return new Error('Key data must be a Buffer or string');
1428 }
1429
1430 // eslint-disable-next-line eqeqeq
1431 if (passphrase != undefined) {
1432 if (typeof passphrase === 'string')
1433 passphrase = Buffer.from(passphrase);
1434 else if (!Buffer.isBuffer(passphrase))
1435 return new Error('Passphrase must be a string or Buffer when supplied');
1436 }
1437
1438 let ret;
1439
1440 // First try as printable string format (e.g. PEM)
1441
1442 // Private keys
1443 if ((ret = OpenSSH_Private.parse(data, passphrase)) !== null)
1444 return ret;
1445 if ((ret = OpenSSH_Old_Private.parse(data, passphrase)) !== null)
1446 return ret;
1447 if ((ret = PPK_Private.parse(data, passphrase)) !== null)
1448 return ret;
1449
1450 // Public keys
1451 if ((ret = OpenSSH_Public.parse(data)) !== null)
1452 return ret;
1453 if ((ret = RFC4716_Public.parse(data)) !== null)
1454 return ret;
1455
1456 // Finally try as a binary format if we were originally passed binary data
1457 if (origBuffer) {
1458 binaryKeyParser.init(origBuffer, 0);
1459 const type = binaryKeyParser.readString(true);
1460 if (type !== undefined) {
1461 data = binaryKeyParser.readRaw();
1462 if (data !== undefined) {
1463 ret = parseDER(data, type, '', type);
1464 // Ignore potentially useless errors in case the data was not actually
1465 // in the binary format
1466 if (ret instanceof Error)
1467 ret = null;
1468 }
1469 }
1470 binaryKeyParser.clear();
1471 }
1472
1473 if (ret)

Callers 15

connectMethod · 0.85
doNextAuthMethod · 0.85
hostKeysProveFunction · 0.85
constructorMethod · 0.85
_writeMethod · 0.85
signMethod · 0.85
getIdentitiesReplyMethod · 0.85
initMethod · 0.85
handlers.misc.jsFile · 0.85
authPKMethod · 0.85
authHostbasedMethod · 0.85
keyParser.jsFile · 0.85

Calls 4

isParsedKeyFunction · 0.85
parseDERFunction · 0.85
initMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…