blob: e77c924694a6b90ac4a0ae8742e98246ff46c8f9 [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
218
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100219Loading and preparing TA for execution
220**************************************
221
222User mode TAs are loaded into final memory in the same way using the user
223mode ELF loader ``ldelf``. The different TA locations has a common
224interface towards ``ldelf`` which makes the user mode operations identical
225regarless of how the TA is stored.
226
227The TA is loaded into secure memory in :ref:`prepare_ta`.
228
229.. _prepare_ta:
230
231.. figure:: ../images/trusted_applications/prepare_ta.png
232 :figclass: align-center
233
234 Preparing TA for execution
235
236After ``ldelf`` has returned with a TA prepared for execution it still
237remains in memory to serve the TA if dlopen() and friends are used.
238``ldelf`` is also used to dump stack trace and detailed memory mappings if
239a TA is terminated via an abort.
240
241A high level view of the entire flow from the client application in Linux
242user space where a session is opened to a TA is given in
243:ref:`open_session`.
244
245.. _open_session:
246
247.. figure:: ../images/trusted_applications/open_session.png
248 :figclass: align-center
249
250 Open session to a TA
251
252
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200253.. _ta_properties:
254
255TA Properties
256*************
257This section give a more in depth description of the TA properties (see
258:ref:`build_trusted_applications` also).
259
Etienne Carriere2f459d12019-03-11 11:58:05 +0100260GlobalPlatform Properties
261=========================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200262Standard TA properties must be defined through property flag in macro
263``TA_FLAGS`` in ``user_ta_header_defines.h``
264
Etienne Carriere2f459d12019-03-11 11:58:05 +0100265Single Instance
266---------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200267``"gpd.ta.singleInstance"`` is a boolean property of the TA. This property
268defines if one instance of the TA must be created and will receive all open
269session request, or if a new specific TA instance must be created for each
270incoming open session request. OP-TEE TA flag ``TA_FLAG_SINGLE_INSTANCE`` sets
271to configuration of this property. The boolean property is set to ``true`` if
272``TA_FLAGS`` sets bit ``TA_FLAG_SINGLE_INSTANCE``, otherwise the boolean
273property is set to ``false``.
274
Etienne Carriere2f459d12019-03-11 11:58:05 +0100275Multi-session
276-------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200277``"gpd.ta.multiSession"`` is a boolean property of the TA. This property defines
278if the TA instance can handle several sessions. If disabled, TA instance support
279only one session. In such case, if the TA already has a opened session, any open
280session request will return with a busy error status.
281
282.. note::
283
284 This property is **meaningless** if TA is **NOT** SingleInstance TA.
285
286OP-TEE TA flag ``TA_FLAG_MULTI_SESSION`` sets to configuration of this property.
287The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
288``TA_FLAG_MULTI_SESSION``, otherwise the boolean property is set to ``false``.
289
Etienne Carriere2f459d12019-03-11 11:58:05 +0100290Keep Alive
291----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200292``"gpd.ta.instanceKeepAlive"`` is a boolean property of the TA. This property
293defines if the TA instance created must be destroyed or not when all sessions
294opened towards the TA are closed. If the property is enabled, TA instance, once
295created (at 1st open session request), is never removed unless the TEE itself is
296restarted (boot/reboot).
297
298.. note::
299
300 This property is **meaningless** if TA is **NOT** SingleInstance TA.
301
302OP-TEE TA flag ``TA_FLAG_INSTANCE_KEEP_ALIVE`` sets to configuration of this
303property. The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
304``TA_FLAG_INSTANCE_KEEP_ALIVE``, otherwise the boolean property is set to
305``false``.
306
Etienne Carriere2f459d12019-03-11 11:58:05 +0100307Heap Size
308---------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200309``"gpd.ta.dataSize"`` is a 32bit integer property of the TA. This property
310defines the size in bytes of the TA allocation pool, in which ``TEE_Malloc()``
311and friends allocate memory. The value of the property must be defined by the
312macro ``TA_DATA_SIZE`` in ``user_ta_header_defines.h`` (see
313:ref:`build_ta_properties`).
314
Etienne Carriere2f459d12019-03-11 11:58:05 +0100315Stack Size
316----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200317``"gpd.ta.stackSize"`` is a 32bit integer property of the TA. This property
318defines the size in bytes of the stack used for TA execution. The value of the
319property must be defined by the macro ``TA_STACK_SIZE`` in
320``user_ta_header_defines.h`` (see :ref:`build_ta_properties`).
321
Etienne Carriere2f459d12019-03-11 11:58:05 +0100322Property Extensions
323===================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200324
Etienne Carriere2f459d12019-03-11 11:58:05 +0100325Secure Data Path Flag
326---------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200327``TA_FLAG_SECURE_DATA_PATH`` is a bit flag supported by ``TA_FLAGS``. This
328property flag claims the secure data support from the OP-TEE OS for the TA.
329Refer to the OP-TEE OS for secure data path support. TAs that do not set
330``TA_FLAG_SECURE_DATA_PATH`` in the value of ``TA_FLAGS`` will **not** be able
331to handle memory reference invocation parameters that relate to secure data path
332buffers.
333
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100334.. _ta_property_cache_maintenance:
Etienne Carriere2f459d12019-03-11 11:58:05 +0100335
336Cache maintenance Flag
337----------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200338``TA_FLAG_CACHE_MAINTENANCE`` is a bit flag supported by ``TA_FLAGS``. This
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100339property flag, when enabled, allows Trusted Applciation to use the cache
340maintenance API extension of the Internal Core API described in
341:ref:`extensions_cache_maintenance`. TAs that do not set
342``TA_FLAG_CACHE_MAINTENANCE`` in the value of their ``TA_FLAGS`` will not be
343able to call the cache maintenance API.
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100344
Etienne Carriere2f459d12019-03-11 11:58:05 +0100345Deprecated Property Flags
346-------------------------
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100347Older versions of OP-TEE used to define extended property flags that are
348deprecated and meaningless to current OP-TEE. These are ``TA_FLAG_USER_MODE``,
349``TA_FLAG_EXEC_DDR`` and ``TA_FLAG_REMAP_SUPPORT``.
Etienne Carriere2f459d12019-03-11 11:58:05 +0100350
351.. _ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
352.. _early TA commit: https://github.com/OP-TEE/optee_os/commit/d0c636148b3a