作为系统范围内可用的 API,我怀疑是否有任何可能的方法来限制DownloadManager
特定的服务器证书。针对您提到的示例,Google-Play 很可能是通过观察下载完成来安装下载的 APK。
但是,如果我的理解是正确的,您可以使用此 SO post中讨论的Retrofit库的文件下载方法来实现您的目标,而证书固定可以通过使用以下类构建 Retrofit 客户端来实现:SelfSigningClientBuilder
SelfSigningClientBuilder.kt
import android.content.Context
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.security.*
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.util.*
import java.util.concurrent.TimeUnit
import javax.net.ssl.*
object SelfSigningClientBuilder {
private const val NET_TIMEOUT_READ = 80L
private const val NET_TIMEOUT_WRITE = 120L
private const val NET_TIMEOUT_CONNECT = 75L
@JvmStatic
fun createClient(context: Context, isCertificateNeeded: Boolean = true): OkHttpClient {
val interceptor = getInterceptor()
if (isCertificateNeeded)
try {
val cf = CertificateFactory.getInstance("X.509")
// assuming the CA certificate is put inside res/raw folder named as ca_cert.pem
val cert = context.resources.openRawResource(R.raw.ca_cert)
val ca = cf?.generateCertificate(cert)
cert.close()
val keyStoreType = KeyStore.getDefaultType()
val keyStore = KeyStore.getInstance(keyStoreType)
keyStore.load(null, null)
keyStore.setCertificateEntry("ca", ca)
val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
val tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
tmf.init(keyStore)
val trustManagers = tmf.trustManagers
if (trustManagers.size != 1 || trustManagers[0] !is X509TrustManager) {
throw IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers))
}
val trustManager = trustManagers[0] as X509TrustManager
val sslContext = SSLContext.getInstance("SSL")
sslContext!!.init(null, trustManagers, null)
return OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManager)
.readTimeout(NET_TIMEOUT_READ, TimeUnit.SECONDS)
.writeTimeout(NET_TIMEOUT_WRITE, TimeUnit.SECONDS)
.connectTimeout(NET_TIMEOUT_CONNECT, TimeUnit.SECONDS)
// .retryOnConnectionFailure(true)
.addInterceptor(interceptor)
.build()
} catch (e: KeyStoreException) {
e.printStackTrace()
} catch (e: CertificateException) {
e.printStackTrace()
} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: KeyManagementException) {
e.printStackTrace()
}
return OkHttpClient.Builder()
.readTimeout(NET_TIMEOUT_READ, TimeUnit.SECONDS)
.writeTimeout(NET_TIMEOUT_WRITE, TimeUnit.SECONDS)
.connectTimeout(NET_TIMEOUT_CONNECT, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
}
private fun getInterceptor(): Interceptor {
return Interceptor { chain ->
val originalRequest = chain.request()
val request: Request = originalRequest.newBuilder()
.header("custom-header", "my-header-value")
.method(originalRequest.method(), originalRequest.body())
.build()
chain.proceed(request)
}
}
}
然后按照这里的代码段构建Retrofit-client,然后使用它来下载文件:
val retrofit = Retrofit.Builder()
.client(SelfSigningClientBuilder.createClient(context, true)
.baseUrl("https://yourdomain.com/")
.build()
在使用这个改造客户端时,我已经解析了Wireshark、Burp-suite和Charles Proxy的所有 REST API 请求——它们都不能显示实际的请求文本,而不是一些乱码数据。因此,我希望您的文件内容在遵循此过程时能够免受 MITM 攻击。