blob: 22a466d08c43f86b4973c90e7a77fe9f119e17e3 [file] [log] [blame]
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001.. _trusted_applications:
2
3####################
4Trusted Applications
5####################
6There are two ways to implement Trusted Applications (TAs), Pseudo TAs and user
7mode TAs. User mode TAs are full featured Trusted Applications as specified by
8the :ref:`globalplatform_api` TEE specifications, these are simply the ones
9people are referring to when they are saying "Trusted Applications" and in most
10cases this is the preferred type of TA to write and use.
11
12.. _pta:
13
14Pseudo Trusted Applications
15***************************
16These are implemented directly to the OP-TEE core tree in, e.g.,
Jerome Forissierfa993db2019-09-13 15:58:53 +020017``core/pta`` and are built along with and statically built into the
Joakim Bech8e5c5b32018-10-25 08:18:32 +020018OP-TEE core blob.
19
20The Pseudo Trusted Applications included in OP-TEE already are OP-TEE secure
21privileged level services hidden behind a "GlobalPlatform TA Client" API. These
22Pseudo TAs are used for various purposes such as specific secure services or
23embedded tests services.
24
25Pseudo TAs **do not** benefit from the GlobalPlatform Core Internal API support
26specified by the GlobalPlatform TEE specs. These APIs are provided to TAs as a
27static library each TA shall link against (the ":ref:`libutee`") and that calls
28OP-TEE core service through system calls. As OP-TEE core does not link with
29:ref:`libutee`, Pseudo TAs can **only** use the OP-TEE core internal APIs and
30routines.
31
32As Pseudo TAs runs at the same privileged execution level as the OP-TEE core
33code itself and that might or might not be desirable depending on the use case.
34
35In most cases an unprivileged (user mode) TA is the best choice instead of
36adding your code directly to the OP-TEE core. However if you decide your
37application is best handled directly in OP-TEE core like this, you can look at
Jerome Forissierfa993db2019-09-13 15:58:53 +020038``core/pta/stats.c`` as a template and just add your Pseudo TA based on
Joakim Bech8e5c5b32018-10-25 08:18:32 +020039that to the ``sub.mk`` in the same directory.
40
41.. _user_mode_ta:
42
43User Mode Trusted Applications
44******************************
45User Mode Trusted Applications are loaded (mapped into memory) by OP-TEE core in
46the Secure World when something in Rich Execution Environment (REE) wants to
47talk to that particular application UUID. They run at a lower CPU privilege
48level than OP-TEE core code. In that respect, they are quite similar to regular
49applications running in the REE, except that they execute in Secure World.
50
51Trusted Application benefit from the GlobalPlatform :ref:`tee_internal_core_api`
52as specified by the GlobalPlatform TEE specifications. There are several types
53of user mode TAs, which differ by the way they are stored.
54
55TA locations
56************
57Plain TAs (user mode) can reside and be loaded from various places. There are
58three ways currently supported in OP-TEE.
59
60.. _early_ta:
61
62Early TA
63========
64The so-called early TAs are virtually identical to the REE FS TAs, but instead
65of being loaded from the Normal World file system, they are linked into a
66special data section in the TEE core blob. Therefore, they are available even
67before ``tee-supplicant`` and the REE's filesystems have come up. Please find
68more details in the `early TA commit`_.
69
70.. _ree_fs_ta:
71
72REE filesystem TA
73=================
74They consist of a cleartext signed ELF_ file, named from the UUID of the TA and
75the suffix ``.ta``. They are built separately from the OP-TEE core boot-time
76blob, although when they are built they use the same build system, and are
77signed with the key from the build of the original OP-TEE core blob.
78
79Because the TAs are signed, they are able to be stored in the untrusted REE
80filesystem, and ``tee-supplicant`` will take care of passing them to be checked
81and loaded by the Secure World OP-TEE core. Note that this type of TA isn't
82encrypted.
83
Jens Wiklander6054d682019-11-19 11:31:14 +010084REE filesystem TAs come in two formats, the legacy TA and the bootstrap TA.
85The bootstrap TA format is used by ``scripts/sign.py`` since version 3.7.0.
86
87All REE filesystems TAs has common header, ``struct shdr``, defined as:
88
89.. code-block:: c
90
91 enum shdr_img_type {
92 SHDR_TA = 0,
93 SHDR_BOOTSTRAP_TA = 1,
94 };
95
96 #define SHDR_MAGIC 0x4f545348
97
98 /**
99 * struct shdr - signed header
100 * @magic: magic number must match SHDR_MAGIC
101 * @img_type: image type, values defined by enum shdr_img_type
102 * @img_size: image size in bytes
103 * @algo: algorithm, defined by public key algorithms TEE_ALG_*
104 * from TEE Internal API specification
105 * @hash_size: size of the signed hash
106 * @sig_size: size of the signature
107 * @hash: hash of an image
108 * @sig: signature of @hash
109 */
110 struct shdr {
111 uint32_t magic;
112 uint32_t img_type;
113 uint32_t img_size;
114 uint32_t algo;
115 uint16_t hash_size;
116 uint16_t sig_size;
117 /*
118 * Commented out element used to visualize the layout dynamic part
119 * of the struct.
120 *
121 * hash is accessed through the macro SHDR_GET_HASH and
122 * signature is accessed through the macro SHDR_GET_SIG
123 *
124 * uint8_t hash[hash_size];
125 * uint8_t sig[sig_size];
126 */
127 };
128
129 #define SHDR_GET_SIZE(x) (sizeof(struct shdr) + (x)->hash_size + \
130 (x)->sig_size)
131 #define SHDR_GET_HASH(x) (uint8_t *)(((struct shdr *)(x)) + 1)
132 #define SHDR_GET_SIG(x) (SHDR_GET_HASH(x) + (x)->hash_size)
133
134
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100135The field ``img_type`` tells the type of TA, if it's ``SHDR_TA`` (0),
Jens Wiklander6054d682019-11-19 11:31:14 +0100136it's a legacy TA. If it's ``SHDR_BOOTSTRAP_TA`` (1) it's a bootstrap TA.
137
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100138The field ``algo`` tells the algorithm used. The script used to sign TAs
139currently uses ``TEE_ALG_RSASSA_PKCS1_V1_5_SHA256`` (0x70004830). This
140means RSA with PKCS#1v1.5 padding and SHA-256 hash function. OP-TEE accepts
141any of the ``TEE_ALG_RSASSA_PKCS1_*`` algorithms.
142
Jens Wiklander6054d682019-11-19 11:31:14 +0100143For bootstrap TAs ``struct shdr`` is followed by a subheader, ``struct
144shdr_bootstrap_ta`` which is defined as:
145
146.. code-block:: c
147
148 /**
149 * struct shdr_bootstrap_ta - bootstrap TA subheader
150 * @uuid: UUID of the TA
151 * @ta_version: Version of the TA
152 */
153 struct shdr_bootstrap_ta {
154 uint8_t uuid[sizeof(TEE_UUID)];
155 uint32_t ta_version;
156 };
157
158The fields ``uuid`` and ``ta_version`` allows extra checks to be performed
159when loading the TA. Currently only the ``uuid`` field is checked.
160
161Last in the TA binary follows the ELF file which normally is stripped
162as additional symbols etc will be ignored when loading the TA.
163
164Legacy TA binary is formatted as:
165
166.. code-block:: none
167
168 hash = H(<struct shdr> || <stripped ELF>)
169 signature = RSA-Sign(hash)
170 legacy_binary = <struct shdr> || <hash> || <signature> || <stripped ELF>
171
172Bootstrap TA binary is formatted as:
173
174.. code-block:: none
175
176 hash = H(<struct shdr> || <struct shdr_bootstrap_ta> || <stripped ELF>)
177 signature = RSA-Sign(<hash>)
178 bootstrap_binary = <struct shdr> || <hash> || <signature> ||
179 <struct shdr_bootstrap_ta> || <stripped ELF>
180
181A REE TA is loaded into shared memory using a series or RPC in
182:ref:`load_ree_ta`. The payload memory is allocated via TEE-supplicant and
183later freed when the TA has been loaded into secure memory in
184:ref:`free_appl_shm`.
185
186.. _load_ree_ta:
187
188.. figure:: ../images/trusted_applications/load_ree_ta.png
189 :figclass: align-center
190
191 Loading a REE TA into nonsecure shared memory
192
193.. _free_appl_shm:
194
195.. figure:: ../images/trusted_applications/free_appl_shm.png
196 :figclass: align-center
197
198 Freeing previously allocated nonsecure shared memory
199
200
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200201.. _secure_storage_ta:
202
203Secure Storage TA
204=================
205These are stored in secure storage. The meta data is stored in a database of all
206installed TAs and the actual binary is stored encrypted and integrity protected
207as a separate file in the untrusted REE filesystem (flash). Before these TAs can
208be loaded they have to be installed first, this is something that can be done
209during initial deployment or at a later stage.
210
211For test purposes the test program xtest can install a TA into secure storage
212with the command:
213
214.. code-block:: bash
215
216 $ xtest --install-ta
217
Jens Wiklander6b67a662019-11-25 10:58:46 +0100218TAs stored in secure storage are kept in a TA database. The TA database
219consists of a single file with the name ``dirf.db`` which is stored either
220in the REE filesystem based secure storage or in RPMB. The file is
221encrypted and integrity protected as any other object in secure storage.
222The TAs themselves are not stored in ``dirf.db``, they are instead stored
223in the REE filesystem encrypted and integrity protected. One reason for
224this is that TAs can potentially be quite large, several megabytes, while
225secure storage is designed to hold only small objects counted in kilobytes.
226
227``dirf.db`` constsist of an array of ``struct tadb_entry``, defined as:
228
229.. code-block:: C
230
231 /*
232 * struct tee_tadb_property
233 * @uuid: UUID of Trusted Application (TA) or Security Domain (SD)
234 * @version: Version of TA or SD
235 * @custom_size:Size of customized properties, prepended to the encrypted
236 * TA binary
237 * @bin_size: Size of the binary TA
238 */
239 struct tee_tadb_property {
240 TEE_UUID uuid;
241 uint32_t version;
242 uint32_t custom_size;
243 uint32_t bin_size;
244 };
245
246 #define TADB_IV_SIZE TEE_AES_BLOCK_SIZE
247 #define TADB_TAG_SIZE TEE_AES_BLOCK_SIZE
248 #define TADB_KEY_SIZE TEE_AES_MAX_KEY_SIZE
249
250 /*
251 * struct tadb_entry - TA database entry
252 * @prop: properties of TA
253 * @file_number: encrypted TA is stored in <file_number>.ta
254 * @iv: Initialization vector of the authentication crypto
255 * @tag: Tag used to validate the authentication encrypted TA
256 * @key: Key used to decrypt the TA
257 */
258 struct tadb_entry {
259 struct tee_tadb_property prop;
260 uint32_t file_number;
261 uint8_t iv[TADB_IV_SIZE];
262 uint8_t tag[TADB_TAG_SIZE];
263 uint8_t key[TADB_KEY_SIZE];
264 };
265
266Entries where the ``UUID`` consists of zeros only are not valid and are
267ignored. The ``file_number`` field represents that name of the file stored
268in the REE filesystem. The filename is made from the decimal string
269representation of ``file_number`` with ``.ta`` appended, or if it was to be
270printed: ``printf("%u.ta", file_number)``.
271
272The TA is decrypted using the authentication encryption algorithm AES-GCM
273initialized with the ``iv`` and ``key`` fields, the ``tag`` field is used
274when finalizing the decryption
275
276A TA is looked up in the TA database by opening ``dirf.db`` and scanning
277through the elements which are of type ``struct tadb_entry`` until a
278matching UUID is found.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200279
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100280Loading and preparing TA for execution
281**************************************
282
283User mode TAs are loaded into final memory in the same way using the user
284mode ELF loader ``ldelf``. The different TA locations has a common
285interface towards ``ldelf`` which makes the user mode operations identical
286regarless of how the TA is stored.
287
288The TA is loaded into secure memory in :ref:`prepare_ta`.
289
290.. _prepare_ta:
291
292.. figure:: ../images/trusted_applications/prepare_ta.png
293 :figclass: align-center
294
295 Preparing TA for execution
296
297After ``ldelf`` has returned with a TA prepared for execution it still
298remains in memory to serve the TA if dlopen() and friends are used.
299``ldelf`` is also used to dump stack trace and detailed memory mappings if
300a TA is terminated via an abort.
301
302A high level view of the entire flow from the client application in Linux
303user space where a session is opened to a TA is given in
304:ref:`open_session`.
305
306.. _open_session:
307
308.. figure:: ../images/trusted_applications/open_session.png
309 :figclass: align-center
310
311 Open session to a TA
312
313
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200314.. _ta_properties:
315
316TA Properties
317*************
318This section give a more in depth description of the TA properties (see
319:ref:`build_trusted_applications` also).
320
Etienne Carriere2f459d12019-03-11 11:58:05 +0100321GlobalPlatform Properties
322=========================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200323Standard TA properties must be defined through property flag in macro
324``TA_FLAGS`` in ``user_ta_header_defines.h``
325
Etienne Carriere2f459d12019-03-11 11:58:05 +0100326Single Instance
327---------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200328``"gpd.ta.singleInstance"`` is a boolean property of the TA. This property
329defines if one instance of the TA must be created and will receive all open
330session request, or if a new specific TA instance must be created for each
331incoming open session request. OP-TEE TA flag ``TA_FLAG_SINGLE_INSTANCE`` sets
332to configuration of this property. The boolean property is set to ``true`` if
333``TA_FLAGS`` sets bit ``TA_FLAG_SINGLE_INSTANCE``, otherwise the boolean
334property is set to ``false``.
335
Etienne Carriere2f459d12019-03-11 11:58:05 +0100336Multi-session
337-------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200338``"gpd.ta.multiSession"`` is a boolean property of the TA. This property defines
339if the TA instance can handle several sessions. If disabled, TA instance support
340only one session. In such case, if the TA already has a opened session, any open
341session request will return with a busy error status.
342
343.. note::
344
345 This property is **meaningless** if TA is **NOT** SingleInstance TA.
346
347OP-TEE TA flag ``TA_FLAG_MULTI_SESSION`` sets to configuration of this property.
348The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
349``TA_FLAG_MULTI_SESSION``, otherwise the boolean property is set to ``false``.
350
Etienne Carriere2f459d12019-03-11 11:58:05 +0100351Keep Alive
352----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200353``"gpd.ta.instanceKeepAlive"`` is a boolean property of the TA. This property
354defines if the TA instance created must be destroyed or not when all sessions
355opened towards the TA are closed. If the property is enabled, TA instance, once
356created (at 1st open session request), is never removed unless the TEE itself is
357restarted (boot/reboot).
358
359.. note::
360
361 This property is **meaningless** if TA is **NOT** SingleInstance TA.
362
363OP-TEE TA flag ``TA_FLAG_INSTANCE_KEEP_ALIVE`` sets to configuration of this
364property. The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
365``TA_FLAG_INSTANCE_KEEP_ALIVE``, otherwise the boolean property is set to
366``false``.
367
Etienne Carriere2f459d12019-03-11 11:58:05 +0100368Heap Size
369---------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200370``"gpd.ta.dataSize"`` is a 32bit integer property of the TA. This property
371defines the size in bytes of the TA allocation pool, in which ``TEE_Malloc()``
372and friends allocate memory. The value of the property must be defined by the
373macro ``TA_DATA_SIZE`` in ``user_ta_header_defines.h`` (see
374:ref:`build_ta_properties`).
375
Etienne Carriere2f459d12019-03-11 11:58:05 +0100376Stack Size
377----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200378``"gpd.ta.stackSize"`` is a 32bit integer property of the TA. This property
379defines the size in bytes of the stack used for TA execution. The value of the
380property must be defined by the macro ``TA_STACK_SIZE`` in
381``user_ta_header_defines.h`` (see :ref:`build_ta_properties`).
382
Etienne Carriere2f459d12019-03-11 11:58:05 +0100383Property Extensions
384===================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200385
Etienne Carriere2f459d12019-03-11 11:58:05 +0100386Secure Data Path Flag
387---------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200388``TA_FLAG_SECURE_DATA_PATH`` is a bit flag supported by ``TA_FLAGS``. This
389property flag claims the secure data support from the OP-TEE OS for the TA.
390Refer to the OP-TEE OS for secure data path support. TAs that do not set
391``TA_FLAG_SECURE_DATA_PATH`` in the value of ``TA_FLAGS`` will **not** be able
392to handle memory reference invocation parameters that relate to secure data path
393buffers.
394
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100395.. _ta_property_cache_maintenance:
Etienne Carriere2f459d12019-03-11 11:58:05 +0100396
397Cache maintenance Flag
398----------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200399``TA_FLAG_CACHE_MAINTENANCE`` is a bit flag supported by ``TA_FLAGS``. This
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100400property flag, when enabled, allows Trusted Applciation to use the cache
401maintenance API extension of the Internal Core API described in
402:ref:`extensions_cache_maintenance`. TAs that do not set
403``TA_FLAG_CACHE_MAINTENANCE`` in the value of their ``TA_FLAGS`` will not be
404able to call the cache maintenance API.
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100405
Etienne Carriere2f459d12019-03-11 11:58:05 +0100406Deprecated Property Flags
407-------------------------
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100408Older versions of OP-TEE used to define extended property flags that are
409deprecated and meaningless to current OP-TEE. These are ``TA_FLAG_USER_MODE``,
410``TA_FLAG_EXEC_DDR`` and ``TA_FLAG_REMAP_SUPPORT``.
Etienne Carriere2f459d12019-03-11 11:58:05 +0100411
412.. _ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
413.. _early TA commit: https://github.com/OP-TEE/optee_os/commit/d0c636148b3a