Flutter SecureSocketServer 传输

问题描述 投票:0回答:1

我在两台设备上运行我的代码,一台服务器一台客户端,我想保护传输,传输正在本地网络中进行(其他人可以监听)。

这就是我创建自签名证书和密钥来初始化服务器的原因。

    var ctx = SecurityContext()
            ..useCertificateChainBytes(utf8.encode(Secure.cer))
            ..usePrivateKeyBytes(utf8.encode(Secure.key));
          
socketServer = await SecureServerSocket.bind(hostAddress, Global.unusedPort, ctx,shared: true);

在客户端:

      var ctx = SecurityContext()
            ..useCertificateChainBytes(utf8.encode(Secure.cer))
            ..usePrivateKeyBytes(utf8.encode(Secure.key));
          
connectedSocket = await SecureSocket.connect(device.ipAddress, device.port!,context: ctx);

Secure.key 和 Secure.cer 是具有证书和密钥的静态字符串。

当我尝试在之间发送消息时,我总是得到:

flutter: ----------------------------------------------------
flutter: HandshakeException: Connection terminated during handshake

我不知道下一步该做什么,如何确保转账安全?我不想加密消息,因为我在之间发送文件字节。

flutter dart networking openssl tls1.3
1个回答
0
投票

我通过将理查德的答案添加到代码中解决了这个问题。

在客户端,我只需要添加defaultContext并为每个badCertificateCallback返回true。就我而言,我没有直接返回 true。我比较了我创建的自签名证书,两台设备都有该证书。

  var ctx = SecurityContext.defaultContext;

  Uint8List bytes = base64.decode(Secure.cerContext);
  connectedSocket = await SecureSocket.connect(ipAddress, port,context: ctx, onBadCertificate: (X509Certificate cert) {
    return const ListEquality().equals(bytes, cert.der);
  });

我认为,只要应用程序没有进行逆向工程并且证书没有被盗,这种方法将更好地防止中间人攻击。

© www.soinside.com 2019 - 2024. All rights reserved.