diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt index f0a4f2899..51e115dbe 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/SshConnection.kt @@ -172,6 +172,7 @@ import java.nio.ByteBuffer import java.security.SecureRandom import java.util.Collections import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicLong import kotlin.coroutines.CoroutineContext import org.connectbot.sshlib.AuthResult as PublicAuthResult @@ -343,9 +344,9 @@ class SshConnection( override suspend fun sendKexInit() = this@SshConnection.sendKexInit() override fun receiveKexInit(msg: SshMsgKexinit, initialExchange: Boolean) = this@SshConnection.receiveKexInit(msg, initialExchange) override suspend fun sendKexExchangeInit() = this@SshConnection.sendKexExchangeInit() - override suspend fun receiveKexDhReply(msg: SshMsgKexdhReply) = this@SshConnection.receiveKexDhReply(msg) - override suspend fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) = this@SshConnection.receiveKexEcdhReply(msg) - override suspend fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) = this@SshConnection.receiveKexDhGexReply(msg) + override fun receiveKexDhReply(msg: SshMsgKexdhReply) = this@SshConnection.receiveKexDhReply(msg) + override fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) = this@SshConnection.receiveKexEcdhReply(msg) + override fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) = this@SshConnection.receiveKexDhGexReply(msg) override fun isRekeying(): Boolean = this@SshConnection.isRekeying override fun isAuthenticationRequestPending(): Boolean = this@SshConnection.authRequestPending override fun authenticationRequestStarted() { @@ -419,8 +420,7 @@ class SshConnection( private var serverExtInfoReceivedCount: Int = 0 private var clientExtInfoSent: Boolean = false - private var nextLocalChannelNumber = 0 - private val channelNumberLock = Mutex() + private val nextLocalChannelNumber = AtomicInteger() private val channelRegistry = SshChannelRegistry() private var agentProvider: AgentProvider? = null @@ -597,7 +597,7 @@ class SshConnection( setData(createByteString(byteArrayOf())) _check() } - writePacket(SshEnums.MessageType.SSH_MSG_IGNORE.id().toInt(), msg.toByteArray()) + writePacket(SshEnums.MessageType.SSH_MSG_IGNORE.id().toInt(), msg.toByteArray(4)) } /** Each connection owns its heartbeat, which is retired with its transport. */ @@ -1529,7 +1529,7 @@ class SshConnection( ) } - private suspend fun receiveKexDhReply(msg: SshMsgKexdhReply) { + private fun receiveKexDhReply(msg: SshMsgKexdhReply) { logger.info("Received DH_REPLY from server") completeKex( serverHostKey = msg.serverKey().data(), @@ -1538,7 +1538,7 @@ class SshConnection( ) } - private suspend fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) { + private fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) { logger.info("Received ECDH_REPLY from server") try { completeKex( @@ -1552,7 +1552,7 @@ class SshConnection( } } - private suspend fun completeKex(serverHostKey: ByteArray, serverPublicKey: ByteArray, signature: ByteArray) { + private fun completeKex(serverHostKey: ByteArray, serverPublicKey: ByteArray, signature: ByteArray) { val kexAlg = kex ?: throw SshException(ERROR_NO_KEX_ALGORITHM_INITIALIZED) val sv = serverVersion ?: throw SshException("Server version not received") val cki = clientKexInit ?: throw SshException("Client KEX_INIT not sent") @@ -1630,7 +1630,7 @@ class SshConnection( } } - private suspend fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) { + private fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) { logger.info("Received DH_GEX_REPLY from server") completeKex( serverHostKey = msg.serverPublicHostKey().data(), @@ -2321,13 +2321,13 @@ class SshConnection( setRecipientChannel(recipientChannel.toLong()) _check() } - writePacket(SshEnums.MessageType.SSH_MSG_CHANNEL_SUCCESS.id().toInt(), reply.toByteArray()) + writePacket(SshEnums.MessageType.SSH_MSG_CHANNEL_SUCCESS.id().toInt(), reply.toByteArray(4)) } else { val reply = SshMsgChannelFailure().apply { setRecipientChannel(recipientChannel.toLong()) _check() } - writePacket(SshEnums.MessageType.SSH_MSG_CHANNEL_FAILURE.id().toInt(), reply.toByteArray()) + writePacket(SshEnums.MessageType.SSH_MSG_CHANNEL_FAILURE.id().toInt(), reply.toByteArray(4)) } } @@ -2426,11 +2426,7 @@ class SshConnection( // Port forwarding support - internal suspend fun allocateChannelNumber(): Int { - channelNumberLock.withLock { - return nextLocalChannelNumber++ - } - } + internal fun allocateChannelNumber(): Int = nextLocalChannelNumber.getAndIncrement() internal suspend fun openDirectTcpipChannel( host: String, @@ -2882,7 +2878,7 @@ class SshConnection( } SshEnums.MessageType.SSH_MSG_SERVICE_ACCEPT -> { - val msg = parseBody(packet, ::SshMsgServiceAccept) + val msg = packet.body() as SshMsgServiceAccept requireAccepted(stateMachine.receiveServiceAccept(msg.serviceName().value()), msgType) } @@ -2895,7 +2891,7 @@ class SshConnection( } SshEnums.MessageType.SSH_MSG_UNIMPLEMENTED -> { - logger.debug("Peer reported packet ${parseBody(packet, ::SshMsgUnimplemented).packetSequence()} as unimplemented") + logger.debug("Peer reported packet {} as unimplemented", (packet.body() as SshMsgUnimplemented).packetSequence()) } SshEnums.MessageType.SSH_MSG_GLOBAL_REQUEST -> { @@ -2993,14 +2989,14 @@ class SshConnection( } SshEnums.MessageType.SSH_MSG_DISCONNECT -> { - val msg = parseBody(packet, ::SshMsgDisconnect) + val msg = packet.body() as SshMsgDisconnect logger.info("Received SSH_MSG_DISCONNECT from server: reason=${msg.reasonCode()}, description=${msg.description().value()}") requireAccepted(stateMachine.disconnect(), msgType) } SshEnums.MessageType.SSH_MSG_EXT_INFO -> { requireAccepted(stateMachine.authorizeExtInfo(), msgType) - processServerExtInfo(parseBody(packet, ::SshMsgExtInfo)) + processServerExtInfo(packet.body() as SshMsgExtInfo) } SshEnums.MessageType.SSH_MSG_PING -> { @@ -3010,7 +3006,7 @@ class SshConnection( val pong = SshMsgPong() pong.setData(createByteString(msg.data().data())) pong._check() - writePacket(SshEnums.MessageType.SSH_MSG_PONG.id().toInt(), pong.toByteArray()) + writePacket(SshEnums.MessageType.SSH_MSG_PONG.id().toInt(), pong.toByteArray(4L + pong.data().lenData())) } pongSend() } @@ -3163,7 +3159,7 @@ class SshConnection( val ping = SshMsgPing() ping.setData(createByteString(payload)) ping._check() - writePacket(SshEnums.MessageType.SSH_MSG_PING.id().toInt(), ping.toByteArray()) + writePacket(SshEnums.MessageType.SSH_MSG_PING.id().toInt(), ping.toByteArray(4L + ping.data().lenData())) } internal suspend fun sendWindowAdjust(recipientChannel: Int, bytesToAdd: Int) { @@ -3175,7 +3171,7 @@ class SshConnection( writePacket( SshEnums.MessageType.SSH_MSG_CHANNEL_WINDOW_ADJUST.id().toInt(), - msg.toByteArray(), + msg.toByteArray(8), ) } @@ -3187,7 +3183,7 @@ class SshConnection( writePacket( SshEnums.MessageType.SSH_MSG_CHANNEL_EOF.id().toInt(), - msg.toByteArray(), + msg.toByteArray(4), ) } @@ -3265,7 +3261,7 @@ class SshConnection( } writePacket( SshEnums.MessageType.SSH_MSG_UNIMPLEMENTED.id().toInt(), - msg.toByteArray(), + msg.toByteArray(4), ) } @@ -3521,7 +3517,7 @@ class SshConnection( writePacket( SshEnums.MessageType.SSH_MSG_CHANNEL_CLOSE.id().toInt(), - msg.toByteArray(), + msg.toByteArray(4), ) } @@ -3541,7 +3537,7 @@ class SshConnection( outboundPacketController.writePacket( SshEnums.MessageType.SSH_MSG_PING.id().toInt(), - ping.toByteArray(), + ping.toByteArray(4L + ping.data().lenData()), beforeWrite = { pendingPings[seq] = current.copy(sentTimeNs = System.nanoTime()) }, diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpClientImpl.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpClientImpl.kt index aaceb9c56..e89e4902d 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpClientImpl.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpClientImpl.kt @@ -35,7 +35,6 @@ import org.connectbot.sshlib.Timeouts import org.connectbot.sshlib.client.asReadOnlyBuffer import org.connectbot.sshlib.kaitaiParseFailureOrNull import org.connectbot.sshlib.protocol.ByteString -import org.connectbot.sshlib.protocol.SftpAcceptedTransition import org.connectbot.sshlib.protocol.SftpCopyData import org.connectbot.sshlib.protocol.SftpState import org.connectbot.sshlib.protocol.SftpStateMachine @@ -47,7 +46,7 @@ import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.concurrent.atomic.AtomicBoolean -private typealias SftpTransition = suspend (suspend (SftpAcceptedTransition) -> Unit) -> Boolean +private typealias SftpTransition = suspend (() -> Unit) -> Boolean /** * Internal implementation of [SftpClient]. @@ -64,7 +63,6 @@ internal class SftpClientImpl private constructor( private val requestTimeoutMs: Long, ) : SftpClient { - private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val closed = AtomicBoolean() override val isOpen: Boolean get() = !closed.get() && session.isOpen && stateMachine.state == SftpState.READY @@ -384,7 +382,7 @@ internal class SftpClientImpl private constructor( transition: SftpTransition = stateMachine::request, timeoutMs: Long = requestTimeoutMs, map: (SftpRawPacket) -> SftpResult, - ): SftpResult = when (val result = dispatcher.request(type, payload, timeoutMs) { action -> transition { action() } }) { + ): SftpResult = when (val result = dispatcher.request(type, payload, timeoutMs, transition)) { is SftpResult.Success -> try { map(result.value) } catch (e: SftpDecodeException) { @@ -492,7 +490,7 @@ internal class SftpClientImpl private constructor( // Send SSH_FXP_INIT val initPayload = ByteBuffer.allocate(4) initPayload.putInt(SFTP_VERSION) - val initResult = if (stateMachine.sendInit {}) { + val initResult = if (stateMachine.sendInit()) { dispatcher.writeRaw(SSH_FXP_INIT, initPayload.array()) } else { return SftpResult.ProtocolError("Failed to admit SFTP INIT") diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcher.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcher.kt index be65eabd6..f65b812c1 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcher.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcher.kt @@ -66,21 +66,21 @@ internal class SftpDispatcher( } /** - * Admit and write a request as one state-machine action, then await its response - * after the state-machine serialization point has been released. + * Register the response waiter inside the state-machine action. Queue the write + * and await its response after releasing the lifecycle lock. */ suspend fun request( type: Int, payload: ByteArray, timeoutMs: Long = Timeouts.DEFAULT_SFTP_REQUEST_TIMEOUT_MS, - authorize: suspend (suspend () -> Unit) -> Boolean, + authorize: suspend (() -> Unit) -> Boolean, ): SftpResult = request(type, listOf(payload), timeoutMs, authorize) suspend fun request( type: Int, payload: List, timeoutMs: Long = Timeouts.DEFAULT_SFTP_REQUEST_TIMEOUT_MS, - authorize: suspend (suspend () -> Unit) -> Boolean, + authorize: suspend (() -> Unit) -> Boolean, ): SftpResult { var requestId = 0 var deferred: CompletableDeferred? = null @@ -162,7 +162,7 @@ internal class SftpDispatcher( suspend fun readRaw(): SftpResult { val result = packetIO.readPacket() if (result is SftpResult.Success && result.value.type == SSH_FXP_VERSION) { - stateMachine.receiveVersion { } + stateMachine.receiveVersion() } return result } @@ -209,11 +209,11 @@ internal class SftpDispatcher( // Notify state machine directly of incoming packet when (packet.type) { - SSH_FXP_STATUS -> stateMachine.receiveStatus { } - SSH_FXP_HANDLE -> stateMachine.receiveHandle { } - SSH_FXP_DATA -> stateMachine.receiveData { } - SSH_FXP_NAME -> stateMachine.receiveName { } - SSH_FXP_ATTRS -> stateMachine.receiveAttrs { } + SSH_FXP_STATUS -> stateMachine.receiveStatus() + SSH_FXP_HANDLE -> stateMachine.receiveHandle() + SSH_FXP_DATA -> stateMachine.receiveData() + SSH_FXP_NAME -> stateMachine.receiveName() + SSH_FXP_ATTRS -> stateMachine.receiveAttrs() } val header = SftpFrameHeader.ResponseHeader(ByteBufferKaitaiStream(responsePayload)) diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/AesGcmCipher.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/AesGcmCipher.kt index 39357cf54..83052b856 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/AesGcmCipher.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/AesGcmCipher.kt @@ -81,28 +81,21 @@ internal class AesGcmCipher( cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmSpec) cipher.updateAAD(packetLength) val output = ByteArray(plaintext.sumOf { it.size } + tagLength) - var written = 0 - for (index in 0 until plaintext.lastIndex) { - val part = plaintext[index] - written += cipher.update(part, 0, part.size, output, written) - } - if (plaintext.isEmpty()) { - cipher.doFinal(output, written) - } else { - val last = plaintext.last() - cipher.doFinal(last, 0, last.size, output, written) - } + cipher.encryptParts(plaintext, output) invocationCounter++ return output } override fun decrypt(packetLength: ByteArray, ciphertext: ByteArray, tag: ByteArray): ByteArray = decryptPacket(packetLength, ciphertext + tag) - override fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray): ByteArray { - initDecrypt(packetLength, encrypted) + override fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray, size: Int): ByteArray { + if (size !in tagLength..encrypted.size) throw TransportException("Truncated AEAD packet") + val gcmSpec = GCMParameterSpec(128, buildNonce()) + cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmSpec) + cipher.updateAAD(packetLength) try { - val plaintext = cipher.doFinal(encrypted) + val plaintext = cipher.doFinal(encrypted, 0, size) invocationCounter++ return plaintext } catch (e: AEADBadTagException) { @@ -110,13 +103,6 @@ internal class AesGcmCipher( } } - private fun initDecrypt(packetLength: ByteArray, encrypted: ByteArray) { - if (encrypted.size < tagLength) throw TransportException("Truncated AEAD packet") - val gcmSpec = GCMParameterSpec(128, buildNonce()) - cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmSpec) - cipher.updateAAD(packetLength) - } - override fun destroy() { key.fill(0) iv.fill(0) diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305Cipher.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305Cipher.kt index 25a3b73d8..cc2596573 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305Cipher.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305Cipher.kt @@ -94,9 +94,16 @@ internal class ChaCha20Poly1305Cipher(private val key: ByteArray) : PacketAead { } override fun encrypt(packetLength: ByteArray, plaintext: ByteArray): AeadResult { - // Note: nonce already set by encryptLength() for the same sequence number - val ciphertext = ByteArray(plaintext.size) - val tag = ByteArray(TAG_SIZE) + val output = encryptPacket(packetLength, listOf(plaintext)) + return AeadResult(output.copyOfRange(0, plaintext.size), output.copyOfRange(plaintext.size, output.size)) + } + + override fun encryptPacket(packetLength: ByteArray, plaintext: ByteArray): ByteArray = encryptPacket(packetLength, listOf(plaintext)) + + override fun encryptPacket(packetLength: ByteArray, plaintext: List): ByteArray { + // Nonce already set by encryptLength() for the same sequence number. + val size = plaintext.sumOf { it.size } + val output = ByteArray(size + TAG_SIZE) if (ChaCha20ParamFactory.usesChaCha20ParameterSpec()) { val polyKeyParams = ChaCha20ParamFactory.create(nonce, 0) @@ -105,30 +112,32 @@ internal class ChaCha20Poly1305Cipher(private val key: ByteArray) : PacketAead { val payloadParams = ChaCha20ParamFactory.create(nonce, 1) payloadCipher.init(Cipher.ENCRYPT_MODE, mainKeySpec, payloadParams) - payloadCipher.doFinal(plaintext, 0, plaintext.size, ciphertext, 0) } else { val params = ChaCha20ParamFactory.create(nonce, 0) payloadCipher.init(Cipher.ENCRYPT_MODE, mainKeySpec, params) payloadCipher.update(polyKeyZeros, 0, 32, polyKey, 0) payloadCipher.update(skipToNextBlock, 0, 32, skipToNextBlock, 0) - payloadCipher.doFinal(plaintext, 0, plaintext.size, ciphertext, 0) } + payloadCipher.encryptParts(plaintext, output) poly.init(polyKey) poly.update(packetLength, 0, 4) - poly.update(ciphertext, 0, ciphertext.size) - poly.finish(tag, 0) + poly.update(output, 0, size) + poly.finish(output, size) polyKey.fill(0) - return AeadResult(ciphertext, tag) + return output } - override fun decrypt(packetLength: ByteArray, ciphertext: ByteArray, tag: ByteArray): ByteArray = decryptBody(packetLength, ciphertext, ciphertext.size, tag, 0) + override fun decrypt(packetLength: ByteArray, ciphertext: ByteArray, tag: ByteArray): ByteArray { + if (tag.size != tagLength) throw TransportException("Invalid ChaCha20-Poly1305 tag length") + return decryptBody(packetLength, ciphertext, ciphertext.size, tag, 0) + } - override fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray): ByteArray { - if (encrypted.size < tagLength) throw TransportException("Truncated ChaCha20-Poly1305 packet") - return decryptBody(packetLength, encrypted, encrypted.size - tagLength, encrypted, encrypted.size - tagLength) + override fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray, size: Int): ByteArray { + if (size !in tagLength..encrypted.size) throw TransportException("Truncated ChaCha20-Poly1305 packet") + return decryptBody(packetLength, encrypted, size - tagLength, encrypted, size - tagLength) } private fun decryptBody(packetLength: ByteArray, ciphertext: ByteArray, ciphertextLength: Int, tag: ByteArray, tagOffset: Int): ByteArray { @@ -176,7 +185,6 @@ internal class ChaCha20Poly1305Cipher(private val key: ByteArray) : PacketAead { } private fun constantTimeEquals(a: ByteArray, b: ByteArray, offset: Int = 0): Boolean { - if (a.size - offset != b.size) return false var result = 0 for (i in b.indices) { result = result or (a[offset + i].toInt() xor b[i].toInt()) diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/PacketAead.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/PacketAead.kt index 415e66b18..74449fef2 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/PacketAead.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/crypto/PacketAead.kt @@ -17,6 +17,7 @@ package org.connectbot.sshlib.crypto +import javax.crypto.Cipher import javax.security.auth.Destroyable internal data class AeadResult(val ciphertext: ByteArray, val tag: ByteArray) @@ -71,14 +72,29 @@ internal interface PacketAead : Destroyable { */ fun decrypt(packetLength: ByteArray, ciphertext: ByteArray, tag: ByteArray): ByteArray - /** Decrypt a contiguous ciphertext and trailing tag, authenticating before returning plaintext. */ - fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray): ByteArray { - if (encrypted.size < tagLength) throw org.connectbot.sshlib.transport.TransportException("Truncated AEAD packet") - val length = encrypted.size - tagLength - return decrypt(packetLength, encrypted.copyOfRange(0, length), encrypted.copyOfRange(length, encrypted.size)) + /** Authenticate the first [size] bytes (ciphertext and tag) and return independently owned plaintext. */ + fun decryptPacket(packetLength: ByteArray, encrypted: ByteArray, size: Int = encrypted.size): ByteArray { + if (size !in tagLength..encrypted.size) throw org.connectbot.sshlib.transport.TransportException("Truncated AEAD packet") + val length = size - tagLength + return decrypt(packetLength, encrypted.copyOfRange(0, length), encrypted.copyOfRange(length, size)) } fun encryptLength(sequenceNumber: Long, plainLength: ByteArray): ByteArray = throw UnsupportedOperationException("This cipher does not encrypt the length field") fun decryptLength(sequenceNumber: Long, encryptedLength: ByteArray): ByteArray = throw UnsupportedOperationException("This cipher does not encrypt the length field") } + +/** Feed a packet's header, payload and padding into one initialized cipher operation. */ +internal fun Cipher.encryptParts(plaintext: List, output: ByteArray) { + var written = 0 + for (index in 0 until plaintext.lastIndex) { + val part = plaintext[index] + written += update(part, 0, part.size, output, written) + } + if (plaintext.isEmpty()) { + doFinal(output, written) + } else { + val last = plaintext.last() + doFinal(last, 0, last.size, output, written) + } +} diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SftpStateMachine.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SftpStateMachine.kt index c19986740..94fd7bede 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SftpStateMachine.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SftpStateMachine.kt @@ -137,12 +137,6 @@ internal data class SftpTransitionMeta( val expectsPending: SftpPendingRequest? = null, ) : MetaInfo -internal data class SftpAcceptedTransition( - val id: SftpTransitionId, - val effects: Set, - val origin: SftpEventOrigin, -) - internal data class SftpFormalTransition( val id: SftpTransitionId, val eventId: SftpEventId, @@ -167,28 +161,26 @@ internal class SftpStateMachine { val id: SftpEventId, val effects: Set, val origin: SftpEventOrigin, - val action: suspend (SftpAcceptedTransition) -> Unit, + val action: (() -> Unit)? = null, val requiresReadySession: Boolean = true, ) : Event { - class SendInit(action: suspend (SftpAcceptedTransition) -> Unit) : + object SendInit : SftpEvent( SftpEventId.SEND_INIT, setOf(SftpEffect.SEND_INIT), SftpEventOrigin.LOCAL_COMMAND, - action, requiresReadySession = false, ) - class ReceiveVersion(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveVersion : SftpEvent( SftpEventId.RECEIVE_VERSION, setOf(SftpEffect.RECEIVE_VERSION), SftpEventOrigin.PARSED_PACKET, - action, requiresReadySession = false, ) - class OpenFile(action: suspend (SftpAcceptedTransition) -> Unit) : + class OpenFile(action: () -> Unit) : SftpEvent( SftpEventId.OPEN_FILE, setOf(SftpEffect.SEND_OPEN_FILE), @@ -196,7 +188,7 @@ internal class SftpStateMachine { action, ) - class OpenDir(action: suspend (SftpAcceptedTransition) -> Unit) : + class OpenDir(action: () -> Unit) : SftpEvent( SftpEventId.OPEN_DIR, setOf(SftpEffect.SEND_OPEN_DIR), @@ -204,7 +196,7 @@ internal class SftpStateMachine { action, ) - class ReadFile(action: suspend (SftpAcceptedTransition) -> Unit) : + class ReadFile(action: () -> Unit) : SftpEvent( SftpEventId.READ_FILE, setOf(SftpEffect.SEND_READ), @@ -212,7 +204,7 @@ internal class SftpStateMachine { action, ) - class WriteFile(action: suspend (SftpAcceptedTransition) -> Unit) : + class WriteFile(action: () -> Unit) : SftpEvent( SftpEventId.WRITE_FILE, setOf(SftpEffect.SEND_WRITE), @@ -220,7 +212,7 @@ internal class SftpStateMachine { action, ) - class ReadDir(action: suspend (SftpAcceptedTransition) -> Unit) : + class ReadDir(action: () -> Unit) : SftpEvent( SftpEventId.READ_DIR, setOf(SftpEffect.SEND_READDIR), @@ -228,7 +220,7 @@ internal class SftpStateMachine { action, ) - class CloseHandle(action: suspend (SftpAcceptedTransition) -> Unit) : + class CloseHandle(action: () -> Unit) : SftpEvent( SftpEventId.CLOSE_HANDLE, setOf(SftpEffect.SEND_CLOSE_HANDLE), @@ -236,7 +228,7 @@ internal class SftpStateMachine { action, ) - class Request(action: suspend (SftpAcceptedTransition) -> Unit) : + class Request(action: () -> Unit) : SftpEvent( SftpEventId.REQUEST, setOf(SftpEffect.SEND_REQUEST), @@ -244,47 +236,42 @@ internal class SftpStateMachine { action, ) - class ReceiveHandle(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveHandle : SftpEvent( SftpEventId.RECEIVE_HANDLE, setOf(SftpEffect.DELIVER_HANDLE), SftpEventOrigin.PARSED_PACKET, - action, ) - class ReceiveData(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveData : SftpEvent( SftpEventId.RECEIVE_DATA, setOf(SftpEffect.DELIVER_DATA), SftpEventOrigin.PARSED_PACKET, - action, ) - class ReceiveName(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveName : SftpEvent( SftpEventId.RECEIVE_NAME, setOf(SftpEffect.DELIVER_NAME), SftpEventOrigin.PARSED_PACKET, - action, ) - class ReceiveAttrs(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveAttrs : SftpEvent( SftpEventId.RECEIVE_ATTRS, setOf(SftpEffect.DELIVER_ATTRS), SftpEventOrigin.PARSED_PACKET, - action, ) - class ReceiveStatus(action: suspend (SftpAcceptedTransition) -> Unit) : + object ReceiveStatus : SftpEvent( SftpEventId.RECEIVE_STATUS, setOf(SftpEffect.DELIVER_STATUS), SftpEventOrigin.PARSED_PACKET, - action, ) - class Disconnect(action: suspend (SftpAcceptedTransition) -> Unit) : + class Disconnect(action: () -> Unit) : SftpEvent( SftpEventId.DISCONNECT, setOf(SftpEffect.DISCONNECT_SFTP), @@ -319,12 +306,12 @@ internal class SftpStateMachine { bindState(closed, SftpState.CLOSED) uninitialized.sftpTransition( - SftpEvent.SendInit {}, + SftpEvent.SendInit, SftpTransitionId.SEND_INIT_UNINITIALIZED, target = waitVersion, ) waitVersion.sftpTransition( - SftpEvent.ReceiveVersion {}, + SftpEvent.ReceiveVersion, SftpTransitionId.RECEIVE_VERSION_WAIT_VERSION, target = ready, ) @@ -366,35 +353,35 @@ internal class SftpStateMachine { ) ready.sftpTransition( - SftpEvent.ReceiveHandle {}, + SftpEvent.ReceiveHandle, SftpTransitionId.RECEIVE_HANDLE_READY, expectsPending = SftpPendingRequest.OPEN, guard = { pendingRequests.contains(PendingRequestKind.OPEN) }, onEffect = { pendingRequests.remove(PendingRequestKind.OPEN) }, ) ready.sftpTransition( - SftpEvent.ReceiveData {}, + SftpEvent.ReceiveData, SftpTransitionId.RECEIVE_DATA_READY, expectsPending = SftpPendingRequest.READ, guard = { pendingRequests.contains(PendingRequestKind.READ) }, onEffect = { pendingRequests.remove(PendingRequestKind.READ) }, ) ready.sftpTransition( - SftpEvent.ReceiveName {}, + SftpEvent.ReceiveName, SftpTransitionId.RECEIVE_NAME_READY, expectsPending = SftpPendingRequest.READ_DIR, guard = { pendingRequests.contains(PendingRequestKind.READ_DIR) }, onEffect = { pendingRequests.remove(PendingRequestKind.READ_DIR) }, ) ready.sftpTransition( - SftpEvent.ReceiveAttrs {}, + SftpEvent.ReceiveAttrs, SftpTransitionId.RECEIVE_ATTRS_READY, expectsPending = SftpPendingRequest.REQUEST, guard = { pendingRequests.contains(PendingRequestKind.GENERIC) }, onEffect = { pendingRequests.remove(PendingRequestKind.GENERIC) }, ) ready.sftpTransition( - SftpEvent.ReceiveStatus {}, + SftpEvent.ReceiveStatus, SftpTransitionId.RECEIVE_STATUS_READY, expectsPending = null, guard = { pendingRequests.isNotEmpty() }, @@ -427,21 +414,21 @@ internal class SftpStateMachine { val state: SftpState get() = activeState - suspend fun sendInit(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.SendInit(action)) - suspend fun receiveVersion(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveVersion(action)) - suspend fun openFile(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.OpenFile(action)) - suspend fun openDir(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.OpenDir(action)) - suspend fun readFile(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReadFile(action)) - suspend fun writeFile(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.WriteFile(action)) - suspend fun readDir(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReadDir(action)) - suspend fun closeHandle(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.CloseHandle(action)) - suspend fun request(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.Request(action)) - suspend fun receiveHandle(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveHandle(action)) - suspend fun receiveData(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveData(action)) - suspend fun receiveName(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveName(action)) - suspend fun receiveAttrs(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveAttrs(action)) - suspend fun receiveStatus(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.ReceiveStatus(action)) - suspend fun disconnect(action: suspend (SftpAcceptedTransition) -> Unit) = process(SftpEvent.Disconnect(action)) + suspend fun sendInit() = process(SftpEvent.SendInit) + suspend fun receiveVersion() = process(SftpEvent.ReceiveVersion) + suspend fun openFile(action: () -> Unit) = process(SftpEvent.OpenFile(action)) + suspend fun openDir(action: () -> Unit) = process(SftpEvent.OpenDir(action)) + suspend fun readFile(action: () -> Unit) = process(SftpEvent.ReadFile(action)) + suspend fun writeFile(action: () -> Unit) = process(SftpEvent.WriteFile(action)) + suspend fun readDir(action: () -> Unit) = process(SftpEvent.ReadDir(action)) + suspend fun closeHandle(action: () -> Unit) = process(SftpEvent.CloseHandle(action)) + suspend fun request(action: () -> Unit) = process(SftpEvent.Request(action)) + suspend fun receiveHandle() = process(SftpEvent.ReceiveHandle) + suspend fun receiveData() = process(SftpEvent.ReceiveData) + suspend fun receiveName() = process(SftpEvent.ReceiveName) + suspend fun receiveAttrs() = process(SftpEvent.ReceiveAttrs) + suspend fun receiveStatus() = process(SftpEvent.ReceiveStatus) + suspend fun disconnect(action: () -> Unit) = process(SftpEvent.Disconnect(action)) internal fun formalModel(): SftpFormalModel { val states = collectStates(stateMachine) @@ -506,7 +493,7 @@ internal class SftpStateMachine { ) }.onTriggered { onEffect?.invoke() - it.event.action(SftpAcceptedTransition(id, effects, event.origin)) + it.event.action?.invoke() } } } diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachine.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachine.kt index fbee834df..6149b7f2c 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachine.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachine.kt @@ -741,9 +741,9 @@ internal interface SshClientCallbacks { suspend fun sendKexInit() fun receiveKexInit(msg: SshMsgKexinit, initialExchange: Boolean): Boolean suspend fun sendKexExchangeInit() - suspend fun receiveKexDhReply(msg: SshMsgKexdhReply) - suspend fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) - suspend fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) + fun receiveKexDhReply(msg: SshMsgKexdhReply) + fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) + fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) fun isRekeying(): Boolean fun isAuthenticationRequestPending(): Boolean fun authenticationRequestStarted() diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/KtorTcpTransport.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/KtorTcpTransport.kt index d02eb8724..3010418f2 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/KtorTcpTransport.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/KtorTcpTransport.kt @@ -218,11 +218,13 @@ class KtorTcpTransport internal constructor( } } - override suspend fun read(count: Int): ByteArray { + override suspend fun read(count: Int): ByteArray = readInto(count) + + internal suspend fun readInto(count: Int, destination: ByteArray? = null): ByteArray { val channel = readChannel ?: throw TransportException("Not connected") try { - val buffer = ByteArray(count) + val buffer = destination ?: ByteArray(count) channel.readFully(buffer, 0, count) return buffer } catch (e: Exception) { diff --git a/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/PacketIO.kt b/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/PacketIO.kt index ac04e2fc8..70de5fe21 100644 --- a/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/PacketIO.kt +++ b/sshlib/src/main/kotlin/org/connectbot/sshlib/transport/PacketIO.kt @@ -75,6 +75,9 @@ internal class PacketIO( private var sendAead: PacketAead? = null private var receiveAead: PacketAead? = null + // Only the packet reader accesses this scratch space. Decoders retain owned plaintext. + private var encryptedInput = ByteArray(0) + // Whether to use ETM (Encrypt-then-MAC) mode for each direction private var sendEtm: Boolean = false private var receiveEtm: Boolean = false @@ -256,7 +259,8 @@ internal class PacketIO( return ReceivedPacket( payload = payload, sequenceNumber = sequenceNumber, - messageNumber = payload.messageNumber(), + // Unknown message numbers have no enum entry. Only those need the raw Kaitai instance. + messageNumber = payload.messageType()?.id()?.toInt() ?: payload.messageNumber(), ) } @@ -452,9 +456,15 @@ internal class PacketIO( throw TransportException("Invalid AEAD packet length: $packetLength") } - val encrypted = transport.read(packetLength + aead.tagLength) - bytesReceivedOnWire += encrypted.size - val plaintext = ByteBuffer.wrap(aead.decryptPacket(aadBytes, encrypted)) + val wireSize = packetLength + aead.tagLength + val encrypted = if (transport is KtorTcpTransport) { + if (encryptedInput.size < wireSize) encryptedInput = ByteArray(wireSize) + transport.readInto(wireSize, encryptedInput) + } else { + transport.read(wireSize) + } + bytesReceivedOnWire += wireSize + val plaintext = ByteBuffer.wrap(aead.decryptPacket(aadBytes, encrypted, wireSize)) receiveSequenceNumber++ return extractPayloadStream(plaintext) diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/client/SshChannelDrainTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/client/SshChannelDrainTest.kt index 67aa3b0f8..90ee53ea8 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/client/SshChannelDrainTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/client/SshChannelDrainTest.kt @@ -305,8 +305,8 @@ class SshChannelDrainTest { val io = SftpPacketIO(session) val allowRead = CompletableDeferred() val machine = SftpStateMachine() - machine.sendInit { } - machine.receiveVersion { } + machine.sendInit() + machine.receiveVersion() val dispatcher = SftpDispatcher( object : SftpPacketTransport by io { override suspend fun readPacket(): SftpResult { @@ -319,7 +319,7 @@ class SshChannelDrainTest { val reader = dispatcher.startReadLoop(backgroundScope) try { val pending = backgroundScope.async { - dispatcher.request(5, byteArrayOf()) { action -> machine.readFile { action() } } + dispatcher.request(5, byteArrayOf(), authorize = machine::readFile) } // The first request has ID 1. Observing its SSH data proves admission and write. withTimeout(5_000) { server.awaitChannelData() } diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcherTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcherTest.kt index ebcac4a9f..261d175be 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcherTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/client/sftp/SftpDispatcherTest.kt @@ -68,11 +68,11 @@ class SftpDispatcherTest { val release = CompletableDeferred() transport.writeGate = release val machine = SftpStateMachine() - machine.sendInit {} - machine.receiveVersion {} + machine.sendInit() + machine.receiveVersion() val dispatcher = SftpDispatcher(transport, machine) val request = backgroundScope.async { - dispatcher.request(5, byteArrayOf()) { action -> machine.readFile { action() } } + dispatcher.request(5, byteArrayOf(), authorize = machine::readFile) } transport.awaitWrite() try { diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/AesGcmCipherTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/AesGcmCipherTest.kt index 36c3911d3..a3da04f70 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/AesGcmCipherTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/AesGcmCipherTest.kt @@ -123,9 +123,10 @@ class AesGcmCipherTest { reference.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), GCMParameterSpec(128, makeIv(counter = 127L + index))) reference.updateAAD(length) val encrypted = reference.doFinal(data) - val original = encrypted.copyOf() - val view = ByteBuffer.wrap(receiver.decryptPacket(length, encrypted)) - assertArrayEquals(original, encrypted) + val scratch = encrypted.copyOf(encrypted.size + 113) + val view = ByteBuffer.wrap(receiver.decryptPacket(length, scratch, encrypted.size)) + assertArrayEquals(encrypted, scratch.copyOf(encrypted.size)) + scratch.fill(42) assertEquals(size, view.capacity()) assertEquals(size, view.remaining()) val decoded = ByteArray(size) diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305CipherTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305CipherTest.kt index ca860bc0f..2f0d7db4c 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305CipherTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/crypto/ChaCha20Poly1305CipherTest.kt @@ -35,15 +35,19 @@ class ChaCha20Poly1305CipherTest { val plaintext = ByteArray(32768) { (it + sequence).toByte() } val length = java.nio.ByteBuffer.allocate(4).putInt(plaintext.size).array() val wireLength = sender.encryptLength(sequence, length) - val encrypted = sender.encryptPacket(wireLength, plaintext) + val parts = listOf(plaintext.copyOfRange(0, 2), plaintext.copyOfRange(2, 32761), plaintext.copyOfRange(32761, plaintext.size)) + val encrypted = sender.encryptPacket(wireLength, parts) assertContentEquals(length, receiver.decryptLength(sequence, wireLength)) - assertContentEquals(plaintext, receiver.decryptPacket(wireLength, encrypted)) + val scratch = encrypted.copyOf(encrypted.size + 113) + val decoded = receiver.decryptPacket(wireLength, scratch, encrypted.size) + scratch.fill(42) + assertContentEquals(plaintext, decoded) for (offset in listOf(0, encrypted.lastIndex)) { - val changed = encrypted.copyOf() + val changed = encrypted.copyOf(encrypted.size + 113) changed[offset] = (changed[offset].toInt() xor 1).toByte() val rejecting = ChaCha20Poly1305Cipher(key.copyOf()) rejecting.decryptLength(sequence, wireLength) - assertFailsWith { rejecting.decryptPacket(wireLength, changed) } + assertFailsWith { rejecting.decryptPacket(wireLength, changed, encrypted.size) } } } assertFailsWith { receiver.decryptPacket(ByteArray(4), ByteArray(15)) } @@ -286,7 +290,7 @@ class ChaCha20Poly1305CipherTest { } @Test - fun `constantTimeEquals is size-sensitive - different length returns false`() { + fun `split decryption rejects a truncated tag`() { val key = ByteArray(64) val enc = ChaCha20Poly1305Cipher(key) val dec = ChaCha20Poly1305Cipher(key) @@ -296,11 +300,8 @@ class ChaCha20Poly1305CipherTest { val encLen = enc.encryptLength(0L, plainLength) val result = enc.encrypt(encLen, plaintext) - // A tag with correct bytes but wrong length would be caught by constantTimeEquals size check; - // verify the cipher correctly rejects a tag of different length by providing wrong data dec.decryptLength(0L, encLen) assertFailsWith { - // Pass a 15-byte tag instead of 16 dec.decrypt(encLen, result.ciphertext, result.tag.copyOf(15)) } } diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SftpStateMachineTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SftpStateMachineTest.kt index c89b514b3..1a0efc209 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SftpStateMachineTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SftpStateMachineTest.kt @@ -18,11 +18,14 @@ package org.connectbot.sshlib.protocol import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.yield import java.nio.file.Files import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -99,25 +102,24 @@ class SftpStateMachineTest { } @Test - fun `request write action is serialized with disconnect`() = runBlocking { + fun `request admission is serialized with disconnect`() = runBlocking { val machine = SftpStateMachine() - assertTrue(machine.sendInit { }) - assertTrue(machine.receiveVersion { }) + assertTrue(machine.sendInit()) + assertTrue(machine.receiveVersion()) val writeStarted = CompletableDeferred() - val finishWrite = CompletableDeferred() + val finishWrite = CountDownLatch(1) - val request = async { + val request = async(Dispatchers.Default) { machine.request { writeStarted.complete(Unit) - finishWrite.await() + check(finishWrite.await(5, TimeUnit.SECONDS)) } } writeStarted.await() - val disconnect = async { machine.disconnect { } } - yield() + val disconnect = async(start = CoroutineStart.UNDISPATCHED) { machine.disconnect { } } assertFalse(disconnect.isCompleted) - finishWrite.complete(Unit) + finishWrite.countDown() assertTrue(request.await()) assertTrue(disconnect.await()) assertEquals(SftpState.CLOSED, machine.state) @@ -137,25 +139,25 @@ class SftpStateMachineTest { @Test fun `enforces response matching in state machine`() = runBlocking { val machine = SftpStateMachine() - assertTrue(machine.sendInit { }) - assertTrue(machine.receiveVersion { }) + assertTrue(machine.sendInit()) + assertTrue(machine.receiveVersion()) // Issue OpenFile -> expect PendingOpen assertTrue(machine.openFile { }) // ReceiveData is declined because no READ is pending - assertFalse(machine.receiveData { }) + assertFalse(machine.receiveData()) // ReceiveHandle is accepted - assertTrue(machine.receiveHandle { }) + assertTrue(machine.receiveHandle()) // Issue ReadFile -> expect PendingRead assertTrue(machine.readFile { }) // ReceiveHandle is declined because no OPEN is pending - assertFalse(machine.receiveHandle { }) + assertFalse(machine.receiveHandle()) // ReceiveData is accepted - assertTrue(machine.receiveData { }) + assertTrue(machine.receiveData()) } } diff --git a/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachineTest.kt b/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachineTest.kt index b2594f4a1..ae5abdc94 100644 --- a/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachineTest.kt +++ b/sshlib/src/test/kotlin/org/connectbot/sshlib/protocol/SshClientStateMachineTest.kt @@ -377,13 +377,13 @@ class SshClientStateMachineTest { override suspend fun sendKexExchangeInit() { actions += "sendKexExchangeInit" } - override suspend fun receiveKexDhReply(msg: SshMsgKexdhReply) { + override fun receiveKexDhReply(msg: SshMsgKexdhReply) { actions += "receiveKexDhReply" } - override suspend fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) { + override fun receiveKexEcdhReply(msg: SshMsgKexEcdhReply) { actions += "receiveKexEcdhReply" } - override suspend fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) { + override fun receiveKexDhGexReply(msg: SshMsgKexDhGexReply) { actions += "receiveKexDhGexReply" } override fun isRekeying(): Boolean = rekeying