blob: 44be1d29cb461c4b876572de330b3c29068e333c [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
135The field ``image_type`` tells the type of TA, if it's ``SHDR_TA`` (0),
136it's a legacy TA. If it's ``SHDR_BOOTSTRAP_TA`` (1) it's a bootstrap TA.
137
138For bootstrap TAs ``struct shdr`` is followed by a subheader, ``struct
139shdr_bootstrap_ta`` which is defined as:
140
141.. code-block:: c
142
143 /**
144 * struct shdr_bootstrap_ta - bootstrap TA subheader
145 * @uuid: UUID of the TA
146 * @ta_version: Version of the TA
147 */
148 struct shdr_bootstrap_ta {
149 uint8_t uuid[sizeof(TEE_UUID)];
150 uint32_t ta_version;
151 };
152
153The fields ``uuid`` and ``ta_version`` allows extra checks to be performed
154when loading the TA. Currently only the ``uuid`` field is checked.
155
156Last in the TA binary follows the ELF file which normally is stripped
157as additional symbols etc will be ignored when loading the TA.
158
159Legacy TA binary is formatted as:
160
161.. code-block:: none
162
163 hash = H(<struct shdr> || <stripped ELF>)
164 signature = RSA-Sign(hash)
165 legacy_binary = <struct shdr> || <hash> || <signature> || <stripped ELF>
166
167Bootstrap TA binary is formatted as:
168
169.. code-block:: none
170
171 hash = H(<struct shdr> || <struct shdr_bootstrap_ta> || <stripped ELF>)
172 signature = RSA-Sign(<hash>)
173 bootstrap_binary = <struct shdr> || <hash> || <signature> ||
174 <struct shdr_bootstrap_ta> || <stripped ELF>
175
176A REE TA is loaded into shared memory using a series or RPC in
177:ref:`load_ree_ta`. The payload memory is allocated via TEE-supplicant and
178later freed when the TA has been loaded into secure memory in
179:ref:`free_appl_shm`.
180
181.. _load_ree_ta:
182
183.. figure:: ../images/trusted_applications/load_ree_ta.png
184 :figclass: align-center
185
186 Loading a REE TA into nonsecure shared memory
187
188.. _free_appl_shm:
189
190.. figure:: ../images/trusted_applications/free_appl_shm.png
191 :figclass: align-center
192
193 Freeing previously allocated nonsecure shared memory
194
195
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200196.. _secure_storage_ta:
197
198Secure Storage TA
199=================
200These are stored in secure storage. The meta data is stored in a database of all
201installed TAs and the actual binary is stored encrypted and integrity protected
202as a separate file in the untrusted REE filesystem (flash). Before these TAs can
203be loaded they have to be installed first, this is something that can be done
204during initial deployment or at a later stage.
205
206For test purposes the test program xtest can install a TA into secure storage
207with the command:
208
209.. code-block:: bash
210
211 $ xtest --install-ta
212
213
214.. _ta_properties:
215
216TA Properties
217*************
218This section give a more in depth description of the TA properties (see
219:ref:`build_trusted_applications` also).
220
Etienne Carriere2f459d12019-03-11 11:58:05 +0100221GlobalPlatform Properties
222=========================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200223Standard TA properties must be defined through property flag in macro
224``TA_FLAGS`` in ``user_ta_header_defines.h``
225
Etienne Carriere2f459d12019-03-11 11:58:05 +0100226Single Instance
227---------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200228``"gpd.ta.singleInstance"`` is a boolean property of the TA. This property
229defines if one instance of the TA must be created and will receive all open
230session request, or if a new specific TA instance must be created for each
231incoming open session request. OP-TEE TA flag ``TA_FLAG_SINGLE_INSTANCE`` sets
232to configuration of this property. The boolean property is set to ``true`` if
233``TA_FLAGS`` sets bit ``TA_FLAG_SINGLE_INSTANCE``, otherwise the boolean
234property is set to ``false``.
235
Etienne Carriere2f459d12019-03-11 11:58:05 +0100236Multi-session
237-------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200238``"gpd.ta.multiSession"`` is a boolean property of the TA. This property defines
239if the TA instance can handle several sessions. If disabled, TA instance support
240only one session. In such case, if the TA already has a opened session, any open
241session request will return with a busy error status.
242
243.. note::
244
245 This property is **meaningless** if TA is **NOT** SingleInstance TA.
246
247OP-TEE TA flag ``TA_FLAG_MULTI_SESSION`` sets to configuration of this property.
248The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
249``TA_FLAG_MULTI_SESSION``, otherwise the boolean property is set to ``false``.
250
Etienne Carriere2f459d12019-03-11 11:58:05 +0100251Keep Alive
252----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200253``"gpd.ta.instanceKeepAlive"`` is a boolean property of the TA. This property
254defines if the TA instance created must be destroyed or not when all sessions
255opened towards the TA are closed. If the property is enabled, TA instance, once
256created (at 1st open session request), is never removed unless the TEE itself is
257restarted (boot/reboot).
258
259.. note::
260
261 This property is **meaningless** if TA is **NOT** SingleInstance TA.
262
263OP-TEE TA flag ``TA_FLAG_INSTANCE_KEEP_ALIVE`` sets to configuration of this
264property. The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
265``TA_FLAG_INSTANCE_KEEP_ALIVE``, otherwise the boolean property is set to
266``false``.
267
Etienne Carriere2f459d12019-03-11 11:58:05 +0100268Heap Size
269---------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200270``"gpd.ta.dataSize"`` is a 32bit integer property of the TA. This property
271defines the size in bytes of the TA allocation pool, in which ``TEE_Malloc()``
272and friends allocate memory. The value of the property must be defined by the
273macro ``TA_DATA_SIZE`` in ``user_ta_header_defines.h`` (see
274:ref:`build_ta_properties`).
275
Etienne Carriere2f459d12019-03-11 11:58:05 +0100276Stack Size
277----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200278``"gpd.ta.stackSize"`` is a 32bit integer property of the TA. This property
279defines the size in bytes of the stack used for TA execution. The value of the
280property must be defined by the macro ``TA_STACK_SIZE`` in
281``user_ta_header_defines.h`` (see :ref:`build_ta_properties`).
282
Etienne Carriere2f459d12019-03-11 11:58:05 +0100283Property Extensions
284===================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200285
Etienne Carriere2f459d12019-03-11 11:58:05 +0100286Secure Data Path Flag
287---------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200288``TA_FLAG_SECURE_DATA_PATH`` is a bit flag supported by ``TA_FLAGS``. This
289property flag claims the secure data support from the OP-TEE OS for the TA.
290Refer to the OP-TEE OS for secure data path support. TAs that do not set
291``TA_FLAG_SECURE_DATA_PATH`` in the value of ``TA_FLAGS`` will **not** be able
292to handle memory reference invocation parameters that relate to secure data path
293buffers.
294
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100295.. _ta_property_cache_maintenance:
Etienne Carriere2f459d12019-03-11 11:58:05 +0100296
297Cache maintenance Flag
298----------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200299``TA_FLAG_CACHE_MAINTENANCE`` is a bit flag supported by ``TA_FLAGS``. This
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100300property flag, when enabled, allows Trusted Applciation to use the cache
301maintenance API extension of the Internal Core API described in
302:ref:`extensions_cache_maintenance`. TAs that do not set
303``TA_FLAG_CACHE_MAINTENANCE`` in the value of their ``TA_FLAGS`` will not be
304able to call the cache maintenance API.
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100305
Etienne Carriere2f459d12019-03-11 11:58:05 +0100306Deprecated Property Flags
307-------------------------
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100308Older versions of OP-TEE used to define extended property flags that are
309deprecated and meaningless to current OP-TEE. These are ``TA_FLAG_USER_MODE``,
310``TA_FLAG_EXEC_DDR`` and ``TA_FLAG_REMAP_SUPPORT``.
Etienne Carriere2f459d12019-03-11 11:58:05 +0100311
312.. _ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
313.. _early TA commit: https://github.com/OP-TEE/optee_os/commit/d0c636148b3a