SSL-SecurityContext-未处理的异常:FileSystemException:无法打开文件-错误:无此类文件或目录,errno = 2

时间:2019-02-05 12:19:50

标签: file ssl flutter filesystemexception

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

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)

这是我的代码的样子: a busy cat

我不确定文件应该放在哪里。 有什么想法吗?

我在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)

1 个答案:

答案 0 :(得分: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);

谢谢大家:)