Skip to content

Instantly share code, notes, and snippets.

@yschimke
Created December 6, 2020 15:40
Show Gist options
  • Save yschimke/4a1eda31c584a64549c790d887f275e0 to your computer and use it in GitHub Desktop.
Save yschimke/4a1eda31c584a64549c790d887f275e0 to your computer and use it in GitHub Desktop.
package com.example.myapplication
import android.annotation.SuppressLint
import android.os.Bundle
import android.security.KeyChain
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.net.Socket
import java.security.KeyStore
import java.security.Principal
import java.security.PrivateKey
import java.security.cert.X509Certificate
import javax.net.ssl.*
class MainActivity : AppCompatActivity() {
@SuppressLint("WrongThread")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
KeyChain.choosePrivateKeyAlias(this,
{ alias -> makeRequest(alias!!) },
arrayOf("RSA", "DSA"), // List of acceptable key types. null for any
null, // issuer, null for any
"server.cryptomix.com", // host name of server requesting the cert, null if unavailable
443, // port of server requesting the cert, -1 if unavailable
"mykey") // alias to preselect, null if unavailable
}
private fun makeRequest(alias: String) {
val t: Thread = object : Thread() {
override fun run() {
sleep(5000)
val pk = KeyChain.getPrivateKey(applicationContext, "mykey")!!
val chain = KeyChain.getCertificateChain(applicationContext, "mykey")!!
println(pk)
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(null as KeyStore?)
val trustManagers = trustManagerFactory.trustManagers
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
keyManagerFactory.init(null, null)
val keyManagers = keyManagerFactory.keyManagers
val km = object : X509KeyManager {
override fun getClientAliases(keyType: String?, issuers: Array<Principal>): Array<String> {
return arrayOf("mykey")
}
override fun chooseClientAlias(keyType: Array<out String>?, issuers: Array<out Principal>?, socket: Socket?): String {
return "mykey"
}
override fun getServerAliases(keyType: String?, issuers: Array<Principal>): Array<String> {
return arrayOf()
}
override fun chooseServerAlias(keyType: String?, issuers: Array<Principal>, socket: Socket): String {
return ""
}
override fun getCertificateChain(alias: String?): Array<X509Certificate> {
return chain
}
override fun getPrivateKey(alias: String?): PrivateKey {
return pk
}
}
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(arrayOf(km), trustManagers, null)
val client: OkHttpClient = OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManagers[0] as X509TrustManager)
.build()
val request: Request = Request.Builder()
// .url("https://prod.idrix.eu/secure/")
.url("https://server.cryptomix.com/secure/")
.build()
try {
client.newCall(request).execute().use { response ->
val string = response.body!!.string()
println(string)
}
} catch (ioe: IOException) {
println(ioe)
}
}
}
t.start()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment