• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 安全单元访问开发指南
2
3## 简介
4安全单元(SecureElement,简称SE),电子设备上可能存在一个或多个安全单元,比如有eSE(Embedded SE)和SIM卡。能够充当安全单元的SIM卡,要求具备NFC功能。
5
6## 场景介绍
7应用程序可以通过接口访问安全单元,比如往安全单元里面写入数据,实现在电子设备上模拟一张NFC卡片的目的。该卡片数据可能存储在eSE安全单元,或在SIM卡安全单元上。安全单元上一般会预置有访问控制规则,应用程序需要具备对应的权限,也就是通过安全单元的访问控制权限校验之后,才能正常访问安全单元。
8
9## 接口说明
10完整的JS API说明以及实例代码请参考:[安全单元接口](../../reference/apis-connectivity-kit/js-apis-secureElement.md)。
11实现安全单元的访问,可能使用到下面的接口。
12
13| 接口名                             | 功能描述                                                                       |
14| ---------------------------------- | ------------------------------------------------------------------------------ |
15| createService(): Promise\<SEService>                    | 建立一个可用于连接到系统中所有可用SE的新连接。                                                               |
16| getReaders(): Reader[]                      | 返回可用SE Reader的数组,包含该设备上支持的所有的安全单元。                                                                |
17| openSession(): Session                 | 在SE Reader实例上创建连接会话,返回Session实例。                                                                |
18| openLogicalChannel(aid: number[]): Promise\<Channel>                  | 打开逻辑通道,返回逻辑Channel实例对象。                                                                |
19| transmit(command: number[]): Promise\<number[]> | 向SE发送APDU数据                                                                |
20| close(): void | 关闭Channel。                                                            |
21
22
23## 主要场景开发步骤
24
25### 应用程序访问安全单元
261. import需要的安全单元模块。
272. 判断设备是否支持安全单元能力。
283. 访问安全单元,实现数据的读取或写入。
29
30```ts
31import { omapi } from '@kit.ConnectivityKit';
32import { BusinessError } from '@kit.BasicServicesKit';
33import { hilog } from '@kit.PerformanceAnalysisKit';
34import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
35
36let seService : omapi.SEService;
37let seReaders : omapi.Reader[];
38let seSession : omapi.Session;
39let seChannel : omapi.Channel;
40let aidArray : number[] = [0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10];
41let p2 : number = 0x00;
42
43export default class EntryAbility extends UIAbility {
44  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
45    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
46
47    // 判断设备是否支持安全单元能力
48    if (!canIUse("SystemCapability.Communication.SecureElement")) {
49      hilog.error(0x0000, 'testTag', 'secure element unavailable.');
50      return;
51    }
52    hilog.info(0x0000, 'testTag', 'secure element available.');
53    this.omaTest();
54  }
55
56  private async omaTest () {
57    // 获取 service
58    await omapi.createService().then((data) => {
59      if (data == undefined || !data.isConnected()) {
60        hilog.error(0x0000, 'testTag', 'secure element service disconnected.');
61        return;
62      }
63      seService = data;
64      hilog.info(0x0000, 'testTag', 'secure element service connected.');
65    }).catch((error: BusinessError) => {
66      hilog.error(0x0000, 'testTag', 'createService error %{public}s', JSON.stringify(error));
67      return;
68    });
69
70    // 获取 readers
71    try {
72      seReaders = seService.getReaders();
73    } catch (error) {
74      hilog.error(0x0000, 'testTag', 'getReaders error %{public}s', JSON.stringify(error));
75    }
76    if (seReaders == undefined || seReaders.length == 0) {
77      hilog.error(0x0000, 'testTag', 'no valid reader found.');
78      seService.shutdown();
79      return;
80    }
81    let reader: (omapi.Reader | undefined);
82    for (let i = 0; i < seReaders.length; ++i) {
83      let r = seReaders[i];
84      if (r.getName().includes("SIM")) {
85        reader = r;
86        break;
87      }
88    }
89    if (reader == undefined) {
90      hilog.error(0x0000, 'testTag', 'no valid sim reader.');
91      return;
92    }
93    hilog.info(0x0000, 'testTag', 'reader is %{public}s', reader?.getName());
94
95    // 获取 session
96    try {
97      seSession = reader?.openSession() as omapi.Session;
98    } catch (error) {
99      hilog.error(0x0000, 'testTag', 'openSession error %{public}s', JSON.stringify(error));
100    }
101    if (seSession == undefined) {
102      hilog.error(0x0000, 'testTag', 'seSession invalid.');
103      seService.shutdown();
104      return;
105    }
106
107    // 获取 channel
108    try {
109      // change the aid value for open logical channel
110      // 修改为打开逻辑通道的应用的aid值
111      seChannel = await seSession.openLogicalChannel(aidArray, p2);
112    } catch (exception) {
113      hilog.error(0x0000, 'testTag', 'openLogicalChannel exception %{public}s', JSON.stringify(exception));
114    }
115
116    if (seChannel == undefined) {
117      hilog.error(0x0000, 'testTag', 'seChannel invalid.');
118      return;
119    }
120
121    // 发送数据
122    let cmdData = [0x01, 0x02, 0x03, 0x04]; // 请更改为正确的data
123    try {
124      let response: number[] = await seChannel.transmit(cmdData)
125      hilog.info(0x0000, 'testTag', 'seChannel.transmit() response = %{public}s.', JSON.stringify(response));
126    } catch (exception) {
127      hilog.error(0x0000, 'testTag', 'seChannel.transmit() exception = %{public}s.', JSON.stringify(exception));
128    }
129
130    // 关闭通道,必须确保通道最终关闭
131    try {
132      seChannel.close();
133    } catch (exception) {
134      hilog.error(0x0000, 'testTag', 'seChannel.close() exception = %{public}s.', JSON.stringify(exception));
135    }
136
137  }
138}
139
140```