HarmonyOS 使用socket.TLSSocket进行SSL加密通信时 socket.TLSConnectOptions>secureOptions>ca 如何配置

let tlsOneWay: socket.TLSSocket = socket.constructTLSSocketInstance(); // One way authentication
tlsOneWay.bind(bindAddr, (err: BusinessError) => {
  if (err) {
    console.log('bind fail');
    return;
  }
  console.log('bind success');
});

let tlsOneWayConnectOptions: socket.TLSConnectOptions = {
  address: {
    address: '192.168.xx.xxx',
    port: 8080
  },
  secureOptions: {
    ca: ["xxxx", "xxxx"],
    cipherSuite: "AES256-SHA256"
  }
}
tlsOneWay.connect(tlsOneWayConnectOptions).then(() => {
  console.log("connect successfully");
}).catch((err: BusinessError) => {
  console.log("connect failed " + JSON.stringify(err));
});

ca: [“xxxx”, “xxxx”] 具体应该如何配置

HarmonyOS
6天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

请按照下方示例再尝试一下:

import type { BusinessError } from '@ohos.base';
import socket from '@ohos.net.socket';
import wifiManager from '@ohos.wifiManager';
import connection from '@ohos.net.connection';
import util from '@ohos.util';
function int2ip(n: number): string {
  return `${(n & 0xFF000000) >>> 24}.${(n & 0x00FF0000) >>> 16}.${(n & 0x0000FF00) >>> 8}.${(n & 0xFF)}`
}

function getLocalIp(): string {
  const ipInfo = wifiManager.getIpInfo()
  console.log(tag + 'ipInfo : ' + JSON.stringify(ipInfo));
  const localIp = int2ip(ipInfo.ipAddress)
  console.log(tag + 'localHost : ' + localIp);
  return localIp;
}

class SocketInfo {
  message: ArrayBuffer = new ArrayBuffer(1);
  remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo;
}
export class TlsSocketTest {
  // host: string = 'xxx.xxx.com';
  host: string = 'your_host_ip';
  port: number = 888;
  ca: string[] = [
    "-----BEGIN CERTIFICATE-----\n" +
      "MIIEaTCCA1GgAwIBAgILBAAAAAABRE7wQkcwDQYJKoZIhvcNAQELBQAwVzELMAkG\n" +
  ....
  "-----END CERTIFICATE-----\n",

  "-----BEGIN CERTIFICATE-----\n" +
  ....
  "-----END CERTIFICATE-----\n",
  ];
  async connectTest(): Promise<void> {
    try {
      console.debug(tag + 'connectTest');
      // 本地IP
      const localIp = getLocalIp();
      // 网络是否正常
      let net = connection.hasDefaultNetSync();
      console.log(tag + 'net : ' + JSON.stringify(net));
      console.log(tag + 'host : ' + this.host + ', port : ' + this.port);
      // 服务器的域名解析,得到服务器IP
      const handler = await connection.getDefaultNet();
      const netAddressArray: connection.NetAddress[] = await handler.getAddressesByName(this.host);
      console.log(tag + 'netAddressArray : ' + JSON.stringify(netAddressArray));
      const serverIp = netAddressArray[0].address;
      console.log(tag + 'serverIp : ' + serverIp);
      // 创建tlssocket
      const tlsSocket = socket.constructTLSSocketInstance();
      let tlsConnectOptions: socket.TLSConnectOptions = {
        address: {
          address: serverIp,
          port: this.port
        },
        secureOptions: {
          ca: this.ca,
        }
      };
      // 绑定本地IP和Port
      tlsSocket.bind({ address: localIp, family: 1, port: 0 })
        .then(() => {
          console.log(tag + 'tlsSocket bind success');
          // 注册接收服务器响应事件
          tlsSocket.on('message', (msgFromServer: SocketInfo) => {
            console.log(tag + 'on(message) : ' + JSON.stringify(msgFromServer));
            let uint8Array = new Uint8Array(msgFromServer.message);
            let textDecoder = util.TextDecoder.create();
            let res = textDecoder.decodeWithStream(uint8Array);
            console.log(tag + 'on(message) , res : ' + JSON.stringify(res));
          })
          // 注册连接成功事件
          tlsSocket.on('connect', () => {
            console.log(tag + 'on(connect)');
          })
          // 开始连接服务器
          tlsSocket.connect(tlsConnectOptions)
            .then(() => {
              console.log(tag + 'tlsSocket connect success');
              tlsSocket.send("hello zhtest")
                .then(() => {
                  console.log(tag + 'tlsSocket send success');
                  tlsSocket.close()
                    .then(() => {
                      console.log(tag + 'tlsSocket close success');
                    })
                    .catch((err: BusinessError) => {
                      console.log(tag + 'tlsSocket close err : ' + JSON.stringify(err));
                    })
                }).catch((err: BusinessError) => {
                console.log(tag + 'tlsSocket send err : ' + JSON.stringify(err));
              });
            })
            .catch((err: BusinessError) => {
              console.log(tag + 'tlsSocket connect err : ' + JSON.stringify(err));
            })
        })
        .catch((err: BusinessError) => {
          console.log(tag + 'tlsSocket bind err : ' + JSON.stringify(err));
        })
    } catch (err) {
      console.log(tag + 'connectTest err : ' + JSON.stringify(err));
    }
  }
}
分享
微博
QQ
微信
回复
6天前
相关问题
HarmonyOS TLS Socket是否能直接信任ca
67浏览 • 1回复 待解决
HarmonyOS 经典蓝牙的socket通信问题
467浏览 • 1回复 待解决
HarmonyOS如何退到后台退出socket
567浏览 • 0回复 待解决
HarmonyOS 关于Socket
167浏览 • 1回复 待解决
HarmonyOS Tcp socket问题
178浏览 • 1回复 待解决
ohos.net.socket (Socket连接)链接不上
5776浏览 • 1回复 待解决
HarmonyOS TLSSocket建连ca入参必传问题
104浏览 • 1回复 待解决
HarmonyOS socket绑定端口失败
136浏览 • 1回复 待解决
HarmonyOS Socket报缓存溢出
190浏览 • 1回复 待解决
HarmonyOS socket 绑定IP问题
157浏览 • 1回复 待解决
HarmonyOS UDP socket bind失败
168浏览 • 1回复 待解决
HarmonyOS socket tcp连接报错
177浏览 • 1回复 待解决
socket连接NetAddress获取问题
510浏览 • 1回复 待解决
基于TLSSocket通信能力
559浏览 • 1回复 待解决
HarmonyOS socket接收文件问题
157浏览 • 1回复 待解决
HarmonyOS 蓝牙socket.sppWrite 报错
88浏览 • 1回复 待解决