Skip to content

Commit 7b156cb

Browse files
committed
refactor: remove unnecessary SNI manipulation from SSL socket factory
In our codebase we currently have two layers of custom logic: - one that alters the SNI in the ClientHello (via a custom SSLSocketFactory) - another that compares an alternate hostname against the SAN entries during client-side certificate verification. This work was done for one of Coder's clients that wants to do auth via certificates instead of API tokens. After recent discussions it turns out the SNI manipulation is not needed, we only need to do custom certificate validation.
1 parent b1a32ff commit 7b156cb

File tree

3 files changed

+8
-328
lines changed

3 files changed

+8
-328
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## Unreleased
66

7+
### Changed
8+
9+
- simplified TLS configuration
10+
711
## 2.22.3 - 2025-09-19
812

913
### Fixed

src/main/kotlin/com/coder/gateway/util/TLS.kt

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import okhttp3.internal.tls.OkHostnameVerifier
55
import org.slf4j.LoggerFactory
66
import java.io.File
77
import java.io.FileInputStream
8-
import java.net.IDN
9-
import java.net.InetAddress
10-
import java.net.Socket
11-
import java.nio.charset.StandardCharsets
128
import java.security.KeyFactory
139
import java.security.KeyStore
1410
import java.security.cert.CertificateException
@@ -21,12 +17,9 @@ import java.util.Locale
2117
import javax.net.ssl.HostnameVerifier
2218
import javax.net.ssl.KeyManager
2319
import javax.net.ssl.KeyManagerFactory
24-
import javax.net.ssl.SNIServerName
2520
import javax.net.ssl.SSLContext
2621
import javax.net.ssl.SSLSession
27-
import javax.net.ssl.SSLSocket
2822
import javax.net.ssl.SSLSocketFactory
29-
import javax.net.ssl.StandardConstants
3023
import javax.net.ssl.TrustManager
3124
import javax.net.ssl.TrustManagerFactory
3225
import javax.net.ssl.X509TrustManager
@@ -60,7 +53,7 @@ fun sslContextFromPEMs(
6053
val kf = KeyFactory.getInstance("RSA")
6154
val keySpec = PKCS8EncodedKeySpec(pemBytes)
6255
kf.generatePrivate(keySpec)
63-
} catch (e: InvalidKeySpecException) {
56+
} catch (_: InvalidKeySpecException) {
6457
val kf = KeyFactory.getInstance("EC")
6558
val keySpec = PKCS8EncodedKeySpec(pemBytes)
6659
kf.generatePrivate(keySpec)
@@ -87,11 +80,7 @@ fun sslContextFromPEMs(
8780

8881
fun coderSocketFactory(settings: CoderTLSSettings): SSLSocketFactory {
8982
val sslContext = sslContextFromPEMs(settings.certPath, settings.keyPath, settings.caPath)
90-
if (settings.altHostname.isBlank()) {
91-
return sslContext.socketFactory
92-
}
93-
94-
return AlternateNameSSLSocketFactory(sslContext.socketFactory, settings.altHostname)
83+
return sslContext.socketFactory
9584
}
9685

9786
fun coderTrustManagers(tlsCAPath: String): Array<TrustManager> {
@@ -115,82 +104,6 @@ fun coderTrustManagers(tlsCAPath: String): Array<TrustManager> {
115104
return trustManagerFactory.trustManagers.map { MergedSystemTrustManger(it as X509TrustManager) }.toTypedArray()
116105
}
117106

118-
class AlternateNameSSLSocketFactory(private val delegate: SSLSocketFactory, private val alternateName: String) :
119-
SSLSocketFactory() {
120-
override fun getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites
121-
122-
override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites
123-
124-
override fun createSocket(): Socket {
125-
val socket = delegate.createSocket() as SSLSocket
126-
customizeSocket(socket)
127-
return socket
128-
}
129-
130-
override fun createSocket(
131-
host: String?,
132-
port: Int,
133-
): Socket {
134-
val socket = delegate.createSocket(host, port) as SSLSocket
135-
customizeSocket(socket)
136-
return socket
137-
}
138-
139-
override fun createSocket(
140-
host: String?,
141-
port: Int,
142-
localHost: InetAddress?,
143-
localPort: Int,
144-
): Socket {
145-
val socket = delegate.createSocket(host, port, localHost, localPort) as SSLSocket
146-
customizeSocket(socket)
147-
return socket
148-
}
149-
150-
override fun createSocket(
151-
host: InetAddress?,
152-
port: Int,
153-
): Socket {
154-
val socket = delegate.createSocket(host, port) as SSLSocket
155-
customizeSocket(socket)
156-
return socket
157-
}
158-
159-
override fun createSocket(
160-
address: InetAddress?,
161-
port: Int,
162-
localAddress: InetAddress?,
163-
localPort: Int,
164-
): Socket {
165-
val socket = delegate.createSocket(address, port, localAddress, localPort) as SSLSocket
166-
customizeSocket(socket)
167-
return socket
168-
}
169-
170-
override fun createSocket(
171-
s: Socket?,
172-
host: String?,
173-
port: Int,
174-
autoClose: Boolean,
175-
): Socket {
176-
val socket = delegate.createSocket(s, host, port, autoClose) as SSLSocket
177-
customizeSocket(socket)
178-
return socket
179-
}
180-
181-
private fun customizeSocket(socket: SSLSocket) {
182-
val params = socket.sslParameters
183-
184-
params.serverNames = listOf(RelaxedSNIHostname(alternateName))
185-
socket.sslParameters = params
186-
}
187-
}
188-
189-
private class RelaxedSNIHostname(hostname: String) : SNIServerName(
190-
StandardConstants.SNI_HOST_NAME,
191-
IDN.toASCII(hostname, 0).toByteArray(StandardCharsets.UTF_8)
192-
)
193-
194107
class CoderHostnameVerifier(private val alternateName: String) : HostnameVerifier {
195108
private val logger = LoggerFactory.getLogger(javaClass)
196109

@@ -238,7 +151,7 @@ class MergedSystemTrustManger(private val otherTrustManager: X509TrustManager) :
238151
) {
239152
try {
240153
otherTrustManager.checkClientTrusted(chain, authType)
241-
} catch (e: CertificateException) {
154+
} catch (_: CertificateException) {
242155
systemTrustManager.checkClientTrusted(chain, authType)
243156
}
244157
}
@@ -249,7 +162,7 @@ class MergedSystemTrustManger(private val otherTrustManager: X509TrustManager) :
249162
) {
250163
try {
251164
otherTrustManager.checkServerTrusted(chain, authType)
252-
} catch (e: CertificateException) {
165+
} catch (_: CertificateException) {
253166
systemTrustManager.checkServerTrusted(chain, authType)
254167
}
255168
}

src/test/kotlin/com/coder/gateway/util/AlternateNameSSLSocketFactoryTest.kt

Lines changed: 0 additions & 237 deletions
This file was deleted.

0 commit comments

Comments
 (0)