Skip to content

Add parameter disableHostkeyVerification #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/src/ssh_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ class SSHChannelController {
if (_done.isCompleted) return;
if (_hasSentClose) return;
_hasSentClose = true;
sendMessage(SSH_Message_Channel_Close(recipientChannel: remoteId));

try {
sendMessage(SSH_Message_Channel_Close(recipientChannel: remoteId));
} catch (e) {
printDebug?.call('SSHChannelController._sendCloseIfNeeded - error: $e');
}
}

void _sendRequestSuccess() {
Expand Down Expand Up @@ -455,8 +460,7 @@ class SSHChannelExtendedDataType {
static const stderr = 1;
}

class SSHChannelDataSplitter
extends StreamTransformerBase<SSHChannelData, SSHChannelData> {
class SSHChannelDataSplitter extends StreamTransformerBase<SSHChannelData, SSHChannelData> {
SSHChannelDataSplitter(this.maxSize);

final int maxSize;
Expand Down
12 changes: 11 additions & 1 deletion lib/src/ssh_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class SSHClient {
/// extension. May not be called if the server does not support the extension.
// final SSHHostKeysHandler? onHostKeys;

/// Allow to disable hostkey verification, which can be slow in debug mode.
final bool disableHostkeyVerification;

/// A [Future] that completes when the transport is closed, or when an error
/// occurs. After this [Future] completes, [isClosed] will be true and no more
/// data can be sent or received.
Expand All @@ -152,6 +155,7 @@ class SSHClient {
this.onUserauthBanner,
this.onAuthenticated,
this.keepAliveInterval = const Duration(seconds: 10),
this.disableHostkeyVerification = false,
}) {
_transport = SSHTransport(
socket,
Expand All @@ -162,6 +166,7 @@ class SSHClient {
onVerifyHostKey: onVerifyHostKey,
onReady: _handleTransportReady,
onPacket: _handlePacket,
disableHostkeyVerification: disableHostkeyVerification,
);

_transport.done.then(
Expand Down Expand Up @@ -476,7 +481,12 @@ class SSHClient {
);
}
_keepAlive?.stop();
_closeChannels();

try {
_closeChannels();
} catch (e) {
printDebug?.call("SSHClient::_handleTransportClosed - error: $e");
}
}

void _handlePacket(Uint8List payload) {
Expand Down
18 changes: 12 additions & 6 deletions lib/src/ssh_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class SSHTransport {
/// Function called when a packet is received.
final SSHPacketHandler? onPacket;

final bool disableHostkeyVerification;

/// A [Future] that completes when the transport is closed, or when an error
/// occurs. After this [Future] completes, [isClosed] will be true and no
/// more data can be sent or received.
Expand All @@ -94,6 +96,7 @@ class SSHTransport {
this.onVerifyHostKey,
this.onReady,
this.onPacket,
this.disableHostkeyVerification = false,
}) {
_initSocket();
_startHandshake();
Expand Down Expand Up @@ -803,12 +806,15 @@ class SSHTransport {
sharedSecret: sharedSecret,
);

final verified = _verifyHostkey(
keyBytes: hostkey,
signatureBytes: hostSignature,
exchangeHash: exchangeHash,
);
if (!verified) throw SSHHostkeyError('Signature verification failed');
if (!disableHostkeyVerification)
{
final verified = _verifyHostkey(
keyBytes: hostkey,
signatureBytes: hostSignature,
exchangeHash: exchangeHash,
);
if (!verified) throw SSHHostkeyError('Signature verification failed');
}

_exchangeHash = exchangeHash;
_sessionId ??= exchangeHash;
Expand Down