When I try to set certificate, I get an error:

E/flutter ( 7195): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = 'assets/ssl/STAR_octopusspace_com.crt' (OS Error: No such file or directory, errno = 2) E/flutter ( 7195): #0 _File.throwIfError (dart:io/file_impl.dart:643:7) E/flutter ( 7195): #1 _File.openSync (dart:io/file_impl.dart:487:5) E/flutter ( 7195): #2 _File.readAsBytesSync (dart:io/file_impl.dart:547:18) E/flutter ( 7195): #3 _SecurityContext.useCertificateChain (dart:io/runtime/binsecure_socket_patch.dart:175:40)

and this is how my code looks like: a busy cat

I'm not sure where should I put my files. Any ideas?

I have declared ssl folder in my yaml file.

  uses-material-design: true
  assets:
      - assets/ssl/
      - assets/graphics/

UPDATE:

Changed code to this:

    final List<int> certificateChainBytes =
        (await rootBundle.load('assets/ssl/STAR_octopusspace_com.crt')).buffer.asInt8List();
    context.useCertificateChainBytes(certificateChainBytes);
    final List<int> keyBytes =
    (await rootBundle.load('assets/ssl/client_octopusspace.key')).buffer.asInt8List();
    context.usePrivateKeyBytes(keyBytes);

but now I get this error:

E/flutter (11733): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: TlsException: Failure in usePrivateKeyBytes (OS Error: E/flutter (11733): KEY_VALUES_MISMATCH(ssl_cert.cc:494), errno = 0) E/flutter (11733): #0 _SecurityContext.usePrivateKeyBytes (dart:io/runtime/binsecure_socket_patch.dart:164:50)

1

There are 1 best solutions below

0
On

This is what worked for me:

final SecurityContext context = SecurityContext.defaultContext;

final ByteData crtData = await rootBundle.load('assets/ssl/STAR_octopusspace_com.crt');
context.setTrustedCertificatesBytes(crtData.buffer.asUint8List());

final ByteData authoritiesBytes = await rootBundle.load('assets/ssl/STAR_octopusspace_com.ca-bundle');
context.setClientAuthoritiesBytes(authoritiesBytes.buffer.asUint8List());

final ByteData keyBytes = await rootBundle.load('assets/ssl/client_octopusspace.key');
context.usePrivateKeyBytes(keyBytes.buffer.asUint8List());

httpClient = HttpClient(context: context);

Thanks everyone :)