Member-only story
Flutter - Android 7 CERTIFICATE_VERIFY_FAILED with LetsEncrypt SSL cert after Sept 30, 2021
After Sept 30, 2021, https get/post requests to a website using a Let’s Encrypt SSL certificate on an old Android 7 device were failing with this error: HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: certificate has expired(handshake.cc:354))
Solution
In Flutter, to once again make SSL https connections on older devices to Let’s Encrypt SSL protected websites, we can supply Let’s Encrypt’s trusted certificate via SecurityContext
to dart:io
HttpClient
object (from the dart native communications library), which we can use directly to make https get/post calls, or we can supply that customized HttpClient
to Flutter/Dart package:http
IOClient
if we are using that popular pub.dev package.
Example
Here’s a Flutter unit test which creates a dart:io
HttpClient
with a SecurityContext
that has a Let's Encrypt root certificate supplied to it. Then, this HttpClient
is provided to package:http
IOClient
which implement's the Client
interface and can be used for all the usual get
, post
etc. calls.
import 'dart:convert';
import 'dart:typed_data';
import 'dart:io';import 'package:test/test.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';void main() {
const sslUrl = 'https://valid-isrgrootx1.letsencrypt.org/';