-
Notifications
You must be signed in to change notification settings - Fork 15
Enhance SSL handling and testing #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guruz
merged 6 commits into
opencloud-eu:main
from
zerox80:fix/tls-hostname-verification
Apr 3, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5cbb4d5
Enhance SSL handling and testing: Update SslUntrustedCertDialog for b…
zerox80 9c68f67
Update opencloudApp/src/main/java/eu/opencloud/android/ui/dialog/SslU…
zerox80 7fdcac7
Update opencloudApp/src/main/java/eu/opencloud/android/ui/dialog/SslU…
zerox80 17ba765
Fix formatting: Add newline at end of HttpClientTlsTest.kt
zerox80 cd0e178
Merge branch 'fix/tls-hostname-verification' of https://github.com/ze…
zerox80 21f5f1d
Add ThreadLocal for last server certificate in AdvancedX509TrustManag…
zerox80 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
opencloudComLibrary/src/test/java/eu/opencloud/android/lib/common/http/HttpClientTlsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package eu.opencloud.android.lib.common.http | ||
|
|
||
| import android.content.Context | ||
| import android.os.Build | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import eu.opencloud.android.lib.common.network.CertificateCombinedException | ||
| import eu.opencloud.android.lib.common.network.NetworkUtils | ||
| import eu.opencloud.android.lib.common.operations.RemoteOperationResult | ||
| import okhttp3.Request | ||
| import okhttp3.mockwebserver.MockResponse | ||
| import okhttp3.mockwebserver.MockWebServer | ||
| import okhttp3.tls.HandshakeCertificates | ||
| import okhttp3.tls.HeldCertificate | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertSame | ||
| import org.junit.Assert.assertThrows | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
| import java.lang.reflect.Field | ||
| import javax.net.ssl.SSLPeerUnverifiedException | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [Build.VERSION_CODES.O], manifest = Config.NONE) | ||
| class HttpClientTlsTest { | ||
|
|
||
| private val context by lazy { ApplicationProvider.getApplicationContext<Context>() } | ||
| private lateinit var server: MockWebServer | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| resetKnownServersStore() | ||
| server = MockWebServer() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| runCatching { server.shutdown() } | ||
| resetKnownServersStore() | ||
| } | ||
|
|
||
| @Test | ||
| fun `rejects trusted certificate for the wrong hostname`() { | ||
| val wrongHostnameCertificate = HeldCertificate.Builder() | ||
| .commonName(WRONG_HOSTNAME) | ||
| .addSubjectAlternativeName(WRONG_HOSTNAME) | ||
| .build() | ||
| val serverCertificates = HandshakeCertificates.Builder() | ||
| .heldCertificate(wrongHostnameCertificate) | ||
| .build() | ||
|
|
||
| server.useHttps(serverCertificates.sslSocketFactory(), false) | ||
| server.enqueue(MockResponse().setResponseCode(200).setBody("ok")) | ||
| server.start() | ||
|
|
||
| NetworkUtils.addCertToKnownServersStore(wrongHostnameCertificate.certificate, context) | ||
|
|
||
| val request = Request.Builder() | ||
| .url(server.url("/")) | ||
| .build() | ||
|
|
||
| val thrown = assertThrows(Exception::class.java) { | ||
| TestHttpClient(context).okHttpClient.newCall(request).execute().use { } | ||
| } | ||
|
|
||
| assertNotNull(findCause<SSLPeerUnverifiedException>(thrown)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `wraps hostname mismatch as certificate combined exception`() { | ||
| val peerUnverifiedException = SSLPeerUnverifiedException("Hostname localhost not verified") | ||
|
|
||
| val result = RemoteOperationResult<Unit>(peerUnverifiedException) | ||
|
|
||
| assertEquals( | ||
| RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED, | ||
| result.code | ||
| ) | ||
| assertTrue(result.exception is CertificateCombinedException) | ||
|
|
||
| val combinedException = result.exception as CertificateCombinedException | ||
| assertSame(peerUnverifiedException, combinedException.sslPeerUnverifiedException) | ||
| } | ||
|
|
||
| private inline fun <reified T : Throwable> findCause(throwable: Throwable): T? { | ||
| var current: Throwable? = throwable | ||
| while (current != null) { | ||
| if (current is T) { | ||
| return current | ||
| } | ||
| current = current.cause | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| private fun resetKnownServersStore() { | ||
| context.deleteFile(KNOWN_SERVERS_STORE_FILE) | ||
|
|
||
| val field: Field = NetworkUtils::class.java.getDeclaredField("mKnownServersStore") | ||
| field.isAccessible = true | ||
| field.set(null, null) | ||
| } | ||
|
|
||
| private class TestHttpClient(context: Context) : HttpClient(context) | ||
|
|
||
| companion object { | ||
| private const val WRONG_HOSTNAME = "wrong.example.test" | ||
| private const val KNOWN_SERVERS_STORE_FILE = "knownServers.bks" | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.