Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 188b519

Browse files
authoredMay 11, 2025··
Added support for API 36 checkServerTrusted (#139)
1 parent 8f364ad commit 188b519

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
 

‎certificatetransparency/src/main/kotlin/com/appmattus/certificatetransparency/internal/verifier/CertificateTransparencyTrustManagerBasic.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ internal class CertificateTransparencyTrustManagerBasic(
6969
null
7070
}
7171

72+
private val checkServerTrustedMethodApi36: Method? = try {
73+
delegate::class.java.getDeclaredMethod(
74+
"checkServerTrusted",
75+
Array<X509Certificate>::class.java,
76+
ByteArray::class.java,
77+
ByteArray::class.java,
78+
String::class.java,
79+
String::class.java
80+
)
81+
} catch (ignored: NoSuchMethodException) {
82+
null
83+
}
84+
7285
private val isSameTrustConfigurationMethod: Method? = try {
7386
delegate::class.java.getDeclaredMethod("isSameTrustConfiguration", String::class.java, String::class.java)
7487
} catch (ignored: NoSuchMethodException) {
@@ -118,6 +131,29 @@ internal class CertificateTransparencyTrustManagerBasic(
118131
return certs
119132
}
120133

134+
// Called through reflection by X509TrustManagerExtensions on Android
135+
@Suppress("unused")
136+
fun checkServerTrusted(
137+
chain: Array<out X509Certificate>,
138+
ocspData: ByteArray?,
139+
tlsSctData: ByteArray?,
140+
authType: String,
141+
host: String
142+
): List<X509Certificate> {
143+
@Suppress("UNCHECKED_CAST")
144+
val certs = checkServerTrustedMethodApi36!!.invoke(delegate, chain, ocspData, tlsSctData, authType, host) as List<X509Certificate>
145+
146+
val result = verifyCertificateTransparency(host, certs.toList())
147+
148+
logger?.log(host, result)
149+
150+
if (result is VerificationResult.Failure && failOnError()) {
151+
throw CertificateException("Certificate transparency failed")
152+
}
153+
154+
return certs
155+
}
156+
121157
// Called through reflection by X509TrustManagerExtensions on Android
122158
@Suppress("unused")
123159
fun isSameTrustConfiguration(hostname1: String?, hostname2: String?): Boolean {

‎certificatetransparency/src/main/kotlin/com/appmattus/certificatetransparency/internal/verifier/CertificateTransparencyTrustManagerExtended.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import javax.net.ssl.SSLEngine
3737
import javax.net.ssl.X509ExtendedTrustManager
3838
import javax.net.ssl.X509TrustManager
3939

40-
@Suppress("LongParameterList", "CustomX509TrustManager", "NewApi")
40+
@Suppress("LongParameterList", "CustomX509TrustManager", "NewApi", "TooManyFunctions")
4141
internal class CertificateTransparencyTrustManagerExtended(
4242
private val delegate: X509TrustManager,
4343
includeHosts: Set<Host>,
@@ -72,6 +72,19 @@ internal class CertificateTransparencyTrustManagerExtended(
7272
null
7373
}
7474

75+
private val checkServerTrustedMethodApi36: Method? = try {
76+
delegate::class.java.getDeclaredMethod(
77+
"checkServerTrusted",
78+
Array<X509Certificate>::class.java,
79+
ByteArray::class.java,
80+
ByteArray::class.java,
81+
String::class.java,
82+
String::class.java
83+
)
84+
} catch (ignored: NoSuchMethodException) {
85+
null
86+
}
87+
7588
private val isSameTrustConfigurationMethod: Method? = try {
7689
delegate::class.java.getDeclaredMethod("isSameTrustConfiguration", String::class.java, String::class.java)
7790
} catch (ignored: NoSuchMethodException) {
@@ -154,6 +167,29 @@ internal class CertificateTransparencyTrustManagerExtended(
154167
return certs
155168
}
156169

170+
// Called through reflection by X509TrustManagerExtensions on Android
171+
@Suppress("unused")
172+
fun checkServerTrusted(
173+
chain: Array<out X509Certificate>,
174+
ocspData: ByteArray?,
175+
tlsSctData: ByteArray?,
176+
authType: String,
177+
host: String
178+
): List<X509Certificate> {
179+
@Suppress("UNCHECKED_CAST")
180+
val certs = checkServerTrustedMethodApi36!!.invoke(delegate, chain, ocspData, tlsSctData, authType, host) as List<X509Certificate>
181+
182+
val result = verifyCertificateTransparency(host, certs.toList())
183+
184+
logger?.log(host, result)
185+
186+
if (result is VerificationResult.Failure && failOnError()) {
187+
throw CertificateException("Certificate transparency failed")
188+
}
189+
190+
return certs
191+
}
192+
157193
// Called through reflection by X509TrustManagerExtensions on Android
158194
@Suppress("unused")
159195
fun isSameTrustConfiguration(hostname1: String?, hostname2: String?): Boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.