@@ -38,13 +38,13 @@ import kotlin.coroutines.suspendCoroutine
3838sealed class SshAuthData {
3939 class Password (val passwordFinder : InteractivePasswordFinder ) : SshAuthData() {
4040 override fun clearCredentials () {
41- passwordFinder.clearPassword ()
41+ passwordFinder.clearPasswords ()
4242 }
4343 }
4444
4545 class PublicKeyFile (val keyFile : File , val passphraseFinder : InteractivePasswordFinder ) : SshAuthData() {
4646 override fun clearCredentials () {
47- passphraseFinder.clearPassword ()
47+ passphraseFinder.clearPasswords ()
4848 }
4949 }
5050
@@ -57,13 +57,14 @@ abstract class InteractivePasswordFinder : PasswordFinder {
5757
5858 private var isRetry = false
5959 private var lastPassword: CharArray? = null
60+ private val rememberToWipe: MutableList <CharArray > = mutableListOf ()
6061
6162 fun resetForReuse () {
6263 isRetry = false
6364 }
6465
65- fun clearPassword () {
66- lastPassword?. clear()
66+ fun clearPasswords () {
67+ rememberToWipe.forEach { it. clear() }
6768 lastPassword = null
6869 }
6970
@@ -73,17 +74,20 @@ abstract class InteractivePasswordFinder : PasswordFinder {
7374 // now being reused for a new one. We try the previous password so that the user
7475 // does not have to type it again.
7576 isRetry = true
76- return lastPassword!!
77+ return lastPassword!! .clone(). also { rememberToWipe.add(it) }
7778 }
78- clearPassword ()
79+ clearPasswords ()
7980 val password = runBlocking(Dispatchers .Main ) {
8081 suspendCoroutine<String ?> { cont ->
8182 askForPassword(cont, isRetry)
8283 }
8384 }
8485 isRetry = true
85- return password?.toCharArray()?.also { lastPassword = it }
86- ? : throw SSHException (DisconnectReason .AUTH_CANCELLED_BY_USER )
86+ if (password == null )
87+ throw SSHException (DisconnectReason .AUTH_CANCELLED_BY_USER )
88+ val passwordChars = password.toCharArray().also { rememberToWipe.add(it) }
89+ lastPassword = passwordChars
90+ return passwordChars.clone().also { rememberToWipe.add(it) }
8791 }
8892
8993 final override fun shouldRetry (resource : Resource <* >? ) = true
0 commit comments