mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 05:13:53 +01:00
* Fixes #3345 * Trust all hosts for beta * Added a custom NetworkFetcger for Fresco when on beta * removed unused assets * make TestCommonsApplication extend Application instead of Commons Application
This commit is contained in:
parent
df426f7c42
commit
fa87eb5661
7 changed files with 231 additions and 56 deletions
|
|
@ -68,7 +68,7 @@ public class NetworkingModule {
|
|||
.cache(new Cache(dir, OK_HTTP_CACHE_SIZE));
|
||||
|
||||
if(ConfigUtils.isBetaFlavour()){
|
||||
builder.sslSocketFactory(SslUtils.INSTANCE.getSslContextForCertificateFile(context, "*.wikimedia.beta.wmflabs.org.cer").getSocketFactory());
|
||||
builder.sslSocketFactory(SslUtils.INSTANCE.getTrustAllHostsSSLSocketFactory());
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,59 +1,16 @@
|
|||
package fr.free.nrw.commons.di
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import java.security.KeyManagementException
|
||||
import java.security.KeyStore
|
||||
import java.security.NoSuchAlgorithmException
|
||||
import java.security.SecureRandom
|
||||
import java.security.cert.Certificate
|
||||
import java.security.cert.CertificateException
|
||||
import java.security.cert.CertificateFactory
|
||||
import java.security.cert.X509Certificate
|
||||
import javax.net.ssl.*
|
||||
import javax.net.ssl.SSLContext
|
||||
import javax.net.ssl.SSLSocketFactory
|
||||
import javax.net.ssl.TrustManager
|
||||
import javax.net.ssl.X509TrustManager
|
||||
|
||||
object SslUtils {
|
||||
|
||||
fun getSslContextForCertificateFile(context: Context, fileName: String): SSLContext {
|
||||
try {
|
||||
val keyStore = SslUtils.getKeyStore(context, fileName)
|
||||
val sslContext = SSLContext.getInstance("SSL")
|
||||
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
|
||||
trustManagerFactory.init(keyStore)
|
||||
sslContext.init(null, trustManagerFactory.trustManagers, SecureRandom())
|
||||
return sslContext
|
||||
} catch (e: Exception) {
|
||||
val msg = "Error during creating SslContext for certificate from assets"
|
||||
e.printStackTrace()
|
||||
throw RuntimeException(msg)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getKeyStore(context: Context, fileName: String): KeyStore? {
|
||||
var keyStore: KeyStore? = null
|
||||
try {
|
||||
val assetManager = context.assets
|
||||
val cf = CertificateFactory.getInstance("X.509")
|
||||
val caInput = assetManager.open(fileName)
|
||||
val ca: Certificate
|
||||
try {
|
||||
ca = cf.generateCertificate(caInput)
|
||||
Log.d("SslUtilsAndroid", "ca=" + (ca as X509Certificate).subjectDN)
|
||||
} finally {
|
||||
caInput.close()
|
||||
}
|
||||
|
||||
val keyStoreType = KeyStore.getDefaultType()
|
||||
keyStore = KeyStore.getInstance(keyStoreType)
|
||||
keyStore!!.load(null, null)
|
||||
keyStore.setCertificateEntry("ca", ca)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
return keyStore
|
||||
}
|
||||
|
||||
fun getTrustAllHostsSSLSocketFactory(): SSLSocketFactory? {
|
||||
try {
|
||||
// Create a trust manager that does not validate certificate chains
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue