Https Issue with Android proxy server using ktor framework

44 Views Asked by At

I am using ktor framework to develop a proxy server on Android device. The server works for http requests But i get error when using https url. The following is the log with curl. I read the ktor docs regarding ssl and certifigtes. https://ktor.io/docs/ssl.html#self-signed-code

But i get error about use of JKS keys

curl -v -x http://aaa:[email protected]:2222 https://www.google.com

*   Trying 192.168.0.100:2222...
* Connected to 192.168.0.100 (192.168.0.100) port 2222 (#0)
* allocate connect buffer
* Establish HTTP proxy tunnel to www.google.com:443
* Proxy auth using Basic with user 'aaa'
> CONNECT www.google.com:443 HTTP/1.1
> Host: www.google.com:443
> Proxy-Authorization: Basic 
> User-Agent: curl/7.83.1
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 OK
< Content-Type: application/octet-stream
<
* Proxy replied 200 to CONNECT request
* CONNECT phase completed
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid
* Closing connection 0
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid

------update Rightnow i don't' have any log. Below is the code. Receivechannel closes after server sends 200 HTTP OK

val tcpSocketBuilder = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp()

val server: Socket?
try {
    //end point
    server = tcpSocketBuilder.connect(call.request.host(), call.request.port())
} catch (e: Exception) {
    Log.v(TAG,"jothi Failed to connect to ${call.request.host()}:${call.request.port()}\n\t${e.printStackTrace()}")
  
    return
}

Log.v(TAG,"jothi Connected to ${call.request.host()}:${call.request.port()}")
val successConnectionString =
    "HTTP/1.1 200 OK\r\nServer-test: https-proxy\r\n\r\n"

call.respondBytesWriter(status = HttpStatusCode.OK){successConnectionString}
Log.v(TAG,"jothi response send ")

val serverReader = server.openReadChannel()
val serverWriter = server.openWriteChannel()






delay(20)

            val readChannel: ByteReadChannel = call.receiveChannel()
            val size = readChannel.availableForRead
            val byteArray: ByteArray = ByteArray(size)
readChannel.readFully(byteArray,0,size)
           Log.v(TAG,"jothi channel is closed for read  " +readChannel.isClosedForRead)
            Log.v(TAG,"jothi read size " +size)

-----TLS Config- It is a proxy so i want to tunnel the https. But after connect the client closes channel.The ssl key is not real. For testing only. It works when i connect from browser with https://192.168.0.100:2222

val pass = "testpass" //keystore password val alias = "certificateAlias" //certifigate alias

val filedir = applicationContext.getExternalFilesDir(null) //app specific files

val destfolder = File(filedir, "jothi")
if (!destfolder.exists()) {
    if (!destfolder.mkdirs()) {
        Log.v(TAG, "jothi Directory not created")
    }
}
val keyStoreFile = File(filedir,"keystore.Jks")

//keystore with certifigates
val keystore = buildKeyStore {
    certificate(alias) {
        hash = HashAlgorithm.SHA256
        sign = SignatureAlgorithm.ECDSA
        keySizeInBits = 256
        password = pass
    }
}
keystore.saveToFile(keyStoreFile, pass)
0

There are 0 best solutions below