-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Handle ECH Retry #9611
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
base: master
Are you sure you want to change the base?
Handle ECH Retry #9611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import java.net.Socket as JavaNetSocket | |
| import java.net.UnknownServiceException | ||
| import java.security.cert.X509Certificate | ||
| import java.util.concurrent.TimeUnit | ||
| import javax.net.ssl.SSLException | ||
| import javax.net.ssl.SSLPeerUnverifiedException | ||
| import javax.net.ssl.SSLSocket | ||
| import okhttp3.CertificatePinner | ||
|
|
@@ -38,6 +39,7 @@ import okhttp3.internal.closeQuietly | |
| import okhttp3.internal.concurrent.TaskRunner | ||
| import okhttp3.internal.concurrent.withLock | ||
| import okhttp3.internal.connection.RoutePlanner.ConnectResult | ||
| import okhttp3.internal.dns.EchRetryConfig | ||
| import okhttp3.internal.http.ExchangeCodec | ||
| import okhttp3.internal.http1.Http1ExchangeCodec | ||
| import okhttp3.internal.platform.Platform | ||
|
|
@@ -73,6 +75,7 @@ class ConnectPlan internal constructor( | |
| private val tunnelRequest: Request?, | ||
| internal val connectionSpecIndex: Int, | ||
| internal val isTlsFallback: Boolean, | ||
| private val echRetryConfig: EchRetryConfig? = null, | ||
| ) : RoutePlanner.Plan, | ||
| ExchangeCodec.Carrier { | ||
| /** True if this connect was canceled; typically because it lost a race. */ | ||
|
|
@@ -98,10 +101,12 @@ class ConnectPlan internal constructor( | |
| get() = protocol != null | ||
|
|
||
| private fun copy( | ||
| route: Route = this.route, | ||
| attempt: Int = this.attempt, | ||
| tunnelRequest: Request? = this.tunnelRequest, | ||
| connectionSpecIndex: Int = this.connectionSpecIndex, | ||
| isTlsFallback: Boolean = this.isTlsFallback, | ||
| echRetryConfig: EchRetryConfig? = this.echRetryConfig, | ||
| ): ConnectPlan = | ||
| ConnectPlan( | ||
| taskRunner = taskRunner, | ||
|
|
@@ -120,6 +125,7 @@ class ConnectPlan internal constructor( | |
| tunnelRequest = tunnelRequest, | ||
| connectionSpecIndex = connectionSpecIndex, | ||
| isTlsFallback = isTlsFallback, | ||
| echRetryConfig = echRetryConfig, | ||
| ) | ||
|
|
||
| override fun connectTcp(): ConnectResult { | ||
|
|
@@ -161,6 +167,7 @@ class ConnectPlan internal constructor( | |
| check(!isReady) { "already connected" } | ||
|
|
||
| val connectionSpecs = route.address.connectionSpecs | ||
| var offeredEchRetryConfig: EchRetryConfig? = null | ||
| var retryTlsConnection: ConnectPlan? = null | ||
| var success = false | ||
|
|
||
|
|
@@ -204,7 +211,21 @@ class ConnectPlan internal constructor( | |
| retryTlsConnection = tlsEquipPlan.nextConnectionSpec(connectionSpecs, sslSocket) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we make
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even better, maybe |
||
|
|
||
| connectionSpec.apply(sslSocket, isFallback = tlsEquipPlan.isTlsFallback) | ||
| connectTls(sslSocket, connectionSpec) | ||
| try { | ||
| connectTls(sslSocket, connectionSpec) | ||
| } catch (e: SSLException) { | ||
| val echRetryConfig = Platform.get().getEchRetryConfig(e) | ||
| if ( | ||
| echRetryConfig != null && | ||
| route.address.hostnameVerifier!!.verify( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This surprises me. Is there an attack that this defends against? |
||
| echRetryConfig.publicHostname, | ||
| sslSocket.session, | ||
| ) | ||
| ) { | ||
| offeredEchRetryConfig = echRetryConfig | ||
| } | ||
| throw e | ||
| } | ||
| call.eventListener.secureConnectEnd(call, handshake) | ||
| } else { | ||
| javaNetSocket = rawSocket | ||
|
|
@@ -239,9 +260,37 @@ class ConnectPlan internal constructor( | |
| call.eventListener.connectFailed(call, route.socketAddress, route.proxy, null, e) | ||
| connectionPool.connectionListener.connectFailed(route, call, e) | ||
|
|
||
| if (!retryOnConnectionFailure || !retryTlsHandshake(e)) { | ||
| retryTlsConnection = null | ||
| } | ||
| retryTlsConnection = | ||
| when { | ||
| echRetryConfig == null && offeredEchRetryConfig != null -> { | ||
| // TODO: Should an ECH retry honor retryOnConnectionFailure? | ||
| // Typically Conscrypt throwing EchConfigMismatchException | ||
| copy( | ||
| route = | ||
| Route( | ||
| address = route.address, | ||
| proxy = route.proxy, | ||
| socketAddress = route.socketAddress, | ||
| echConfigList = offeredEchRetryConfig.configList, | ||
| ), | ||
| echRetryConfig = offeredEchRetryConfig, | ||
| ) | ||
| } | ||
|
|
||
| echRetryConfig != null && offeredEchRetryConfig != null -> { | ||
| // TODO: Should we treat an untrusted or missing ECH retry config as an ordinary | ||
| // SSLException and try another connection spec? | ||
|
Comment on lines
+281
to
+282
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that if we have ECH information, we shouldn’t let ourselves be downgraded. |
||
| null | ||
| } | ||
|
|
||
| retryOnConnectionFailure && retryTlsHandshake(e) -> { | ||
| retryTlsConnection | ||
| } | ||
|
|
||
| else -> { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| return ConnectResult( | ||
| plan = this, | ||
|
|
@@ -561,6 +610,7 @@ class ConnectPlan internal constructor( | |
| tunnelRequest = tunnelRequest, | ||
| connectionSpecIndex = connectionSpecIndex, | ||
| isTlsFallback = isTlsFallback, | ||
| echRetryConfig = echRetryConfig, | ||
| ) | ||
|
|
||
| fun closeQuietly() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,11 @@ data class DnsMessage( | |
| } | ||
| } | ||
|
|
||
| internal data class EchRetryConfig( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to its own file |
||
| val configList: ByteString, | ||
| val publicHostname: String, | ||
| ) | ||
|
|
||
| @OkHttpInternalApi | ||
| data class Question( | ||
| val name: String, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think I got think backwards, re-reading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you should only be verifying the public hostname against the certificate, not against the request. Conscrypt isn't able to do the hostname validation itself, which is why it's necessary to do it here in OkHttp.
FYI @tweksteen