Skip to content

Commit 0345cb7

Browse files
committed
test: harden generated TLS trust setup
1 parent 5cc09ef commit 0345cb7

1 file changed

Lines changed: 14 additions & 50 deletions

File tree

  • waltid-services/waltid-service-commons-test/src/main/kotlin/id/walt/commons/testing

waltid-services/waltid-service-commons-test/src/main/kotlin/id/walt/commons/testing/TestTls.kt

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import java.net.InetAddress
1010
import java.security.KeyPair
1111
import java.security.KeyStore
1212
import java.security.PrivateKey
13-
import java.security.cert.CertificateException
1413
import java.security.cert.X509Certificate
14+
import java.util.UUID
1515
import javax.net.ssl.SSLContext
1616
import javax.net.ssl.TrustManagerFactory
1717
import javax.net.ssl.X509TrustManager
@@ -27,7 +27,7 @@ import javax.security.auth.x500.X500Principal
2727
object TestTls {
2828
const val SERVER_ALIAS = "waltid-test-server"
2929
private const val CA_ALIAS = "waltid-test-ca"
30-
private const val PASSWORD = "waltid-test-only"
30+
private val keyStorePassword = UUID.randomUUID().toString()
3131

3232
private data class Material(
3333
val serverKeyStore: KeyStore,
@@ -41,7 +41,7 @@ object TestTls {
4141

4242
fun caCertificateDer(): ByteArray = material.caCertificate.encoded
4343

44-
fun password(): CharArray = PASSWORD.toCharArray()
44+
fun password(): CharArray = keyStorePassword.toCharArray()
4545

4646
@Volatile
4747
private var installed = false
@@ -52,33 +52,28 @@ object TestTls {
5252
if (installed) return
5353

5454
val systemTrustManager = defaultTrustManager(null)
55-
val testTrustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
55+
val combinedTrustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
5656
load(null, null)
57+
systemTrustManager.acceptedIssuers.forEachIndexed { index, certificate ->
58+
setCertificateEntry("system-$index", certificate)
59+
}
5760
setCertificateEntry(CA_ALIAS, material.caCertificate)
5861
}
59-
val testTrustManager = defaultTrustManager(testTrustStore)
60-
val compositeTrustManager = CompositeTrustManager(systemTrustManager, testTrustManager)
62+
val combinedTrustManager = defaultTrustManager(combinedTrustStore)
6163

62-
SSLContext.getInstance("TLS").apply {
63-
init(null, arrayOf(compositeTrustManager), null)
64+
SSLContext.getInstance("TLSv1.3").apply {
65+
init(null, arrayOf(combinedTrustManager), null)
6466
SSLContext.setDefault(this)
6567
}
6668

6769
// CIO obtains its default trust manager from the JSSE trust store rather than SSLContext.
68-
// Persist a combined store before any test client engine is initialized.
69-
val combinedTrustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
70-
load(null, null)
71-
systemTrustManager.acceptedIssuers.forEachIndexed { index, certificate ->
72-
setCertificateEntry("system-$index", certificate)
73-
}
74-
setCertificateEntry(CA_ALIAS, material.caCertificate)
75-
}
70+
// Persist the same combined store before any test client engine is initialized.
7671
val trustStoreFile = kotlin.io.path.createTempFile("waltid-test-trust-", ".p12").toFile().apply {
7772
deleteOnExit()
7873
outputStream().use { combinedTrustStore.store(it, password()) }
7974
}
8075
System.setProperty("javax.net.ssl.trustStore", trustStoreFile.absolutePath)
81-
System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD)
76+
System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword)
8277
System.setProperty("javax.net.ssl.trustStoreType", combinedTrustStore.type)
8378

8479
installed = true
@@ -87,7 +82,7 @@ object TestTls {
8782
private fun generateMaterial(): Material {
8883
val caKeyStore = buildKeyStore {
8984
certificate(CA_ALIAS) {
90-
password = PASSWORD
85+
password = keyStorePassword
9186
keyType = KeyType.CA
9287
hash = HashAlgorithm.SHA256
9388
sign = SignatureAlgorithm.RSA
@@ -103,7 +98,7 @@ object TestTls {
10398
)
10499
val serverKeyStore = buildKeyStore {
105100
certificate(SERVER_ALIAS) {
106-
password = PASSWORD
101+
password = keyStorePassword
107102
keyType = KeyType.Server
108103
hash = HashAlgorithm.SHA256
109104
sign = SignatureAlgorithm.RSA
@@ -122,37 +117,6 @@ object TestTls {
122117
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply {
123118
init(keyStore)
124119
}.trustManagers.filterIsInstance<X509TrustManager>().single()
125-
126-
private class CompositeTrustManager(
127-
private val system: X509TrustManager,
128-
private val test: X509TrustManager,
129-
) : X509TrustManager {
130-
override fun checkClientTrusted(chain: Array<out X509Certificate>, authType: String) =
131-
checkTrusted(chain, authType, X509TrustManager::checkClientTrusted)
132-
133-
override fun checkServerTrusted(chain: Array<out X509Certificate>, authType: String) =
134-
checkTrusted(chain, authType, X509TrustManager::checkServerTrusted)
135-
136-
private fun checkTrusted(
137-
chain: Array<out X509Certificate>,
138-
authType: String,
139-
check: X509TrustManager.(Array<out X509Certificate>, String) -> Unit,
140-
) {
141-
try {
142-
system.check(chain, authType)
143-
} catch (systemFailure: CertificateException) {
144-
try {
145-
test.check(chain, authType)
146-
} catch (testFailure: CertificateException) {
147-
testFailure.addSuppressed(systemFailure)
148-
throw testFailure
149-
}
150-
}
151-
}
152-
153-
override fun getAcceptedIssuers(): Array<X509Certificate> =
154-
system.acceptedIssuers + test.acceptedIssuers
155-
}
156120
}
157121

158122
/** Adds a server connector backed by [TestTls]'s CA-signed certificate. */

0 commit comments

Comments
 (0)