Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
Image data captured by a camera in secure mode is signed by creating an attestation key and initializing an attestation session, to ensure authenticity and integrity of the image data.
This feature requires that the device supports the secure camera mode.
You can call the getSupportedSceneModes method to check whether the current device supports the secure camera mode. If the return value is camera.SceneMode.SECURE_PHOTO, the device supports the secure camera mode. If other values are returned, the device does not support the secure camera mode. The following is an example:
- import { camera } from '@kit.CameraKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- // Obtain the secure camera.
- function getSecureCameraDevice(cameraManager: camera.CameraManager): camera.CameraDevice {
- // Obtain the list of cameras supported by the device.
- const cameraDevices = cameraManager.getSupportedCameras();
- if (cameraDevices.length < 1) {
- throw new Error('no camera devices');
- }
- // Obtain the front camera object. Currently, the secure camera must be a front camera.
- const frontCamera: camera.CameraDevice | undefined = cameraDevices.find((profile: camera.CameraDevice) => {
- return profile.cameraPosition != camera.CameraPosition.CAMERA_POSITION_BACK;
- });
- if (frontCamera === undefined) {
- throw new Error('no front cameras');
- }
- // Check whether the front camera supports the secure camera mode. If yes, the front camera can be used for subsequent secure camera operations.
- const modes = cameraManager.getSupportedSceneModes(frontCamera);
- if (modes.indexOf(camera.SceneMode.SECURE_PHOTO) === -1) {
- throw new Error('current device not support secure camera');
- }
- return frontCamera;
- }

For details about the APIs and their usage, see API Reference.
| API | Description |
|---|---|
| createAttestKey(options: AttestOptions): Promise<void> | Creates an attestation key. |
| initializeAttestContext(userData: string, options: AttestOptions): Promise<AttestReturnResult> | Initializes an attestation session. |
| finalizeAttestContext(options: AttestOptions): Promise<void> | Ends an attestation session. |
| destroyAttestKey(): Promise<void> | Destroys an attestation key. |
Import the camera module, trustedAppService module, and dependent modules.
- import { camera } from '@kit.CameraKit';
- import { trustedAppService } from '@kit.DeviceSecurityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
Initialize the secure camera by referring to the secure camera development guide.
You need to:
Create an attestation key and initialize an attestation session.
An attestation session can be initialized only after an attestation key is successfully created.
An attestation key is valid for seven days. To avoid repeated creation of attestation keys, you are advised to call the attestation session initialization API first. If the initialization fails, destroy and create the attestation key, and initialize the attestation key again.
The device serial number needs to be obtained each time the secure mode is enabled for a camera, and passed for initializing an attestation session for the camera in secure mode. In other scenarios, the value 0 can be passed.
When initializeAttestContext is called to initialize an attestation session, the value of userData must contain 16 to 127 bytes.
- // Parameters for creating an attestation key.
- const createProperties: Array<trustedAppService.AttestParam> = [
- {
- tag: trustedAppService.AttestTag.ATTEST_TAG_ALGORITHM,
- value: trustedAppService.AttestKeyAlg.ATTEST_ALG_ECC
- },
- {
- tag: trustedAppService.AttestTag.ATTEST_TAG_KEY_SIZE,
- value: trustedAppService.AttestKeySize.ATTEST_ECC_KEY_SIZE_256
- }
- ];
- const createOptions: trustedAppService.AttestOptions = {
- properties: createProperties
- };
- // Parameters for initializing an attestation session.
- const userData = "trusted_app_service_demo"; // Example value. Generate a value on your own, which contains 16 to 127 bytes.
- const deviceId = 7483679320805398131; // Example value. Obtain the value from Camera Engine.
- const initProperties: Array<trustedAppService.AttestParam> = [
- {
- tag: trustedAppService.AttestTag.ATTEST_TAG_DEVICE_TYPE,
- value: trustedAppService.AttestType.ATTEST_TYPE_CAMERA
- },
- {
- tag: trustedAppService.AttestTag.ATTEST_TAG_DEVICE_ID,
- value: BigInt(deviceId)
- }
- ];
- const initOptions: trustedAppService.AttestOptions = {
- properties: initProperties
- };
- // Create an attestation key and initialize an attestation session.
- let certChainList: Array<string>;
- try {
- await trustedAppService.createAttestKey(createOptions);
- const result = await trustedAppService.initializeAttestContext(userData, initOptions);
- certChainList = result.certChains;
- } catch (err) {
- const error = err as BusinessError;
- console.error(`Failed to initialize attest context, message:${error.message}, code:${error.code}`);
- }
Create a secure camera session, configure the input and output streams, and start the preview stream and secure data stream by referring to the secure camera development guide.
Ends an attestation session.
- // Parameters for ending the attestation session.
- const finalProperties: Array<trustedAppService.AttestParam> = [
- {
- tag: trustedAppService.AttestTag.ATTEST_TAG_DEVICE_TYPE,
- value: trustedAppService.AttestType.ATTEST_TYPE_CAMERA
- }
- ];
- const finalOptions: trustedAppService.AttestOptions = {
- properties: finalProperties,
- };
- // End the attestation session.
- try {
- await trustedAppService.finalizeAttestContext(finalOptions);
- } catch (err) {
- const error = err as BusinessError;
- console.error(`Failed to finalize attest context, message:${error.message}, code:${error.code}`);
- }
To destroy an attestation key, call the destroyAttestKey API after the attestation session ends. Image security verification, location security verification, and secure image compression and cropping share the same attestation key. Before destroying an attestation key, ensure that the location security verification function is not using the attestation key.
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.