-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use Let's Encrypt certificates on API 23 devices (#640)
Android 7 devices no longer trust certificates issued by Let's Encrypt (see https://letsencrypt.org/2020/11/06/own-two-feet and https://letsencrypt.org/2023/07/10/cross-sign-expiration.html for details). To work around that provide the Let's Encrypt root certs as resources. On API 24+ devices add those via network_security_config.xml. On API 23 devices they need to be installed manually for OkHttp SSL connections, and checked when there is an SSL error in LoginWebViewActivity. The root certificates were downloaded from https://letsencrypt.org/certificates/: - https://letsencrypt.org/certs/isrgrootx1.der (self-signed) - https://letsencrypt.org/certs/isrg-root-x1-cross-signed.der (cross-signed) - https://letsencrypt.org/certs/isrg-root-x2.der (self-signed) - https://letsencrypt.org/certs/isrg-root-x2-cross-signed.der (cross-signed) Fixes #638
- Loading branch information
1 parent
fefeb0a
commit 3e1d94d
Showing
12 changed files
with
152 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright 2024 Pachli Association | ||
~ | ||
~ This file is a part of Pachli. | ||
~ | ||
~ This program is free software; you can redistribute it and/or modify it under the terms of the | ||
~ GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
~ License, or (at your option) any later version. | ||
~ | ||
~ Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
~ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
~ Public License for more details. | ||
~ | ||
~ You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
~ see <http://www.gnu.org/licenses>. | ||
--> | ||
|
||
<network-security-config xmlns:tools="http://schemas.android.com/tools"> | ||
<base-config cleartextTrafficPermitted="false"> | ||
<trust-anchors> | ||
<certificates src="@raw/isrg_root_x1_cross_signed" /> | ||
<certificates src="@raw/isrg_root_x2" /> | ||
<certificates src="@raw/isrg_root_x2_cross_signed" /> | ||
<certificates src="@raw/isrgrootx1" /> | ||
<certificates src="system" /> | ||
<certificates src="user" tools:ignore="AcceptsUserCertificates" /> | ||
</trust-anchors> | ||
</base-config> | ||
</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
56 changes: 56 additions & 0 deletions
56
core/network/src/main/kotlin/app/pachli/core/network/util/LocalCertificateTrustManager.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,56 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.core.network.util | ||
|
||
import android.content.Context | ||
import app.pachli.core.network.R | ||
import java.security.cert.CertificateFactory | ||
import java.security.cert.X509Certificate | ||
import javax.net.ssl.X509TrustManager | ||
import okhttp3.tls.HandshakeCertificates | ||
|
||
// Devices running Android 7 (API 23) do not trust the Let's Encrypt certificate and | ||
// will refuse to connect. These functions provide certificates and a trust manager | ||
// that contain the Let's Encrypt certificates and are used when configuring OkHttp | ||
// and handling LoginWebViewActivity SSL errors. | ||
// | ||
// See https://github.com/pachli/pachli-android/issues/638#issuecomment-2071935438 | ||
// for the background. | ||
|
||
/** | ||
* @return [HandshakeCertificates] containing the platform's trusted certificates and | ||
* the extra certificates in `values/raw`. | ||
*/ | ||
fun localHandshakeCertificates(context: Context): HandshakeCertificates { | ||
val certFactory = CertificateFactory.getInstance("X.509") | ||
return HandshakeCertificates.Builder() | ||
.addPlatformTrustedCertificates() | ||
.addTrustedCertificate(certFactory.generateCertificate(context.resources.openRawResource(R.raw.isrg_root_x1_cross_signed)) as X509Certificate) | ||
.addTrustedCertificate(certFactory.generateCertificate(context.resources.openRawResource(R.raw.isrg_root_x2)) as X509Certificate) | ||
.addTrustedCertificate(certFactory.generateCertificate(context.resources.openRawResource(R.raw.isrg_root_x2_cross_signed)) as X509Certificate) | ||
.addTrustedCertificate(certFactory.generateCertificate(context.resources.openRawResource(R.raw.isrgrootx1)) as X509Certificate) | ||
.build() | ||
} | ||
|
||
/** | ||
* @return An [X509TrustManager] configured with certificates loaded from | ||
* localCertHandshakeCertificates]. | ||
*/ | ||
// Exists so that LoginWebViewActivity does not depend on HandshakeCertificates | ||
// (on okHttp type), but X509TrustManager, a javax type. | ||
fun localTrustManager(context: Context): X509TrustManager = localHandshakeCertificates(context).trustManager |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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