harmony(鸿蒙)TLSSocket

2022-10-28 浏览 (831)

TLSSocket

The Transport Layer Security (TLS) protocol is designed to help protect the privacy of information at the transport layer. TLSSocket is an extension to socket communication. It provides higher security than socket communication by adding a security protection layer, which consists of the following submodules: key, certificate, and communication.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import socket from '@ohos.net.tlssocket'

socket.constructTLSSocketInstance

constructTLSSocketInstance(): TLSSocket

Creates a TLSSocket object.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Example

let tlssocket = socket.constructTLSSocketInstance();

tlssocket.connect

connect(options: TLSConnectOptions, callback: AsyncCallback<void>): void

Sets up a TLSSocket connection, and creates and initializes a TLS session. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.

Parameters

NameTypeMandatoryDescription
optionsTLSConnectOptionsYesParameters required for the connection.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.

Example

let options = {
            ALPNProtocols: ["spdy/1", "http/1.1"],
            address: {
                address: "xxx",
                port: "xxxx",
                family: 1,
            },
            secureOptions: {
                key: "xxxx",
                cert: "xxxx",
                ca: ["xxxx"],
                passwd: "xxxx",
                protocols: "TlsV1_2",
                useRemoteCipherPrefer: true,
                signatureAlgorithms: SHA256,
                cipherSuites: AES256-SHA256,
            },
};

tlssocket.connect(options, (err, data) => {
    console.info(err);
    console.info(data);
});

tlssocket.connect

connect(options: TLSConnectOptions): Promise<void>;

Sets up a TLSSocket connection, and creates and initializes a TLS session. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
optionsTLSConnectOptionsYesParameters required for the connection.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.

Example

let options = {
            ALPNProtocols: ["spdy/1", "http/1.1"],
            address: {
                address: "xxxx",
                port: "xxxx",
                family: 1,
            },
            secureOptions: {
                key: "xxxx",
                cert: "xxxx",
                ca: ["xxxx"],
                passwd: "xxxx",
                protocols: "TlsV1_2",
                useRemoteCipherPrefer: true,
                signatureAlgorithms: SHA256,
                cipherSuites: AES256-SHA256,
            },
};

tlssocket.connect(options).then(data => {
    console.info(data);
}).catch(err => {
    console.error(err);
});

tlssocket.getCertificate

getCertificate(callback: AsyncCallback<string>): void;

Obtains the local digital certificate after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<string>YesCallback used to return the result.

Example

tlssocket.getCertificate((err, data) => {
  if (err) {
    console.log("getCertificate callback error = " + err);
  } else {
    console.log("getCertificate callback = " + data);
  }
});

tlssocket.getCertificate

getCertificate():Promise<string>;

Obtains the local digital certificate after a TLSSocket connection is established. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<string>Promise used to return the result.

Example

tlssocket.getCertificate().then(data => {
  console.info(data);
}).catch(err => {
  console.error(err);
});

tlssocket.getRemoteCertificate

getRemoteCertificate(callback: AsyncCallback<string>): void;

Obtains the remote digital certificate after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<string>YesCallback used to return the result.

Example

tlssocket.getRemoteCertificate((err, data) => {
  if (err) {
    console.log("getRemoteCertificate callback error = " + err);
  } else {
    console.log("getRemoteCertificate callback = " + data);
  }
});

tlssocket.getRemoteCertificate

getRemoteCertificate():Promise<string>;

Obtains the remote digital certificate after a TLSSocket connection is established. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<string>Promise used to return the result.

Example

tlssocket.getRemoteCertificate().then(data => {
  console.info(data);
}).catch(err => {
  console.error(err);
});

tlssocket.getProtocol

getProtocol(callback: AsyncCallback<string>): void;

Obtains the communication protocol after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<string>YesCallback used to return the result.

Example

tlssocket.getProtocol((err, data) => {
  if (err) {
    console.log("getProtocol callback error = " + err);
  } else {
    console.log("getProtocol callback = " + data);
  }
});

tlssocket.getProtocol

