allow user to specify tls version

This commit is contained in:
Jonathan Fisher 2020-11-09 22:44:36 +00:00
parent 6c7f6d4b19
commit 3e0c9e9728
5 changed files with 23 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import oppen.ariane.Ariane
import oppen.toURI import oppen.toURI
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import oppen.ariane.io.GemState import oppen.ariane.io.GemState
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -26,13 +27,13 @@ class GeminiDatasourceTests {
private val capsuleIndex = 3 private val capsuleIndex = 3
private fun setTLSProtocol(protocol: String){ @Before
gemini = Datasource.factory(InstrumentationRegistry.getInstrumentation().targetContext, protocol) private fun setup(){
gemini = Datasource.factory(InstrumentationRegistry.getInstrumentation().targetContext)
} }
@Test @Test
fun arianeHomePageTest(){ fun arianeHomePageTest(){
setTLSProtocol("TLSv1")
var hasRequested = false var hasRequested = false
var hasResponded = false var hasResponded = false
@ -58,12 +59,9 @@ class GeminiDatasourceTests {
@Test @Test
fun aCapsuleTest(){ fun aCapsuleTest(){
setTLSProtocol("TLSv1.3")
var hasRequested = false var hasRequested = false
var hasResponded = false var hasResponded = false
gemini.request(capsules[capsuleIndex].toURI()){ state -> gemini.request(capsules[capsuleIndex].toURI()){ state ->
when(state){ when(state){

View File

@ -8,8 +8,8 @@ interface Datasource {
fun request(uri: URI, onUpdate: (state: GemState) -> Unit) fun request(uri: URI, onUpdate: (state: GemState) -> Unit)
companion object{ companion object{
fun factory(context: Context, protocol: String): Datasource { fun factory(context: Context): Datasource {
return GeminiDatasource(context, protocol) return GeminiDatasource(context)
} }
} }
} }

View File

@ -2,6 +2,7 @@ package oppen.ariane.io.gemini
import android.content.Context import android.content.Context
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.preference.PreferenceManager
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import oppen.ariane.io.GemState import oppen.ariane.io.GemState
@ -21,10 +22,11 @@ const val GEMINI_SCHEME = "gemini"
* *
*/ */
class GeminiDatasource( class GeminiDatasource(
private val context: Context, private val context: Context): Datasource {
private val protocol: String): Datasource {
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
private var last: URI? = null private var last: URI? = null
override fun request(uri: URI, onUpdate: (state: GemState) -> Unit) { override fun request(uri: URI, onUpdate: (state: GemState) -> Unit) {
//Any inputted uri starting with a colon is an app-specific command, eg. :prefs :settings //Any inputted uri starting with a colon is an app-specific command, eg. :prefs :settings
@ -124,6 +126,10 @@ class GeminiDatasource(
last = uri last = uri
val port = if(uri.port == -1) 1965 else uri.port val port = if(uri.port == -1) 1965 else uri.port
val protocol = prefs.getString("tls_protocol", "TLS")
println("REQ_PROTOCOL: $protocol")
val sslContext = SSLContext.getInstance(protocol) val sslContext = SSLContext.getInstance(protocol)
sslContext.init(null, trustAllCerts, SecureRandom()) sslContext.init(null, trustAllCerts, SecureRandom())
@ -133,7 +139,12 @@ class GeminiDatasource(
try { try {
socket = factory.createSocket(uri.host, port) as SSLSocket socket = factory.createSocket(uri.host, port) as SSLSocket
socket.enabledCipherSuites = factory.supportedCipherSuites socket.enabledCipherSuites = factory.supportedCipherSuites
socket.enabledProtocols = socket.supportedProtocols
when (protocol) {
"TLS" -> socket.enabledProtocols = socket.supportedProtocols
else -> socket.enabledProtocols = arrayOf(protocol)
}
socket.startHandshake() socket.startHandshake()
}catch(ce: ConnectException){ }catch(ce: ConnectException){
println("socket error: $ce") println("socket error: $ce")

View File

@ -102,7 +102,7 @@ class GemActivity : AppCompatActivity() {
model.initialise( model.initialise(
home = home ?: Ariane.DEFAULT_HOME_CAPSULE, home = home ?: Ariane.DEFAULT_HOME_CAPSULE,
gemini = Datasource.factory(this, "TLSv1.2"), gemini = Datasource.factory(this),
bookmarks = BookmarksDatasource.getDefault(applicationContext) bookmarks = BookmarksDatasource.getDefault(applicationContext)
){ state -> ){ state ->

View File

@ -24,6 +24,8 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
tlsCategory.title = "TLS Config" tlsCategory.title = "TLS Config"
screen.addPreference(tlsCategory) screen.addPreference(tlsCategory)
//todo - need to add the default "TLS" option here,
// otherwise once set in this screen user can't get that default back
val sslContext = SSLContext.getInstance("TLS") val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, null, SecureRandom()) sslContext.init(null, null, SecureRandom())
val factory: SSLSocketFactory = sslContext.socketFactory val factory: SSLSocketFactory = sslContext.socketFactory