【HarmonyOS 5】鸿蒙TEE(可信执行环境)详解 原创

GeorgeGcs
发布于 2025-6-27 18:26
浏览
0收藏

【HarmonyOS 5】鸿蒙TEE(可信执行环境)详解

一、TEE是什么?

1、TEE的定义:
可信执行环境(Trusted Execution Environment),简称TEE,是存在于智能手机、平板或任意移动设备主处理器中的一个安全区域,确保各种敏感数据在一个可信环境中被存储、处理和受到保护。

2、TEE的作用
简单来说,TEE构建一个隔离的安全执行环境。该环境具备独立的计算和存储能力,能确保在其中运行的程序和数据受到严格保护,即使主系统被GJ或篡改,TEE内的内容也能保持安全。

TEE为授权安全软件,也称为“可信应用”提供一个安全的执行环境,通过实施保护、保密性、完整性和数据访问权限确保端到端的安全。

3、TEE的能力归属:
TEE在鸿蒙系统中以Kit的形式,提供给App使用。在Device Security Kit中包括应用设备状态检测、安全检测、可信应用服务、业务风险检测能力。

二、如何使用TEE?

目前提供可信应用服务,例如:安全摄像头场景,安全地址位置场景,安全图像压缩和裁剪场景。

【从API12开始,开发者需要开通可信应用服务才可以正常使用接口,否则调用接口会抛出异常,已上架的应用需要开通服务后重新上架。】

1、首先我们需要给App开通可信应用服务:
默认在AGC平台是没有该设置入门。需要App通过白名单审核,审核通过后方可开通可信应用服务。
说明。
开通“可信应用服务”需要先申请进入允许清单,请将Developer ID、公司名称、应用名称、申请使用的服务和使用该服务的场景,发送到agconnect@huawei.com。AGC运营将审核相关材料,通过后将为您配置受限开放服务使用的名单,审核周期为1-3个工作日,请耐心等待。

可信应用服务:点击“可信应用服务”右侧的按钮,接入“可信应用服务”:
【HarmonyOS 5】鸿蒙TEE(可信执行环境)详解-鸿蒙开发者社区
2、可信服务调用流程:
我们以为安全相机为例讲解:
【HarmonyOS 5】鸿蒙TEE(可信执行环境)详解-鸿蒙开发者社区

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*480
    this.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
    );
  }

  // 创建预览Surface
  createPreviewSurface(): string {
    // 这里需要实现创建预览Surface的逻辑
    // 实际开发中可能需要使用鸿蒙的UI组件来创建预览Surface
    return '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 {
    // 将数据转换为ArrayBuffer
    const 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 {
    // 将数据转换为ArrayBuffer
    const 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);
  }
}

注意事项

  1. 权限要求:使用TEE功能需要在config.json中声明相应权限:

    {
      "requestPermissions": [
        {
          "name": "ohos.permission.USE_TRUSTED_ENVIRONMENT",
          "reason": "需要访问TEE环境"
        }
      ]
    }
    
  2. 数据大小限制:TEE的安全内存通常有限,不要存储过大的数据。

  3. 生命周期管理:确保在不需要时及时释放TEE资源,避免内存泄漏。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2025-6-30 09:09:00修改
收藏
回复
举报
回复
    相关推荐