getProtocol():Promise<string>;

Obtains the communication protocol after a TLSSocket connection is established. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<string>Promise used to return the result.

Example

tlssocket.getProtocol().then(data => {
  console.info(data);
}).catch(err => {
  console.error(err);
});

tlssocket.getCipherSuites

getCipherSuites(callback: AsyncCallback<Array<string>>): void;

Obtains the cipher suites supported by both parties after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<string>>YesCallback used to return the result.

Example

tlssocket.getCipherSuites((err, data) => {
  if (err) {
    console.log("getCipherSuites callback error = " + err);
  } else {
    console.log("getCipherSuites callback = " + data);
  }
});

tlssocket.getCipherSuites

getCipherSuites(): Promise<Array<string>>;

Obtains the cipher suites supported by both parties after a TLSSocket connection is established. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<Array<string>>Promise used to return the result.

Example

tlssocket.getCipherSuites().then(data => {
  console.info(data);
}).catch(err => {
  console.error(err);
});

tlssocket.getSignatureAlgorithms

getSignatureAlgorithms(callback: AsyncCallback<Array<string>>): void;

Obtains the signing algorithms supported by both parties after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<string>>YesCallback used to return the result.

Example

tlssocket.getSignatureAlgorithms((err, data) => {
  if (err) {
    console.log("getSignatureAlgorithms callback error = " + err);
  } else {
    console.log("getSignatureAlgorithms callback = " + data);
  }
});

tlssocket.getSignatureAlgorithms

getSignatureAlgorithms(): Promise<Array<string>>;

Obtains the signing algorithms supported by both parties after a TLSSocket connection is established. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<Array<string>>Promise used to return the result.

Example

tlssocket.getSignatureAlgorithms().then(data => {
  console.info(data);
}).catch(err => {
  console.error(err);
});

tlssocket.close

close(callback: AsyncCallback<void>): void;

Closes a TLSSocket connection. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

tlssocket.close((err) => {
  if (err) {
    console.log("close callback error = " + err);
  } else {
    console.log("close success");
  }
});

tlssocket.close

close(): Promise<void>;

Closes a TLSSocket connection. This API uses a promise to return the result.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return value

TypeDescription
Promise<void>Promise used to return the result.

Example

tlssocket.close().then(() =>
  console.log("close success");
}).catch(err => {
  console.error(err);
});

TLSConnectOptions

Defines a TLSSocket connection.

System capability: SystemCapability.Communication.NetStack

NameTypeDescription
addressNetAddressGateway address.
secureOptionsTLSSecureOptionsTLS security options.
ALPNProtocolsArray<string>Application Layer Protocol Negotiation (ALPN) protocols.

NetAddress

Defines a network address.

System capability: SystemCapability.Communication.NetStack

NameTypeDescription
addressstringNetwork address.
familynumberAddress family identifier. The value is 1 for IPv4 and 2 for IPv6. The default value is 1.
portnumberPort number. The value ranges from 0 to 65535.

TLSSecureOptions

Defines TLS security options.

System capability: SystemCapability.Communication.NetStack

NameTypeDescription
castring |Array<string>CA certificate.
certstringLocal digital certificate.
keystringPrivate key of the local digital certificate.
passwdstringPassword.
protocolsstringProtocols.
useRemoteCipherPreferbooleanWhether to use the remote cipher suite preferentially.
signatureAlgorithmsstringSigning algorithms.
cipherSuitesstringCipher suites.

你可能感兴趣的鸿蒙文章

harmony(鸿蒙)APIs

harmony(鸿蒙)API Reference Document Description

harmony(鸿蒙)BundleStatusCallback

harmony(鸿蒙)innerBundleManager

harmony(鸿蒙)distributedBundle

harmony(鸿蒙)Bundle

harmony(鸿蒙)Context

harmony(鸿蒙)DataUriUtils

harmony(鸿蒙)EnterpriseAdminExtensionAbility

harmony(鸿蒙)Work Scheduler Callbacks

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/03b0582cabad474fad93e95dc23e34a4