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

Method authPK

lib/protocol/Protocol.js:631–749  ·  view source on GitHub ↗
(username, pubKey, keyAlgo, cbSign)

Source from the content-addressed store, hash-verified

629 sendPacket(this, this._packetRW.write.finalize(packet));
630 }
631 authPK(username, pubKey, keyAlgo, cbSign) {
632 if (this._server)
633 throw new Error('Client-only method called in server mode');
634
635 pubKey = parseKey(pubKey);
636 if (pubKey instanceof Error)
637 throw new Error('Invalid key');
638
639 const keyType = pubKey.type;
640 pubKey = pubKey.getPublicSSH();
641
642 if (typeof keyAlgo === 'function') {
643 cbSign = keyAlgo;
644 keyAlgo = undefined;
645 }
646 if (!keyAlgo)
647 keyAlgo = keyType;
648
649 const userLen = Buffer.byteLength(username);
650 const algoLen = Buffer.byteLength(keyAlgo);
651 const pubKeyLen = pubKey.length;
652 const sessionID = this._kex.sessionID;
653 const sesLen = sessionID.length;
654 const payloadLen =
655 (cbSign ? 4 + sesLen : 0)
656 + 1 + 4 + userLen + 4 + 14 + 4 + 9 + 1 + 4 + algoLen + 4 + pubKeyLen;
657 let packet;
658 let p;
659 if (cbSign) {
660 packet = Buffer.allocUnsafe(payloadLen);
661 p = 0;
662 writeUInt32BE(packet, sesLen, p);
663 packet.set(sessionID, p += 4);
664 p += sesLen;
665 } else {
666 packet = this._packetRW.write.alloc(payloadLen);
667 p = this._packetRW.write.allocStart;
668 }
669
670 packet[p] = MESSAGE.USERAUTH_REQUEST;
671
672 writeUInt32BE(packet, userLen, ++p);
673 packet.utf8Write(username, p += 4, userLen);
674
675 writeUInt32BE(packet, 14, p += userLen);
676 packet.utf8Write('ssh-connection', p += 4, 14);
677
678 writeUInt32BE(packet, 9, p += 14);
679 packet.utf8Write('publickey', p += 4, 9);
680
681 packet[p += 9] = (cbSign ? 1 : 0);
682
683 writeUInt32BE(packet, algoLen, ++p);
684 packet.utf8Write(keyAlgo, p += 4, algoLen);
685
686 writeUInt32BE(packet, pubKeyLen, p += algoLen);
687 packet.set(pubKey, p += 4);
688

Callers 3

connectMethod · 0.80
doNextAuthMethod · 0.80
tryNextAgentKeyMethod · 0.80

Calls 5

parseKeyFunction · 0.85
writeUInt32BEFunction · 0.85
pushMethod · 0.80
allocMethod · 0.45
finalizeMethod · 0.45

Tested by

no test coverage detected