@tokamakdev/platform-android-arm64 0.1.1-beta.139 → 0.1.1-beta.141
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android-arm64/build/entrypoint +51 -1
- package/android-arm64/lib/TokamakRuntime/libtokamak.a +2 -2
- package/android-arm64/native-shell/app/TokamakActivity.kt +2 -40
- package/android-arm64/native-shell/app/TokamakApplication.kt +20 -2
- package/android-arm64/native-shell/app/TokamakProxy.kt +25 -53
- package/android-arm64/native-shell/app/TokamakRuntime.kt +35 -19
- package/android-arm64/native-shell/app/TokamakWebViewClient.kt +4 -25
- package/package.json +1 -1
|
@@ -249,6 +249,55 @@ release_signing() {
|
|
|
249
249
|
EOF
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
+
# The gateway's server certificate for the app host, with its key, in
|
|
253
|
+
# assets/tokamak-gateway.p12. The network security config trusts the
|
|
254
|
+
# certificate's authority for the app host alone, so the WebView verifies the
|
|
255
|
+
# gateway itself. Each build creates its own authority and discards the
|
|
256
|
+
# authority's key. The gateway's key ships in the app, which is safe only while
|
|
257
|
+
# the WebView sends the app host's traffic to no listener but the gateway. The
|
|
258
|
+
# keystore's password is public, so the keystore uses the algorithms every
|
|
259
|
+
# Android version reads, at one iteration, and the certificates outlive any app.
|
|
260
|
+
gateway_identity() {
|
|
261
|
+
local keytool=keytool
|
|
262
|
+
if [[ -n "${JAVA_HOME:-}" ]]; then
|
|
263
|
+
keytool=$JAVA_HOME/bin/keytool
|
|
264
|
+
fi
|
|
265
|
+
if ! command -v "$keytool" > /dev/null; then
|
|
266
|
+
echo "Android builds create the gateway certificate with keytool, which the JDK provides" >&2
|
|
267
|
+
exit 69
|
|
268
|
+
fi
|
|
269
|
+
local resources=$app/src/main/res
|
|
270
|
+
local keystore=(
|
|
271
|
+
"$keytool"
|
|
272
|
+
-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE
|
|
273
|
+
-J-Dkeystore.pkcs12.keyProtectionAlgorithm=PBEWithSHA1AndDESede
|
|
274
|
+
-J-Dkeystore.pkcs12.macAlgorithm=HmacPBESHA1
|
|
275
|
+
-J-Dkeystore.pkcs12.keyPbeIterationCount=1
|
|
276
|
+
-J-Dkeystore.pkcs12.macIterationCount=1
|
|
277
|
+
-keystore "$app/src/main/assets/tokamak-gateway.p12" -storetype PKCS12 -storepass tokamak
|
|
278
|
+
)
|
|
279
|
+
local key=(-keyalg EC -groupname secp256r1 -sigalg SHA256withECDSA -validity 36500)
|
|
280
|
+
mkdir -p "$resources/raw" "$resources/xml"
|
|
281
|
+
"${keystore[@]}" -genkeypair -alias authority "${key[@]}" -dname 'CN=tokamak gateway' \
|
|
282
|
+
-ext bc:c=ca:true,pathlen:0
|
|
283
|
+
"${keystore[@]}" -genkeypair -alias gateway "${key[@]}" -dname "CN=$host" -signer authority \
|
|
284
|
+
-ext "san=dns:$host" -ext eku=serverAuth -ext ku:c=digitalSignature
|
|
285
|
+
"${keystore[@]}" -exportcert -alias authority -rfc -file "$resources/raw/tokamak_gateway_authority.pem"
|
|
286
|
+
"${keystore[@]}" -delete -alias authority
|
|
287
|
+
cat > "$resources/xml/tokamak_network_security_config.xml" <<EOF
|
|
288
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
289
|
+
<network-security-config>
|
|
290
|
+
<base-config cleartextTrafficPermitted="false" />
|
|
291
|
+
<domain-config>
|
|
292
|
+
<domain includeSubdomains="false">$(xml_escape "$host")</domain>
|
|
293
|
+
<trust-anchors>
|
|
294
|
+
<certificates src="@raw/tokamak_gateway_authority" />
|
|
295
|
+
</trust-anchors>
|
|
296
|
+
</domain-config>
|
|
297
|
+
</network-security-config>
|
|
298
|
+
EOF
|
|
299
|
+
}
|
|
300
|
+
|
|
252
301
|
xml_escape() {
|
|
253
302
|
printf '%s' "$1" |
|
|
254
303
|
sed -e 's/&/\&/g' \
|
|
@@ -290,6 +339,7 @@ if [[ -n "$icon" ]]; then
|
|
|
290
339
|
fi
|
|
291
340
|
has_icon=true
|
|
292
341
|
fi
|
|
342
|
+
gateway_identity
|
|
293
343
|
|
|
294
344
|
# Each plugin is a library module, so its manifest merges below the app's and
|
|
295
345
|
# its dependencies stay its own. Its class is a Kotlin class name in a package,
|
|
@@ -503,7 +553,7 @@ manifest=$app/src/main/AndroidManifest.xml
|
|
|
503
553
|
if [[ "$has_icon" == true ]]; then
|
|
504
554
|
icon_attribute=' android:icon="@mipmap/ic_launcher"'
|
|
505
555
|
fi
|
|
506
|
-
printf ' <application android:name=".TokamakApplication" android:label="%s"%s android:
|
|
556
|
+
printf ' <application android:name=".TokamakApplication" android:label="%s"%s android:networkSecurityConfig="@xml/tokamak_network_security_config">\n' "$(xml_escape "$app_name")" "$icon_attribute"
|
|
507
557
|
printf ' <meta-data android:name="tokamak.host" android:value="%s" />\n' "$host"
|
|
508
558
|
if [[ -n "$dev_endpoint" ]]; then
|
|
509
559
|
printf ' <meta-data android:name="tokamak.dev.endpoint" android:value="%s" />\n' "$dev_endpoint"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
2
|
name: package/android-arm64/lib/TokamakRuntime/libtokamak.a
|
|
3
|
-
size:
|
|
4
|
-
sha256:
|
|
3
|
+
size: 35439000 bytes
|
|
4
|
+
sha256: 691f1506c430b96330b354c89bc13d8a0ed38c77848708cc6b3f6e885b911150
|
|
@@ -23,9 +23,6 @@ class TokamakActivity : Activity() {
|
|
|
23
23
|
get() = application as TokamakApplication
|
|
24
24
|
internal lateinit var webView: WebView
|
|
25
25
|
private set
|
|
26
|
-
private var runtime: TokamakRuntime? = null
|
|
27
|
-
private var proxyPort: Int? = null
|
|
28
|
-
private var restoreGeneration = 0L
|
|
29
26
|
private var destroyed = false
|
|
30
27
|
|
|
31
28
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
@@ -54,19 +51,10 @@ class TokamakActivity : Activity() {
|
|
|
54
51
|
deliver(intent)
|
|
55
52
|
}
|
|
56
53
|
|
|
57
|
-
override fun onResume() {
|
|
58
|
-
super.onResume()
|
|
59
|
-
restoreGeneration += 1
|
|
60
|
-
val generation = restoreGeneration
|
|
61
|
-
runtime?.let { restoreGateway(it, generation) }
|
|
62
|
-
}
|
|
63
|
-
|
|
64
54
|
override fun onDestroy() {
|
|
65
55
|
destroyed = true
|
|
66
|
-
restoreGeneration += 1
|
|
67
56
|
if (tokamak.activity === this) tokamak.activity = null
|
|
68
57
|
if (isFinishing) tokamak.permissionRequests.cancel()
|
|
69
|
-
TokamakProxy.release(this)
|
|
70
58
|
webView.stopLoading()
|
|
71
59
|
webView.destroy()
|
|
72
60
|
super.onDestroy()
|
|
@@ -97,37 +85,11 @@ class TokamakActivity : Activity() {
|
|
|
97
85
|
show(FAILED)
|
|
98
86
|
return
|
|
99
87
|
}
|
|
100
|
-
this.runtime
|
|
101
|
-
proxyPort = runtime.port
|
|
102
|
-
TokamakProxy.acquire(this, tokamak.appHost, runtime.port)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private fun restoreGateway(runtime: TokamakRuntime, generation: Long) {
|
|
106
|
-
Thread {
|
|
107
|
-
val result = runCatching { runtime.restoreGateway() }
|
|
108
|
-
runOnUiThread {
|
|
109
|
-
if (
|
|
110
|
-
destroyed ||
|
|
111
|
-
this.runtime !== runtime ||
|
|
112
|
-
generation != restoreGeneration
|
|
113
|
-
) return@runOnUiThread
|
|
114
|
-
result
|
|
115
|
-
.onSuccess { port ->
|
|
116
|
-
if (port == runtime.port && port != proxyPort) {
|
|
117
|
-
proxyPort = port
|
|
118
|
-
TokamakProxy.acquire(this, tokamak.appHost, port)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
.onFailure { error ->
|
|
122
|
-
Log.w(TAG, "tokamak gateway could not recover", error)
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}.start()
|
|
88
|
+
TokamakProxy.route(this, tokamak.appHost, runtime.port) { load(runtime) }
|
|
126
89
|
}
|
|
127
90
|
|
|
128
|
-
|
|
91
|
+
private fun load(runtime: TokamakRuntime) {
|
|
129
92
|
if (destroyed) return
|
|
130
|
-
val runtime = runtime ?: return
|
|
131
93
|
webView.webViewClient =
|
|
132
94
|
TokamakWebViewClient(this, tokamak.appHost, runtime)
|
|
133
95
|
webView.loadUrl("https://${tokamak.appHost}/")
|
|
@@ -11,6 +11,7 @@ import androidx.lifecycle.Lifecycle
|
|
|
11
11
|
import androidx.lifecycle.LifecycleOwner
|
|
12
12
|
import androidx.lifecycle.ProcessLifecycleOwner
|
|
13
13
|
import java.io.File
|
|
14
|
+
import java.security.KeyStore
|
|
14
15
|
import java.util.concurrent.Executors
|
|
15
16
|
import kotlin.reflect.KClass
|
|
16
17
|
|
|
@@ -21,6 +22,12 @@ private const val DEV_SESSION_TOKEN_METADATA = "tokamak.dev.session-token"
|
|
|
21
22
|
/** How long the Worker has for `resume` and `suspend`. */
|
|
22
23
|
private const val LIFECYCLE_DEADLINE_MILLIS = 10_000L
|
|
23
24
|
|
|
25
|
+
/** The asset holding the gateway's server certificate and key, which the build creates. */
|
|
26
|
+
private const val GATEWAY_KEYSTORE = "tokamak-gateway.p12"
|
|
27
|
+
|
|
28
|
+
/** The gateway keystore's password, which is public: the key ships in the app. */
|
|
29
|
+
private const val GATEWAY_KEYSTORE_PASSWORD = "tokamak"
|
|
30
|
+
|
|
24
31
|
/**
|
|
25
32
|
* The tokamak process. Owns the runtime and native plugins, which outlive any activity, so a
|
|
26
33
|
* plugin can run Worker code when the system starts the app in the background.
|
|
@@ -194,13 +201,16 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
194
201
|
val endpoint = metadata?.getString(DEV_ENDPOINT_METADATA)
|
|
195
202
|
val sessionToken = metadata?.getString(DEV_SESSION_TOKEN_METADATA)
|
|
196
203
|
val startsInForeground = activity != null
|
|
204
|
+
val server = gatewayIdentity()
|
|
197
205
|
val runtime =
|
|
198
206
|
if (!endpoint.isNullOrEmpty() && !sessionToken.isNullOrEmpty()) {
|
|
199
207
|
TokamakRuntime.startDevelopment(
|
|
200
|
-
stateDir(), appHost, endpoint, sessionToken, startsInForeground, workerPlugins,
|
|
208
|
+
stateDir(), appHost, endpoint, sessionToken, server, startsInForeground, workerPlugins,
|
|
201
209
|
)
|
|
202
210
|
} else {
|
|
203
|
-
TokamakRuntime.start(
|
|
211
|
+
TokamakRuntime.start(
|
|
212
|
+
unpackApp(), stateDir(), storageDir(), appHost, server, startsInForeground, workerPlugins,
|
|
213
|
+
)
|
|
204
214
|
}
|
|
205
215
|
mainExecutor.execute {
|
|
206
216
|
record(runtime, ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
|
|
@@ -208,6 +218,14 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
208
218
|
return runtime
|
|
209
219
|
}
|
|
210
220
|
|
|
221
|
+
/** The gateway's server certificate and key. */
|
|
222
|
+
private fun gatewayIdentity(): KeyStore.PrivateKeyEntry {
|
|
223
|
+
val password = GATEWAY_KEYSTORE_PASSWORD.toCharArray()
|
|
224
|
+
val keystore = KeyStore.getInstance("PKCS12")
|
|
225
|
+
assets.open(GATEWAY_KEYSTORE).use { keystore.load(it, password) }
|
|
226
|
+
return keystore.getEntry("gateway", KeyStore.PasswordProtection(password)) as KeyStore.PrivateKeyEntry
|
|
227
|
+
}
|
|
228
|
+
|
|
211
229
|
private fun metadata() =
|
|
212
230
|
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).metaData
|
|
213
231
|
|
|
@@ -1,65 +1,37 @@
|
|
|
1
1
|
package com.tokamak.runtime
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
3
4
|
import androidx.webkit.ProxyConfig
|
|
4
5
|
import androidx.webkit.ProxyController
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Routes the app host, in every WebView of the process, through the gateway's port. The gateway
|
|
9
|
+
* keeps one port for the life of the process, so the route is set once and never cleared: without
|
|
10
|
+
* it the WebView would connect to the app host itself, where another app or device could answer.
|
|
11
|
+
* Main thread only.
|
|
12
|
+
*/
|
|
6
13
|
internal object TokamakProxy {
|
|
7
|
-
private var
|
|
8
|
-
private
|
|
9
|
-
private var changing = false
|
|
14
|
+
private var routed = false
|
|
15
|
+
private val waiting = mutableListOf<() -> Unit>()
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
fun release(activity: TokamakActivity) {
|
|
17
|
-
if (desired?.activity === activity) desired = null
|
|
18
|
-
reconcile()
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
private fun reconcile() {
|
|
22
|
-
if (changing || active === desired) return
|
|
23
|
-
val current = active
|
|
24
|
-
if (current != null) {
|
|
25
|
-
clear(current)
|
|
17
|
+
/** Routes [host] through the gateway's [port], then runs [ready] once the route is in place. */
|
|
18
|
+
fun route(context: Context, host: String, port: Int, ready: () -> Unit) {
|
|
19
|
+
if (routed) {
|
|
20
|
+
ready()
|
|
26
21
|
return
|
|
27
22
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
active = null
|
|
35
|
-
changing = false
|
|
36
|
-
reconcile()
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private fun set(request: Request) {
|
|
41
|
-
changing = true
|
|
42
|
-
ProxyController.getInstance().setProxyOverride(
|
|
43
|
-
proxyConfig(request),
|
|
44
|
-
request.activity.mainExecutor,
|
|
45
|
-
) {
|
|
46
|
-
active = request
|
|
47
|
-
changing = false
|
|
48
|
-
if (desired === request) request.activity.proxyReady()
|
|
49
|
-
reconcile()
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private fun proxyConfig(request: Request): ProxyConfig =
|
|
54
|
-
ProxyConfig.Builder()
|
|
55
|
-
.addProxyRule("http://127.0.0.1:${request.port}")
|
|
56
|
-
.addBypassRule(request.host)
|
|
23
|
+
waiting += ready
|
|
24
|
+
// Only the first caller requests the route.
|
|
25
|
+
if (waiting.size > 1) return
|
|
26
|
+
val config = ProxyConfig.Builder()
|
|
27
|
+
.addProxyRule("http://127.0.0.1:$port")
|
|
28
|
+
.addBypassRule(host)
|
|
57
29
|
.setReverseBypassEnabled(true)
|
|
58
30
|
.build()
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
31
|
+
ProxyController.getInstance().setProxyOverride(config, context.mainExecutor) {
|
|
32
|
+
routed = true
|
|
33
|
+
waiting.forEach { it() }
|
|
34
|
+
waiting.clear()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
65
37
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package com.tokamak.runtime
|
|
2
2
|
|
|
3
3
|
import java.io.File
|
|
4
|
+
import java.security.KeyStore
|
|
4
5
|
|
|
5
6
|
/** The tokamak runtime, running for the life of the process. */
|
|
6
7
|
internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
@@ -27,8 +28,6 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
27
28
|
val port: Int
|
|
28
29
|
get() = nativePort(handle)
|
|
29
30
|
|
|
30
|
-
fun restoreGateway(): Int = nativeRestoreGateway(handle)
|
|
31
|
-
|
|
32
31
|
/**
|
|
33
32
|
* Delivers the event [name] with its encoded [payload] to the Worker's listeners, blocking
|
|
34
33
|
* until they return or [timeoutMillis] passes, and returns their encoded reply, which is empty
|
|
@@ -57,12 +56,6 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
57
56
|
fun reply(id: Long, result: TokamakPluginResult) =
|
|
58
57
|
nativeReply(handle, id, result.payload, result.failed, result.done)
|
|
59
58
|
|
|
60
|
-
/**
|
|
61
|
-
* The authority a server certificate for [host] must chain to, or null
|
|
62
|
-
* when tokamak does not vouch for the host.
|
|
63
|
-
*/
|
|
64
|
-
fun serverAuthority(host: String): ByteArray? = nativeServerAuthority(handle, host)
|
|
65
|
-
|
|
66
59
|
/**
|
|
67
60
|
* The client certificate and PKCS#8 private key, both DER, to present for
|
|
68
61
|
* [host]. Null when tokamak cannot authenticate the connection.
|
|
@@ -76,43 +69,70 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
76
69
|
}
|
|
77
70
|
|
|
78
71
|
/**
|
|
79
|
-
* Start the runtime, which delivers `start` with [foreground]
|
|
80
|
-
* [plugins], throwing when it cannot serve the app.
|
|
72
|
+
* Start the runtime, which delivers `start` with [foreground], whose Worker calls
|
|
73
|
+
* [plugins], and whose gateway presents [server], throwing when it cannot serve the app.
|
|
81
74
|
*/
|
|
82
75
|
fun start(
|
|
83
76
|
packagedDir: File,
|
|
84
77
|
stateDir: File,
|
|
85
78
|
storageDir: File,
|
|
86
79
|
host: String,
|
|
80
|
+
server: KeyStore.PrivateKeyEntry,
|
|
87
81
|
foreground: Boolean,
|
|
88
82
|
plugins: Plugins,
|
|
89
83
|
): TokamakRuntime =
|
|
90
84
|
TokamakRuntime(
|
|
91
|
-
nativeStart(
|
|
85
|
+
nativeStart(
|
|
86
|
+
packagedDir.path,
|
|
87
|
+
stateDir.path,
|
|
88
|
+
storageDir.path,
|
|
89
|
+
host,
|
|
90
|
+
server.chain(),
|
|
91
|
+
server.privateKey.encoded,
|
|
92
|
+
foreground,
|
|
93
|
+
plugins,
|
|
94
|
+
),
|
|
92
95
|
)
|
|
93
96
|
|
|
94
97
|
/**
|
|
95
98
|
* Start the runtime against a host development server, delivering `start` with
|
|
96
|
-
* [foreground], whose development Worker calls [plugins]
|
|
99
|
+
* [foreground], whose development Worker calls [plugins], and whose gateway presents
|
|
100
|
+
* [server].
|
|
97
101
|
*/
|
|
98
102
|
fun startDevelopment(
|
|
99
103
|
stateDir: File,
|
|
100
104
|
host: String,
|
|
101
105
|
endpoint: String,
|
|
102
106
|
sessionToken: String,
|
|
107
|
+
server: KeyStore.PrivateKeyEntry,
|
|
103
108
|
foreground: Boolean,
|
|
104
109
|
plugins: Plugins,
|
|
105
110
|
): TokamakRuntime =
|
|
106
111
|
TokamakRuntime(
|
|
107
|
-
nativeStartDevelopment(
|
|
112
|
+
nativeStartDevelopment(
|
|
113
|
+
stateDir.path,
|
|
114
|
+
host,
|
|
115
|
+
endpoint,
|
|
116
|
+
sessionToken,
|
|
117
|
+
server.chain(),
|
|
118
|
+
server.privateKey.encoded,
|
|
119
|
+
foreground,
|
|
120
|
+
plugins,
|
|
121
|
+
),
|
|
108
122
|
)
|
|
109
123
|
|
|
124
|
+
/** The entry's certificates, leaf first, in DER. */
|
|
125
|
+
private fun KeyStore.PrivateKeyEntry.chain(): Array<ByteArray> =
|
|
126
|
+
certificateChain.map { it.encoded }.toTypedArray()
|
|
127
|
+
|
|
110
128
|
@JvmStatic
|
|
111
129
|
private external fun nativeStart(
|
|
112
130
|
packagedDir: String,
|
|
113
131
|
stateDir: String,
|
|
114
132
|
storageDir: String,
|
|
115
133
|
host: String,
|
|
134
|
+
serverChain: Array<ByteArray>,
|
|
135
|
+
serverKey: ByteArray,
|
|
116
136
|
foreground: Boolean,
|
|
117
137
|
plugins: Plugins,
|
|
118
138
|
): Long
|
|
@@ -123,6 +143,8 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
123
143
|
host: String,
|
|
124
144
|
endpoint: String,
|
|
125
145
|
sessionToken: String,
|
|
146
|
+
serverChain: Array<ByteArray>,
|
|
147
|
+
serverKey: ByteArray,
|
|
126
148
|
foreground: Boolean,
|
|
127
149
|
plugins: Plugins,
|
|
128
150
|
): Long
|
|
@@ -142,9 +164,6 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
142
164
|
@JvmStatic
|
|
143
165
|
private external fun nativePort(handle: Long): Int
|
|
144
166
|
|
|
145
|
-
@JvmStatic
|
|
146
|
-
private external fun nativeRestoreGateway(handle: Long): Int
|
|
147
|
-
|
|
148
167
|
@JvmStatic
|
|
149
168
|
private external fun nativeEmit(
|
|
150
169
|
handle: Long,
|
|
@@ -156,9 +175,6 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
156
175
|
@JvmStatic
|
|
157
176
|
private external fun nativeFetch(handle: Long, path: String, timeoutMillis: Long): ByteArray
|
|
158
177
|
|
|
159
|
-
@JvmStatic
|
|
160
|
-
private external fun nativeServerAuthority(handle: Long, host: String): ByteArray?
|
|
161
|
-
|
|
162
178
|
@JvmStatic
|
|
163
179
|
private external fun nativeClientIdentity(
|
|
164
180
|
handle: Long,
|
|
@@ -4,10 +4,8 @@ import android.app.Activity
|
|
|
4
4
|
import android.content.Intent
|
|
5
5
|
import android.graphics.Bitmap
|
|
6
6
|
import android.net.Uri
|
|
7
|
-
import android.net.http.SslError
|
|
8
7
|
import android.webkit.ClientCertRequest
|
|
9
8
|
import android.webkit.WebResourceRequest
|
|
10
|
-
import android.webkit.SslErrorHandler
|
|
11
9
|
import android.webkit.WebView
|
|
12
10
|
import android.webkit.WebViewClient
|
|
13
11
|
import java.io.ByteArrayInputStream
|
|
@@ -17,7 +15,10 @@ import java.security.cert.CertificateFactory
|
|
|
17
15
|
import java.security.cert.X509Certificate
|
|
18
16
|
import java.security.spec.PKCS8EncodedKeySpec
|
|
19
17
|
|
|
20
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Answers the app origin's client certificate requests with the runtime's decision. The network
|
|
20
|
+
* security config trusts the gateway's certificate, so no certificate error reaches the app.
|
|
21
|
+
*/
|
|
21
22
|
internal class TokamakWebViewClient(
|
|
22
23
|
private val activity: Activity,
|
|
23
24
|
private val host: String,
|
|
@@ -40,18 +41,6 @@ internal class TokamakWebViewClient(
|
|
|
40
41
|
return true
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
|
|
44
|
-
val host = Uri.parse(error.url).host
|
|
45
|
-
val authority = host?.let(runtime::serverAuthority)
|
|
46
|
-
val trusted = authority != null && chainsTo(error, authority)
|
|
47
|
-
if (trusted) {
|
|
48
|
-
handler.proceed()
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
if (authority != null) android.util.Log.w("tokamak", "refused the gateway's certificate: $error")
|
|
52
|
-
handler.cancel()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
44
|
override fun onReceivedClientCertRequest(view: WebView, request: ClientCertRequest) {
|
|
56
45
|
// Android reports no failure count, so every request is a first attempt.
|
|
57
46
|
val identity = runtime.clientIdentity(request.host, 0)
|
|
@@ -78,16 +67,6 @@ internal class TokamakWebViewClient(
|
|
|
78
67
|
}
|
|
79
68
|
}
|
|
80
69
|
|
|
81
|
-
private fun chainsTo(error: SslError, authority: ByteArray): Boolean {
|
|
82
|
-
if (error.primaryError != SslError.SSL_UNTRUSTED) return false
|
|
83
|
-
val presented = error.certificate.x509Certificate ?: return false
|
|
84
|
-
val issuer = certificate(authority) ?: return false
|
|
85
|
-
return runCatching {
|
|
86
|
-
presented.checkValidity()
|
|
87
|
-
presented.verify(issuer.publicKey)
|
|
88
|
-
}.isSuccess
|
|
89
|
-
}
|
|
90
|
-
|
|
91
70
|
private fun certificate(der: ByteArray): X509Certificate? =
|
|
92
71
|
runCatching {
|
|
93
72
|
CertificateFactory.getInstance("X.509")
|