Repository navigation
Expand file tree
/
Copy pathPlatform.kt
More file actions
308 lines (265 loc) · 9.77 KB
/
Copy pathPlatform.kt
File metadata and controls
308 lines (265 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2012 Square, Inc.
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.internal.platform
import android.annotation.SuppressLint
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import java.security.GeneralSecurityException
import java.security.KeyStore
import java.security.Security
import java.util.logging.Level
import java.util.logging.Logger
import javax.net.ssl.ExtendedSSLSession
import javax.net.ssl.SNIHostName
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.internal.platform.android.AndroidLog
import okhttp3.internal.readFieldOrNull
import okhttp3.internal.tls.BasicCertificateChainCleaner
import okhttp3.internal.tls.BasicTrustRootIndex
import okhttp3.internal.tls.CertificateChainCleaner
import okhttp3.internal.tls.TrustRootIndex
import okio.Buffer
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
/**
* Access to platform-specific features.
*
* ### Session Tickets
*
* Supported on Android 2.3+.
* Supported on JDK 8+ via Conscrypt.
*
* ### ALPN (Application Layer Protocol Negotiation)
*
* Supported on Android 5.0+.
*
* Supported on OpenJDK 8 via the JettyALPN-boot library or Conscrypt.
*
* Supported on OpenJDK 9+ via SSLParameters and SSLSocket features.
*
* ### Trust Manager Extraction
*
* Supported on Android 2.3+ and OpenJDK 7+. There are no public APIs to recover the trust
* manager that was used to create an [SSLSocketFactory].
*
* Not supported by choice on JDK9+ due to access checks.
*
* ### Android Cleartext Permit Detection
*
* Supported on Android 6.0+ via `NetworkSecurityPolicy`.
*/
open class Platform {
/** Prefix used on custom headers. */
fun getPrefix() = "OkHttp"
open fun newSSLContext(): SSLContext = SSLContext.getInstance("TLS")
open fun platformTrustManager(): X509TrustManager {
val factory =
TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm(),
)
factory.init(null as KeyStore?)
val trustManagers = factory.trustManagers!!
check(trustManagers.size == 1 && trustManagers[0] is X509TrustManager) {
"Unexpected default trust managers: ${trustManagers.contentToString()}"
}
return trustManagers[0] as X509TrustManager
}
open fun trustManager(sslSocketFactory: SSLSocketFactory): X509TrustManager? {
return try {
// Attempt to get the trust manager from an OpenJDK socket factory. We attempt this on all
// platforms in order to support Robolectric, which mixes classes from both Android and the
// Oracle JDK. Note that we don't support HTTP/2 or other nice features on Robolectric.
val sslContextClass = Class.forName("sun.security.ssl.SSLContextImpl")
val context = readFieldOrNull(sslSocketFactory, sslContextClass, "context") ?: return null
readFieldOrNull(context, X509TrustManager::class.java, "trustManager")
} catch (e: ClassNotFoundException) {
null
} catch (e: RuntimeException) {
// Throws InaccessibleObjectException (added in JDK9) on JDK 17 due to
// JEP 403 Strongly Encapsulate JDK Internals.
if (e.javaClass.name != "java.lang.reflect.InaccessibleObjectException") {
throw e
}
null
}
}
/**
* Configure TLS extensions on `sslSocket` for `route`.
*/
open fun configureTlsExtensions(
sslSocket: SSLSocket,
hostname: String?,
protocols: List<@JvmSuppressWildcards Protocol>,
) {
}
/** Called after the TLS handshake to release resources allocated by [configureTlsExtensions]. */
open fun afterHandshake(sslSocket: SSLSocket) {
}
/** Returns the negotiated protocol, or null if no protocol was negotiated. */
open fun getSelectedProtocol(sslSocket: SSLSocket): String? = null
/** For MockWebServer. This returns the inbound SNI names. */
@SuppressLint("NewApi")
@IgnoreJRERequirement // This function is overridden to require API >= 24.
open fun getHandshakeServerNames(sslSocket: SSLSocket): List<String> {
val session = sslSocket.session as? ExtendedSSLSession ?: return listOf()
return session.requestedServerNames.mapNotNull { (it as? SNIHostName)?.asciiName }
}
@Throws(IOException::class)
open fun connectSocket(
socket: Socket,
address: InetSocketAddress,
connectTimeout: Int,
) {
socket.connect(address, connectTimeout)
}
open fun log(
message: String,
level: Int = INFO,
t: Throwable? = null,
) {
val logLevel = if (level == WARN) Level.WARNING else Level.INFO
logger.log(logLevel, message, t)
}
open fun isCleartextTrafficPermitted(hostname: String): Boolean = true
/**
* Returns an object that holds a stack trace created at the moment this method is executed. This
* should be used specifically for [java.io.Closeable] objects and in conjunction with
* [logCloseableLeak].
*/
open fun getStackTraceForCloseable(closer: String): Any? {
return when {
logger.isLoggable(Level.FINE) -> Throwable(closer) // These are expensive to allocate.
else -> null
}
}
open fun logCloseableLeak(
message: String,
stackTrace: Any?,
) {
var logMessage = message
if (stackTrace == null) {
logMessage += " To see where this was allocated, set the OkHttpClient logger level to " +
"FINE: Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);"
}
log(logMessage, WARN, stackTrace as Throwable?)
}
open fun buildCertificateChainCleaner(trustManager: X509TrustManager): CertificateChainCleaner =
BasicCertificateChainCleaner(buildTrustRootIndex(trustManager))
open fun buildTrustRootIndex(trustManager: X509TrustManager): TrustRootIndex = BasicTrustRootIndex(*trustManager.acceptedIssuers)
open fun newSslSocketFactory(trustManager: X509TrustManager): SSLSocketFactory {
try {
return newSSLContext().apply {
init(null, arrayOf<TrustManager>(trustManager), null)
}.socketFactory
} catch (e: GeneralSecurityException) {
throw AssertionError("No System TLS: $e", e) // The system has no TLS. Just give up.
}
}
override fun toString(): String = javaClass.simpleName
companion object {
@Volatile private var platform = findPlatform()
const val INFO = 4
const val WARN = 5
private val logger = Logger.getLogger(OkHttpClient::class.java.name)
@JvmStatic
fun get(): Platform = platform
fun resetForTests(platform: Platform = findPlatform()) {
this.platform = platform
}
fun alpnProtocolNames(protocols: List<Protocol>) = protocols.filter { it != Protocol.HTTP_1_0 }.map { it.toString() }
// This explicit check avoids activating in Android Studio with Android specific classes
// available when running plugins inside the IDE.
val isAndroid: Boolean
get() = "Dalvik" == System.getProperty("java.vm.name")
private val isConscryptPreferred: Boolean
get() {
val preferredProvider = Security.getProviders()[0].name
return "Conscrypt" == preferredProvider
}
private val isOpenJSSEPreferred: Boolean
get() {
val preferredProvider = Security.getProviders()[0].name
return "OpenJSSE" == preferredProvider
}
private val isBouncyCastlePreferred: Boolean
get() {
val preferredProvider = Security.getProviders()[0].name
return "BC" == preferredProvider
}
/** Attempt to match the host runtime to a capable Platform implementation. */
private fun findPlatform(): Platform =
if (isAndroid) {
findAndroidPlatform()
} else {
findJvmPlatform()
}
private fun findAndroidPlatform(): Platform {
AndroidLog.enable()
return Android10Platform.buildIfSupported() ?: AndroidPlatform.buildIfSupported()!!
}
private fun findJvmPlatform(): Platform {
if (isConscryptPreferred) {
val conscrypt = ConscryptPlatform.buildIfSupported()
if (conscrypt != null) {
return conscrypt
}
}
if (isBouncyCastlePreferred) {
val bc = BouncyCastlePlatform.buildIfSupported()
if (bc != null) {
return bc
}
}
if (isOpenJSSEPreferred) {
val openJSSE = OpenJSSEPlatform.buildIfSupported()
if (openJSSE != null) {
return openJSSE
}
}
// An Oracle JDK 9 like OpenJDK, or JDK 8 251+.
val jdk9 = Jdk9Platform.buildIfSupported()
if (jdk9 != null) {
return jdk9
}
// An Oracle JDK 8 like OpenJDK, pre 251.
val jdkWithJettyBoot = Jdk8WithJettyBootPlatform.buildIfSupported()
if (jdkWithJettyBoot != null) {
return jdkWithJettyBoot
}
return Platform()
}
/**
* Returns the concatenation of 8-bit, length prefixed protocol names.
* http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
*/
fun concatLengthPrefixed(protocols: List<Protocol>): ByteArray {
val result = Buffer()
for (protocol in alpnProtocolNames(protocols)) {
result.writeByte(protocol.length)
result.writeUtf8(protocol)
}
return result.readByteArray()
}
}
}