2023-09-12 14:51:43 +00:00
|
|
|
import 'dart:io';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/services/app_settings.service.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
2023-09-12 14:51:43 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
class HttpSSLCertOverride extends HttpOverrides {
|
2024-07-26 13:59:02 +00:00
|
|
|
static final Logger _log = Logger("HttpSSLCertOverride");
|
|
|
|
final SSLClientCertStoreVal? _clientCert;
|
|
|
|
late final SecurityContext? _ctxWithCert;
|
|
|
|
|
|
|
|
HttpSSLCertOverride() : _clientCert = SSLClientCertStoreVal.load() {
|
|
|
|
if (_clientCert != null) {
|
|
|
|
_ctxWithCert = SecurityContext(withTrustedRoots: true);
|
|
|
|
if (_ctxWithCert != null) {
|
|
|
|
setClientCert(_ctxWithCert, _clientCert);
|
|
|
|
} else {
|
|
|
|
_log.severe("Failed to create security context with client cert!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ctxWithCert = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setClientCert(SecurityContext ctx, SSLClientCertStoreVal cert) {
|
|
|
|
try {
|
|
|
|
_log.info("Setting client certificate");
|
|
|
|
ctx.usePrivateKeyBytes(cert.data, password: cert.password);
|
2024-07-28 22:32:53 +00:00
|
|
|
ctx.useCertificateChainBytes(cert.data, password: cert.password);
|
2024-07-26 13:59:02 +00:00
|
|
|
} catch (e) {
|
|
|
|
_log.severe("Failed to set SSL client cert: $e");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:51:43 +00:00
|
|
|
@override
|
|
|
|
HttpClient createHttpClient(SecurityContext? context) {
|
2024-07-26 13:59:02 +00:00
|
|
|
if (context != null) {
|
|
|
|
if (_clientCert != null) {
|
|
|
|
setClientCert(context, _clientCert);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
context = _ctxWithCert;
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:51:43 +00:00
|
|
|
return super.createHttpClient(context)
|
|
|
|
..badCertificateCallback = (X509Certificate cert, String host, int port) {
|
|
|
|
AppSettingsEnum setting = AppSettingsEnum.allowSelfSignedSSLCert;
|
2024-01-27 16:14:32 +00:00
|
|
|
|
2023-09-12 14:51:43 +00:00
|
|
|
// Check if user has allowed self signed SSL certificates.
|
|
|
|
bool selfSignedCertsAllowed =
|
|
|
|
Store.get(setting.storeKey as StoreKey<bool>, setting.defaultValue);
|
|
|
|
|
|
|
|
bool isLoggedIn = Store.tryGet(StoreKey.currentUser) != null;
|
|
|
|
|
|
|
|
// Conduct server host checks if user is logged in to avoid making
|
|
|
|
// insecure SSL connections to services that are not the immich server.
|
|
|
|
if (isLoggedIn && selfSignedCertsAllowed) {
|
|
|
|
String serverHost =
|
|
|
|
Uri.parse(Store.tryGet(StoreKey.serverEndpoint) ?? "").host;
|
|
|
|
|
|
|
|
selfSignedCertsAllowed &= serverHost.contains(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selfSignedCertsAllowed) {
|
2024-07-26 13:59:02 +00:00
|
|
|
_log.severe("Invalid SSL certificate for $host:$port");
|
2023-09-12 14:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return selfSignedCertsAllowed;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|