0

当我尝试设置证书时,出现错误:

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)

这就是我的代码的样子: 一只忙碌的猫

我不确定我应该把我的文件放在哪里。有任何想法吗?

我已经在我的 yaml 文件中声明了 ssl 文件夹。

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

更新:

将代码更改为:

    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);

但现在我得到这个错误:

E/flutter (11733): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:TlsException:usePrivateKeyBytes 失败(操作系统错误: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)

4

1 回答 1

0

这对我有用:

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);

感谢大家 :)

于 2019-02-06T08:08:59.270 回答