Skip to content

Commit

Permalink
fix(auth/connectivity): try to fix certificate issue on windows 11 (#…
Browse files Browse the repository at this point in the history
…2472)

## Description

## Tests
  • Loading branch information
prateekmedia authored Jul 17, 2024
2 parents 5dd8c0a + ad87a08 commit f2057cb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 78 deletions.
30 changes: 0 additions & 30 deletions auth/assets/ca/lets-encrypt-r3.pem

This file was deleted.

143 changes: 109 additions & 34 deletions auth/lib/core/win_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
// Inspired from https://github.com/netfarm/lehttp_overrides

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

// expired
const String kSha1DstX3 = 'DAC9024F54D8F6DF94935FB1732638CA6AD77C13';

import 'package:logging/logging.dart';
// ISRG Root X1
const String kIsrgRootX1 = '''-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
T8KOEUt+zwvo/7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4Ui0/1lvh+wjChP4kqKOJ2qxq
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----''';

/*
Reference from
https://github.com/realm/realm-dart/blob/main/packages/realm_dart/lib/src/handles/native/default_client.dart
https://github.com/realm/realm-dart/pull/1378
*/
HttpClient windowsHttpClient() {
final logger = Logger("WindowsHttpClient");
const isrgRootX1CertPEM = // The root certificate used by lets encrypt
'''
subject=CN=ISRG Root X1,O=Internet Security Research Group,C=US
issuer=CN=DST Root CA X3,O=Digital Signature Trust Co.
-----BEGIN CERTIFICATE-----
const String kIsrgRootX1Alt = '''-----BEGIN CERTIFICATE-----
MIIFYDCCBEigAwIBAgIQQAF3ITfU6UK47naqPGQKtzANBgkqhkiG9w0BAQsFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTIxMDEyMDE5MTQwM1oXDTI0MDkzMDE4MTQwM1ow
Expand Down Expand Up @@ -44,28 +70,77 @@ MA0GCSqGSIb3DQEBCwUAA4IBAQAKcwBslm7/DlLQrt2M51oGrS+o44+/yQoDFVDC
WCLKTVXkcGdtwlfFRjlBz4pYg1htmf5X6DYO8A4jqv2Il9DjXA6USbW1FzXSLr9O
he8Y4IWS6wY7bCkjCWDcRQJMEhg76fsO3txE+FiYruq9RUWhiF1myv4Q6W+CyBFC
Dfvp7OOGAN6dEOM4+qR9sdjoSYKEBpsr6GtPAQw4dy753ec5
-----END CERTIFICATE-----''';
-----END CERTIFICATE-----
''';

const String kLetsEncryptCert = '''-----BEGIN CERTIFICATE-----
MIIFFjCCA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QEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W
PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl
ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz
CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm
lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4
avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2
yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O
yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids
hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+
HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv
MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX
nLRbwHOoq7hHwg==
-----END CERTIFICATE-----
''';

String _hexConverter(Uint8List bytes) {
final StringBuffer buffer = StringBuffer();
for (int part in bytes) {
buffer.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
}
return buffer.toString().toUpperCase();
}

class WindowsHttpOverrides extends HttpOverrides {
static bool? _initRootCert;
final bool allowExpiredDSTX3;

static bool _addRootCert() {
if (Platform.isWindows) {
final List<int> cert = ascii.encode(kIsrgRootX1);
SecurityContext.defaultContext.setTrustedCertificatesBytes(cert);
final List<int> certAlt = ascii.encode(kIsrgRootX1Alt);
SecurityContext.defaultContext.setTrustedCertificatesBytes(certAlt);
final List<int> certAlt2 = ascii.encode(kLetsEncryptCert);
SecurityContext.defaultContext.setTrustedCertificatesBytes(certAlt2);
}
return true;
}

WindowsHttpOverrides({this.allowExpiredDSTX3 = false}) {
_initRootCert ??= _addRootCert();
}

if (Platform.isWindows) {
final context = SecurityContext(withTrustedRoots: true);
try {
logger.info('Adding certificate to trusted certificates');
context.setTrustedCertificatesBytes(
const AsciiEncoder().convert(isrgRootX1CertPEM),
);
logger.info("Certificate added to trusted certificates");
return HttpClient(context: context);
} on TlsException catch (e) {
logger.warning(
"Error adding certificate to trusted certificates: ${e.osError?.message}",
);
// certificate is already trusted. Nothing to do here
if (e.osError?.message.contains("CERT_ALREADY_IN_HASH_TABLE") != true) {
rethrow;
} else {
return HttpClient();
}
@override
HttpClient createHttpClient(SecurityContext? context) {
final HttpClient client = super.createHttpClient(context);
if (allowExpiredDSTX3) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) =>
_hexConverter(cert.sha1) == kSha1DstX3;
}
return client;
}
throw UnsupportedError("This platform is not supported");
}
27 changes: 14 additions & 13 deletions auth/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/core/constants.dart';
import 'package:ente_auth/core/logging/super_logging.dart';
import 'package:ente_auth/core/network.dart';
import 'package:ente_auth/core/win_http_client.dart';
import 'package:ente_auth/ente_theme_data.dart';
import 'package:ente_auth/locale.dart';
import 'package:ente_auth/services/authenticator_service.dart';
Expand Down Expand Up @@ -67,7 +68,7 @@ Future<void> initSystemTray() async {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows) {
await whiteListLetsEncryptRootCA();
HttpOverrides.global = WindowsHttpOverrides();
}

if (PlatformUtil.isDesktop()) {
Expand All @@ -90,18 +91,18 @@ void main() async {
}
}

Future<void> whiteListLetsEncryptRootCA() async {
try {
// https://stackoverflow.com/a/71090239
// https://github.com/ente-io/ente/issues/2178
ByteData data =
await PlatformAssetBundle().load('assets/ca/lets-encrypt-r3.pem');
SecurityContext.defaultContext
.setTrustedCertificatesBytes(data.buffer.asUint8List());
} catch (e) {
_logger.severe("Failed to whitelist Let's Encrypt Root CA", e);
}
}
// Future<void> whiteListLetsEncryptRootCA() async {
// try {
// // https://stackoverflow.com/a/71090239
// // https://github.com/ente-io/ente/issues/2178
// ByteData data =
// await PlatformAssetBundle().load('assets/ca/lets-encrypt-r3.pem');
// SecurityContext.defaultContext
// .setTrustedCertificatesBytes(data.buffer.asUint8List());
// } catch (e) {
// _logger.severe("Failed to whitelist Let's Encrypt Root CA", e);
// }
// }

Future<void> _runInForeground() async {
final savedThemeMode = _themeMode(await AdaptiveTheme.getThemeMode());
Expand Down
1 change: 0 additions & 1 deletion auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ flutter:
- assets/custom-icons/icons/
- assets/custom-icons/_data/
- assets/svg/
- assets/ca/

fonts:
- family: Inter
Expand Down

0 comments on commit f2057cb

Please sign in to comment.