-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Making address and port configurable * Making address and port configurable * Adding support for SSL connections * Small change * Unused
- Loading branch information
1 parent
252a572
commit a09161e
Showing
9 changed files
with
97 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<network-security-config> | ||
<debug-overrides> | ||
<trust-anchors> | ||
<!-- Trust user added CAs while debuggable only --> | ||
<certificates src="user" /> | ||
</trust-anchors> | ||
</debug-overrides> | ||
</network-security-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
mock/src/main/java/com/telefonica/mock/cert/AllCertsAllowedBuilderUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.telefonica.mock.cert | ||
|
||
import android.annotation.SuppressLint | ||
import okhttp3.OkHttpClient | ||
import java.security.KeyManagementException | ||
import java.security.NoSuchAlgorithmException | ||
import java.security.cert.CertificateException | ||
import javax.net.ssl.HostnameVerifier | ||
import javax.net.ssl.SSLContext | ||
import javax.net.ssl.TrustManager | ||
import javax.net.ssl.X509TrustManager | ||
|
||
object AllCertsAllowedBuilderUpdater { | ||
|
||
@SuppressLint("CustomX509TrustManager") | ||
@JvmStatic | ||
fun update(builder: OkHttpClient.Builder) { | ||
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager { | ||
@SuppressLint("TrustAllX509TrustManager") | ||
@Throws(CertificateException::class) | ||
override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) { | ||
} | ||
|
||
@SuppressLint("TrustAllX509TrustManager") | ||
@Throws(CertificateException::class) | ||
override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) { | ||
} | ||
|
||
override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> { | ||
return arrayOf() | ||
} | ||
}) | ||
|
||
try { | ||
val sslContext = SSLContext.getInstance("SSL") | ||
sslContext.init(null, trustAllCerts, java.security.SecureRandom()) | ||
val sslSocketFactory = sslContext.socketFactory | ||
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) | ||
builder.hostnameVerifier(HostnameVerifier { _, _ -> true }) | ||
} catch (e: NoSuchAlgorithmException) { | ||
throw RuntimeException(e) | ||
} catch (e: KeyManagementException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
} |