【HarmonyOS 5】鸿蒙TEE(可信执行环境)详解
一、TEE是什么?
1、TEE的定义:
可信执行环境(Trusted Execution Environment),简称TEE,是存在于智能手机、平板或任意移动设备主处理器中的一个安全区域,确保各种敏感数据在一个可信环境中被存储、处理和受到保护。
2、TEE的作用
简单来说,TEE构建一个隔离的安全执行环境。该环境具备独立的计算和存储能力,能确保在其中运行的程序和数据受到严格保护,即使主系统被GJ或篡改,TEE内的内容也能保持安全。
TEE为授权安全软件,也称为“可信应用”提供一个安全的执行环境,通过实施保护、保密性、完整性和数据访问权限确保端到端的安全。
3、TEE的能力归属:
TEE在鸿蒙系统中以Kit的形式,提供给App使用。在Device Security Kit中包括应用设备状态检测、安全检测、可信应用服务、业务风险检测能力。
4、硬件级安全保障
依赖芯片级的安全能力(如ARM的TrustZone、Intel的SGX等技术),通过硬件隔离机制确保TEE的安全性,降低软件层面被GJ的风险。
5、 与鸿蒙系统深度集成
适配鸿蒙的微内核架构和分布式特性,可在多设备(如手机、智能手表、智能家居设备)之间实现安全能力的协同,例如在分布式场景下统一管理用户身份和数据安全。
6、动态信任链机制
从系统启动阶段就建立可信根,通过信任链传递确保TEE环境的完整性,防止启动过程中被恶意篡改。
7、 移动支付与金融安全
在进行扫码支付、银行卡信息输入等操作时,鸿蒙TEE可保护支付密钥和交易数据,防止被恶意软件窃取,类似传统手机中TEE用于保护银联闪付等功能。
8、生物特征识别
存储和处理指纹、人脸等生物特征数据,确保解锁、支付等场景中生物信息的安全,例如手机解锁时,生物特征的比对过程在TEE中完成。
9、 数字版权管理(DRM)
保护视频、音乐等数字内容的版权,确保只有授权设备和用户才能播放加密的媒体文件,防止盗版内容传播。
10、 企业级安全应用
为企业提供安全的身份认证、数据加密传输等功能,例如在鸿蒙平板或笔记本中,TEE可保护企业机密文件和远程办公的安全连接。
二、如何使用TEE?
目前提供可信应用服务,例如:安全摄像头场景,安全地址位置场景,安全图像压缩和裁剪场景。
【从API12开始,开发者需要开通可信应用服务才可以正常使用接口,否则调用接口会抛出异常,已上架的应用需要开通服务后重新上架。】
1、首先我们需要给App开通可信应用服务:
默认在AGC平台是没有该设置入门。需要App通过白名单审核,审核通过后方可开通可信应用服务。
说明。
开通“可信应用服务”需要先申请进入允许清单,请将Developer ID、公司名称、应用名称、申请使用的服务和使用该服务的场景,发送到agconnect@huawei.com。AGC运营将审核相关材料,通过后将为您配置受限开放服务使用的名单,审核周期为1-3个工作日,请耐心等待。
可信应用服务:点击“可信应用服务”右侧的按钮,接入“可信应用服务”:
2、可信服务调用流程:
我们以为安全相机为例讲解:
import { camera } from '@kit.CameraKit';
import { image } from '@kit.ImageKit';@Entry
@Component
struct SecureCameraDemo {private cameraManager: camera.CameraManager = camera.getCameraManager();private secureCamera: camera.CameraDevice | null = null;private cameraInput: camera.CameraInput | null = null;private previewOutput: camera.PreviewOutput | null = null;private secureOutput: camera.PreviewOutput | null = null;private secureSession: camera.SecureSession | null = null;private imageReceiver: image.ImageReceiver | null = null;private previewProfile: camera.Profile | null = null;private secureCameraSerialNumber: bigint | null = null;private previewSurfaceId: string = '';aboutToAppear() {this.initSecureCamera();}aboutToDisappear() {this.releaseResources();}// 初始化安全相机async initSecureCamera() {try {// 选择支持安全相机的设备await this.selectSecureCameraDevice();if (!this.secureCamera) {console.error('未找到支持安全相机的设备');return;}// 查询相机设备在安全模式下支持的输出能力await this.getSecureCameraOutputCapability();if (!this.previewProfile) {console.error('未获取到支持的预览配置');return;}// 创建设备输入输出await this.createInputAndOutputs();// 打开安全设备await this.openSecureCamera();// 创建安全相机会话,配流启流await this.openSecureSession();// 注册安全数据流回调this.registerSecureDataCallback();} catch (error) {console.error(`初始化安全相机失败: ${JSON.stringify(error)}`);}}// 选择支持安全相机的设备async selectSecureCameraDevice() {const cameraArray = await this.cameraManager.getSupportedCameras();for (const cameraDevice of cameraArray) {if (await this.isSecureCamera(cameraDevice)) {this.secureCamera = cameraDevice;console.info('找到支持安全相机的设备');break;}}}// 判断设备是否支持安全相机async isSecureCamera(cameraDevice: camera.CameraDevice): Promise<boolean> {const sceneModes = await this.cameraManager.getSupportedSceneModes(cameraDevice);const secureMode = sceneModes.find(mode => mode === camera.SceneMode.SECURE_PHOTO);return secureMode !== undefined;}// 查询相机设备在安全模式下支持的输出能力async getSecureCameraOutputCapability() {if (!this.secureCamera) return;const outputCap = await this.cameraManager.getSupportedOutputCapability(this.secureCamera, camera.SceneMode.SECURE_PHOTO);// 选择推荐的预览分辨率 640*480this.previewProfile = outputCap.previewProfiles.find(profile => profile.size.width === 640 && profile.size.height === 480);// 如果没有找到 640*480 的分辨率,选择第一个可用的if (!this.previewProfile && outputCap.previewProfiles.length > 0) {this.previewProfile = outputCap.previewProfiles[0];}}// 创建设备输入输出async createInputAndOutputs() {if (!this.secureCamera || !this.previewProfile) return;// 创建输入流this.cameraInput = await this.cameraManager.createCameraInput(this.secureCamera);// 创建普通预览输出流this.previewSurfaceId = this.createPreviewSurface();this.previewOutput = await this.cameraManager.createPreviewOutput(this.previewProfile, this.previewSurfaceId);// 创建安全数据输出流this.imageReceiver = image.createImageReceiver({ width: this.previewProfile.size.width, height: this.previewProfile.size.height },image.ImageFormat.JPEG, 8);const secureSurfaceId = await this.imageReceiver.getReceivingSurfaceId();this.secureOutput = await this.cameraManager.createPreviewOutput(this.previewProfile, secureSurfaceId);}// 创建预览SurfacecreatePreviewSurface(): string {// 这里需要实现创建预览Surface的逻辑// 实际开发中可能需要使用鸿蒙的UI组件来创建预览Surfacereturn 'previewSurfaceId';}// 打开安全设备async openSecureCamera() {if (!this.cameraInput) return;// 打开安全相机并获取序列号this.secureCameraSerialNumber = await this.cameraInput.open(true);console.info(`安全相机已打开,序列号: ${this.secureCameraSerialNumber}`);// 使用序列号创建证明密钥和初始化证明会话// 注意:这部分需要调用DeviceSecurity Kit的API,此处仅为示例this.initializeAttestationSession(this.secureCameraSerialNumber);}// 初始化证明会话(调用DeviceSecurity Kit)initializeAttestationSession(serialNumber: bigint) {// 实际开发中需要调用DeviceSecurity Kit的API// 以下为示例代码,需要根据实际API进行调整console.info(`使用安全相机序列号 ${serialNumber} 初始化证明会话`);// const deviceSecurityKit = ...; // 获取DeviceSecurity Kit实例// deviceSecurityKit.createAttestationKey(serialNumber);// deviceSecurityKit.initializeAttestationSession(...);}// 创建安全相机会话,配流启流async openSecureSession() {if (!this.cameraManager || !this.cameraInput || !this.previewOutput || !this.secureOutput) return;try {this.secureSession = await this.cameraManager.createSession(camera.SceneMode.SECURE_PHOTO);if (!this.secureSession) {console.error('创建安全会话失败');return;}await this.secureSession.beginConfig();await this.secureSession.addInput(this.cameraInput);await this.secureSession.addOutput(this.previewOutput);await this.secureSession.addOutput(this.secureOutput);await this.secureSession.addSecureOutput(this.secureOutput); // 标记为安全输出await this.secureSession.commitConfig();await this.secureSession.start();console.info('安全会话已启动');} catch (error) {console.error(`打开安全会话失败: ${JSON.stringify(error)}`);}}// 注册安全数据流回调registerSecureDataCallback() {if (!this.imageReceiver) return;this.imageReceiver.on('imageArrival', async () => {try {const img = await this.imageReceiver!.readNextImage();const component = await img.getComponent(image.ComponentType.JPEG);const buffer = component.byteBuffer;// 将安全数据发送到服务器进行验证this.sendSecureDataToServer(buffer);// 处理完后释放图像资源img.release();} catch (error) {console.error(`处理安全数据失败: ${JSON.stringify(error)}`);}});}// 将安全数据发送到服务器进行验证sendSecureDataToServer(buffer: ArrayBuffer) {// 实际开发中需要实现将安全数据发送到服务器的逻辑console.info(`发送安全数据到服务器,数据大小: ${buffer.byteLength} 字节`);// 示例:使用fetch API发送数据/*fetch('https://your-server.com/verify-secure-data', {method: 'POST',body: buffer,headers: {'Content-Type': 'application/octet-stream','Secure-Camera-Serial': this.secureCameraSerialNumber?.toString() || ''}}).then(response => response.json()).then(result => {console.info('服务器验证结果:', result);}).catch(error => {console.error('发送安全数据失败:', error);});*/}// 释放资源async releaseResources() {try {// 停止会话if (this.secureSession) {await this.secureSession.stop();await this.secureSession.release();this.secureSession = null;}// 释放输出if (this.previewOutput) {await this.previewOutput.release();this.previewOutput = null;}if (this.secureOutput) {await this.secureOutput.release();this.secureOutput = null;}// 释放输入if (this.cameraInput) {await this.cameraInput.release();this.cameraInput = null;}// 释放图像接收器if (this.imageReceiver) {this.imageReceiver.release();this.imageReceiver = null;}console.info('安全相机资源已释放');} catch (error) {console.error(`释放资源失败: ${JSON.stringify(error)}`);}}build() {Column() {Text('安全相机演示').fontSize(20).fontWeight(FontWeight.Bold).margin({ top: 20, bottom: 20 })// 这里可以添加预览界面组件// 实际开发中需要根据鸿蒙的UI组件来实现预览显示Text('安全相机预览区域').width('100%').height(300).backgroundColor('#CCCCCC').textAlign(TextAlign.Center).margin({ bottom: 20 })Button('释放相机').onClick(() => {this.releaseResources();})}.width('100%').height('100%').padding(15)}
}
三、环境隔离的使用方法
综上所述,基于TEE的环境。我们将敏感信息加密后,放在TEE环境中,就可以做到最高的安全保护。
对用户的敏感数据(如生物特征信息、支付密码、加密密钥等)进行加密存储和处理,防止数据被未授权的程序或进程访问。
1. 使用DeviceSecurity Kit访问TEE
HarmonyOS提供了DeviceSecurity Kit
来访问TEE功能,包括创建安全密钥、执行安全操作和存储敏感数据。主要步骤如下:
步骤1:导入依赖
import { deviceSecurity } from '@ohos.security.deviceSecurity';
步骤2:获取TEE会话
async function getTEESession() {try {// 获取TEE服务实例const teeService = await deviceSecurity.getTrustedExecutionEnvironmentService();// 创建安全会话(根据实际需求选择合适的安全级别)const session = await teeService.createSession({authType: deviceSecurity.AuthType.ALL, // 认证类型authLevel: deviceSecurity.AuthLevel.SYSTEM, // 认证级别userId: 0 // 用户ID});return session;} catch (error) {console.error(`获取TEE会话失败: ${JSON.stringify(error)}`);return null;}
}
2. 在TEE中缓存数据
在TEE环境中缓存数据有两种主要方式:安全存储和安全内存。
方式1:安全存储(持久化缓存)
使用TrustedStorage
接口将数据加密存储在TEE中:
async function cacheDataSecurely(session: deviceSecurity.TrustedExecutionEnvironmentSession, key: string, data: string) {try {// 将数据转换为ArrayBufferconst dataBuffer = new TextEncoder().encode(data);// 创建安全存储对象const trustedStorage = await session.getTrustedStorage();// 存储数据(自动加密)await trustedStorage.save(key, dataBuffer);console.info(`数据已安全存储,键: ${key}`);} catch (error) {console.error(`安全存储失败: ${JSON.stringify(error)}`);}
}
方式2:安全内存(临时缓存)
使用TrustedMemory
接口在TEE的安全内存中临时缓存数据:
async function cacheDataInSecureMemory(session: deviceSecurity.TrustedExecutionEnvironmentSession, data: string) {try {// 将数据转换为ArrayBufferconst dataBuffer = new TextEncoder().encode(data);// 创建安全内存区域const trustedMemory = await session.allocateTrustedMemory(dataBuffer.byteLength);// 写入数据到安全内存await trustedMemory.write(dataBuffer);console.info('数据已缓存到安全内存');return trustedMemory;} catch (error) {console.error(`安全内存缓存失败: ${JSON.stringify(error)}`);return null;}
}
3. 从TEE中读取缓存数据
根据存储方式的不同,读取数据的方法也不同:
读取安全存储数据
async function readSecurelyStoredData(session: deviceSecurity.TrustedExecutionEnvironmentSession, key: string) {try {const trustedStorage = await session.getTrustedStorage();const dataBuffer = await trustedStorage.read(key);// 将ArrayBuffer转换为字符串const data = new TextDecoder().decode(dataBuffer);return data;} catch (error) {console.error(`读取安全存储数据失败: ${JSON.stringify(error)}`);return null;}
}
读取安全内存数据
async function readDataFromSecureMemory(trustedMemory: deviceSecurity.TrustedMemory) {try {const dataBuffer = await trustedMemory.read();const data = new TextDecoder().decode(dataBuffer);return data;} catch (error) {console.error(`读取安全内存数据失败: ${JSON.stringify(error)}`);return null;}
}
4. 释放资源
使用完毕后,需要释放TEE资源以确保安全性:
async function releaseTEEResources(session: deviceSecurity.TrustedExecutionEnvironmentSession, trustedMemory?: deviceSecurity.TrustedMemory) {try {// 释放安全内存(如果有)if (trustedMemory) {await trustedMemory.release();}// 关闭会话await session.close();console.info('TEE资源已释放');} catch (error) {console.error(`释放TEE资源失败: ${JSON.stringify(error)}`);}
}
完整示例
下面是一个完整的示例,演示如何在TEE中缓存和读取数据:
import { deviceSecurity } from '@ohos.security.deviceSecurity';async function demoTEECaching() {// 1. 获取TEE会话const session = await getTEESession();if (!session) return;try {// 2. 缓存数据到安全存储await cacheDataSecurely(session, 'sensitiveKey', '这是敏感数据');// 3. 从安全存储读取数据const storedData = await readSecurelyStoredData(session, 'sensitiveKey');console.info(`从安全存储读取的数据: ${storedData}`);// 4. 缓存数据到安全内存const secureMemory = await cacheDataInSecureMemory(session, '临时敏感数据');// 5. 从安全内存读取数据if (secureMemory) {const memoryData = await readDataFromSecureMemory(secureMemory);console.info(`从安全内存读取的数据: ${memoryData}`);}} catch (error) {console.error(`TEE数据缓存演示失败: ${JSON.stringify(error)}`);} finally {// 6. 释放资源await releaseTEEResources(session);}
}
注意事项
-
权限要求:使用TEE功能需要在
config.json
中声明相应权限:{"requestPermissions": [{"name": "ohos.permission.USE_TRUSTED_ENVIRONMENT","reason": "需要访问TEE环境"}] }
-
数据大小限制:TEE的安全内存通常有限,不要存储过大的数据。
-
生命周期管理:确保在不需要时及时释放TEE资源,避免内存泄漏。
-
错误处理:TEE操作可能因硬件限制或安全策略失败,需要完善的错误处理。
-
兼容性:不同设备的TEE实现可能存在差异,建议进行充分测试。