diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeGraphQLClient.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeGraphQLClient.kt index d9d9923346..f93bac258c 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeGraphQLClient.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeGraphQLClient.kt @@ -1,6 +1,7 @@ package com.braintreepayments.api import java.util.Locale +import javax.net.ssl.SSLException internal class BraintreeGraphQLClient( private val httpClient: HttpClient = createDefaultHttpClient() @@ -78,8 +79,10 @@ internal class BraintreeGraphQLClient( companion object { + @Throws(SSLException::class) private fun createDefaultHttpClient(): HttpClient { - val socketFactory = TLSSocketFactory(TLSCertificatePinning.certInputStream) + val socketFactory = + TLSSocketFactory(TLSCertificatePinning.createCertificateInputStream()) return HttpClient(socketFactory, BraintreeGraphQLResponseParser()) } } diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeHttpClient.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeHttpClient.kt index 3d54d37e68..00e5fc5dc7 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeHttpClient.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/BraintreeHttpClient.kt @@ -4,6 +4,7 @@ import android.net.Uri import com.braintreepayments.api.HttpClient.RetryStrategy import org.json.JSONException import org.json.JSONObject +import javax.net.ssl.SSLException /** * Network request class that handles Braintree request specifics and threading. @@ -178,8 +179,10 @@ internal class BraintreeHttpClient( private const val USER_AGENT_HEADER = "User-Agent" private const val CLIENT_KEY_HEADER = "Client-Key" + @Throws(SSLException::class) private fun createDefaultHttpClient(): HttpClient { - val socketFactory = TLSSocketFactory(TLSCertificatePinning.certInputStream) + val socketFactory = + TLSSocketFactory(TLSCertificatePinning.createCertificateInputStream()) return HttpClient(socketFactory, BraintreeHttpResponseParser()) } } diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/TLSCertificatePinning.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/TLSCertificatePinning.kt index bfc6f2838b..945f0a315b 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/TLSCertificatePinning.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/TLSCertificatePinning.kt @@ -1,9 +1,7 @@ package com.braintreepayments.api import java.io.ByteArrayInputStream -import java.io.InputStream import java.nio.charset.StandardCharsets -import javax.net.ssl.SSLException /** * THIS CODE IS GENERATED BY 'codify_certificates.sh' in Braintree Github Enterprise repo @@ -714,7 +712,7 @@ CkPa1mnI2g7vVq2d66WZmpsDtY1EwUya7vmZTxw7BFlntV7lrVxPLhUbq1wJGIbT uEnXgUGZxnLYZfCNsC/UAQxvKKpxBLWus/n1InM/97HfA31WJsvQ -----END CERTIFICATE-----""" - @get:Throws(SSLException::class) - val certInputStream: InputStream - get() = ByteArrayInputStream(CERTIFICATES.toByteArray(StandardCharsets.UTF_8)) + private val certificateByteArray = CERTIFICATES.toByteArray(StandardCharsets.UTF_8) + + fun createCertificateInputStream() = ByteArrayInputStream(certificateByteArray) } diff --git a/CHANGELOG.md b/CHANGELOG.md index e429e3a54b..d9f676bf9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Fix `PayPalNativeCheckoutAccountNonce` Null Pointer Exception by ensuring that all `@NonNull` values are initialized with a non-null value. * BraintreeCore * Use TLS 1.3 for all HTTP requests, when available + * Refactor TLSCertificatePinning `certInputStream` property to initialize a `ByteArrayInputStream` once instead of every time the property is accessed. ## 4.47.0 (2024-06-06) diff --git a/SharedUtils/src/main/java/com/braintreepayments/api/TLSSocketFactory.java b/SharedUtils/src/main/java/com/braintreepayments/api/TLSSocketFactory.java index e8821ef60c..7edb4e0154 100644 --- a/SharedUtils/src/main/java/com/braintreepayments/api/TLSSocketFactory.java +++ b/SharedUtils/src/main/java/com/braintreepayments/api/TLSSocketFactory.java @@ -13,7 +13,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException;