blob: 51542abfcc8c2ec150c46e6d72bd36a500fbc4db [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=================
Sumit Gargde0c0c72020-04-17 13:15:30 +053074They consist of a ELF_ file, signed and optionally encrypted, named from the
75UUID of the TA and the suffix ``.ta``. They are built separately from the
76OP-TEE core boot-time blob, although when they are built they use the same
77build system, and are signed with the key from the build of the original OP-TEE
78core blob.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020079
Sumit Gargde0c0c72020-04-17 13:15:30 +053080Because the TAs are signed and optionally encrypted with
81``scripts/sign_encrypt.py``, they are able to be stored in the untrusted REE
Joakim Bech8e5c5b32018-10-25 08:18:32 +020082filesystem, and ``tee-supplicant`` will take care of passing them to be checked
Sumit Gargde0c0c72020-04-17 13:15:30 +053083and loaded by the Secure World OP-TEE core.
Joakim Bech8e5c5b32018-10-25 08:18:32 +020084
Sumit Gargde0c0c72020-04-17 13:15:30 +053085REE-FS TA rollback protection
86-----------------------------
87OP-TEE core maintains a ``ta_ver.db`` file in secure storage to check for
88version of REE TAs as they are loaded from REE-FS in order to prevent against
89any TA version downgrades. TA version can be configured via TA build option:
90``CFG_TA_VERSION=<unsigned integer>``.
Jens Wiklander6054d682019-11-19 11:31:14 +010091
Sumit Gargde0c0c72020-04-17 13:15:30 +053092Note: Here rollback protection is effective only when ``CFG_RPMB_FS=y``.
93
94REE-FS TA formats
95-----------------
96REE filesystem TAs come in three formats:
97
98 1. Legacy TAs signed, not encrypted, cannot be created anymore by the build
99 scripts since version 3.7.0.
100
101 2. Bootstrap TAs, signed with the key from the build of the original OP-TEE
102 core blob, not encrypted.
103
104 3. Encrypted TAs, sign-then-encrypt-then-MAC, encrypted with ``TA_ENC_KEY``
105 when ``CFG_ENCRYPT_TA=y``. During OP-TEE runtime, the symmetric key used
106 to decrypt TA has to be provided in a platform specific manner via
107 overriding API:
108
109 .. code-block:: c
110
111 TEE_Result tee_otp_get_ta_enc_key(uint32_t key_type, uint8_t *buffer,
112 size_t len);
113
114REE-FS TA header structure
115--------------------------
Jens Wiklander6054d682019-11-19 11:31:14 +0100116All REE filesystems TAs has common header, ``struct shdr``, defined as:
117
118.. code-block:: c
119
120 enum shdr_img_type {
121 SHDR_TA = 0,
122 SHDR_BOOTSTRAP_TA = 1,
Sumit Gargde0c0c72020-04-17 13:15:30 +0530123 SHDR_ENCRYPTED_TA = 2,
Jens Wiklander6054d682019-11-19 11:31:14 +0100124 };
125
126 #define SHDR_MAGIC 0x4f545348
127
128 /**
129 * struct shdr - signed header
130 * @magic: magic number must match SHDR_MAGIC
131 * @img_type: image type, values defined by enum shdr_img_type
132 * @img_size: image size in bytes
133 * @algo: algorithm, defined by public key algorithms TEE_ALG_*
134 * from TEE Internal API specification
135 * @hash_size: size of the signed hash
136 * @sig_size: size of the signature
137 * @hash: hash of an image
138 * @sig: signature of @hash
139 */
140 struct shdr {
141 uint32_t magic;
142 uint32_t img_type;
143 uint32_t img_size;
144 uint32_t algo;
145 uint16_t hash_size;
146 uint16_t sig_size;
147 /*
148 * Commented out element used to visualize the layout dynamic part
149 * of the struct.
150 *
151 * hash is accessed through the macro SHDR_GET_HASH and
152 * signature is accessed through the macro SHDR_GET_SIG
153 *
154 * uint8_t hash[hash_size];
155 * uint8_t sig[sig_size];
156 */
157 };
158
159 #define SHDR_GET_SIZE(x) (sizeof(struct shdr) + (x)->hash_size + \
160 (x)->sig_size)
161 #define SHDR_GET_HASH(x) (uint8_t *)(((struct shdr *)(x)) + 1)
162 #define SHDR_GET_SIG(x) (SHDR_GET_HASH(x) + (x)->hash_size)
163
164
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100165The field ``img_type`` tells the type of TA, if it's ``SHDR_TA`` (0),
Jens Wiklander6054d682019-11-19 11:31:14 +0100166it's a legacy TA. If it's ``SHDR_BOOTSTRAP_TA`` (1) it's a bootstrap TA.
167
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100168The field ``algo`` tells the algorithm used. The script used to sign TAs
169currently uses ``TEE_ALG_RSASSA_PKCS1_V1_5_SHA256`` (0x70004830). This
170means RSA with PKCS#1v1.5 padding and SHA-256 hash function. OP-TEE accepts
171any of the ``TEE_ALG_RSASSA_PKCS1_*`` algorithms.
172
Jens Wiklander6054d682019-11-19 11:31:14 +0100173For bootstrap TAs ``struct shdr`` is followed by a subheader, ``struct
174shdr_bootstrap_ta`` which is defined as:
175
176.. code-block:: c
177
178 /**
179 * struct shdr_bootstrap_ta - bootstrap TA subheader
180 * @uuid: UUID of the TA
181 * @ta_version: Version of the TA
182 */
183 struct shdr_bootstrap_ta {
184 uint8_t uuid[sizeof(TEE_UUID)];
185 uint32_t ta_version;
186 };
187
188The fields ``uuid`` and ``ta_version`` allows extra checks to be performed
189when loading the TA. Currently only the ``uuid`` field is checked.
190
Sumit Gargde0c0c72020-04-17 13:15:30 +0530191For encrypted TAs ``struct shdr`` is followed by a subheader, ``struct
192shdr_bootstrap_ta`` which is followed by another subheader, ``struct
193shdr_encrypted_ta`` defined as:
194
195.. code-block:: c
196
197 /**
198 * struct shdr_encrypted_ta - encrypted TA header
199 * @enc_algo: authenticated encyption algorithm, defined by symmetric key
200 * algorithms TEE_ALG_* from TEE Internal API
201 * specification
202 * @flags: authenticated encyption flags
203 * @iv_size: size of the initialization vector
204 * @tag_size: size of the authentication tag
205 * @iv: initialization vector
206 * @tag: authentication tag
207 */
208 struct shdr_encrypted_ta {
209 uint32_t enc_algo;
210 uint32_t flags;
211 uint16_t iv_size;
212 uint16_t tag_size;
213 /*
214 * Commented out element used to visualize the layout dynamic part
215 * of the struct.
216 *
217 * iv is accessed through the macro SHDR_ENC_GET_IV and
218 * tag is accessed through the macro SHDR_ENC_GET_TAG
219 *
220 * uint8_t iv[iv_size];
221 * uint8_t tag[tag_size];
222 */
223 };
224
225The field ``enc_algo`` tells the algorithm used. The script used to encrypt
226TAs currently uses ``TEE_ALG_AES_GCM`` (0x40000810). OP-TEE core also accepts
227``TEE_ALG_AES_CCM`` algorithm.
228
229The field ``flags`` supports a single flag to tell encryption key type which
230is defined as:
231
232.. code-block:: c
233
234 #define SHDR_ENC_KEY_TYPE_MASK 0x1
235
236 enum shdr_enc_key_type {
237 SHDR_ENC_KEY_DEV_SPECIFIC = 0,
238 SHDR_ENC_KEY_CLASS_WIDE = 1,
239 };
240
241REE-FS TA binary formats
242------------------------
243TA binary follows the ELF file which normally is stripped as additional
244symbols etc will be ignored when loading the TA.
Jens Wiklander6054d682019-11-19 11:31:14 +0100245
246Legacy TA binary is formatted as:
247
248.. code-block:: none
249
250 hash = H(<struct shdr> || <stripped ELF>)
251 signature = RSA-Sign(hash)
252 legacy_binary = <struct shdr> || <hash> || <signature> || <stripped ELF>
253
254Bootstrap TA binary is formatted as:
255
256.. code-block:: none
257
258 hash = H(<struct shdr> || <struct shdr_bootstrap_ta> || <stripped ELF>)
259 signature = RSA-Sign(<hash>)
260 bootstrap_binary = <struct shdr> || <hash> || <signature> ||
261 <struct shdr_bootstrap_ta> || <stripped ELF>
262
Sumit Gargde0c0c72020-04-17 13:15:30 +0530263Encrypted TA binary is formatted as:
264
265.. code-block:: none
266
267 nonce = <unique random value>
268 ciphertext, tag = AES_GCM(<stripped ELF>)
269 hash = H(<struct shdr> || <struct shdr_bootstrap_ta> ||
270 <struct shdr_encrypted_ta> || <nonce> || <tag> || <stripped ELF>)
271 signature = RSA-Sign(<hash>)
272 encrypted_binary = <struct shdr> || <hash> || <signature> ||
273 <struct shdr_bootstrap_ta> ||
274 <struct shdr_encrypted_ta> || <nonce> || <tag> ||
275 <ciphertext>
276
277Loading REE-FS TA
278-----------------
Jens Wiklander6054d682019-11-19 11:31:14 +0100279A REE TA is loaded into shared memory using a series or RPC in
280:ref:`load_ree_ta`. The payload memory is allocated via TEE-supplicant and
281later freed when the TA has been loaded into secure memory in
282:ref:`free_appl_shm`.
283
284.. _load_ree_ta:
285
286.. figure:: ../images/trusted_applications/load_ree_ta.png
287 :figclass: align-center
288
289 Loading a REE TA into nonsecure shared memory
290
291.. _free_appl_shm:
292
293.. figure:: ../images/trusted_applications/free_appl_shm.png
294 :figclass: align-center
295
296 Freeing previously allocated nonsecure shared memory
297
298
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200299.. _secure_storage_ta:
300
301Secure Storage TA
302=================
303These are stored in secure storage. The meta data is stored in a database of all
304installed TAs and the actual binary is stored encrypted and integrity protected
305as a separate file in the untrusted REE filesystem (flash). Before these TAs can
306be loaded they have to be installed first, this is something that can be done
307during initial deployment or at a later stage.
308
309For test purposes the test program xtest can install a TA into secure storage
310with the command:
311
312.. code-block:: bash
313
314 $ xtest --install-ta
315
Jens Wiklander6b67a662019-11-25 10:58:46 +0100316TAs stored in secure storage are kept in a TA database. The TA database
317consists of a single file with the name ``dirf.db`` which is stored either
318in the REE filesystem based secure storage or in RPMB. The file is
319encrypted and integrity protected as any other object in secure storage.
320The TAs themselves are not stored in ``dirf.db``, they are instead stored
321in the REE filesystem encrypted and integrity protected. One reason for
322this is that TAs can potentially be quite large, several megabytes, while
323secure storage is designed to hold only small objects counted in kilobytes.
324
325``dirf.db`` constsist of an array of ``struct tadb_entry``, defined as:
326
327.. code-block:: C
328
329 /*
330 * struct tee_tadb_property
331 * @uuid: UUID of Trusted Application (TA) or Security Domain (SD)
332 * @version: Version of TA or SD
333 * @custom_size:Size of customized properties, prepended to the encrypted
334 * TA binary
335 * @bin_size: Size of the binary TA
336 */
337 struct tee_tadb_property {
338 TEE_UUID uuid;
339 uint32_t version;
340 uint32_t custom_size;
341 uint32_t bin_size;
342 };
343
344 #define TADB_IV_SIZE TEE_AES_BLOCK_SIZE
345 #define TADB_TAG_SIZE TEE_AES_BLOCK_SIZE
346 #define TADB_KEY_SIZE TEE_AES_MAX_KEY_SIZE
347
348 /*
349 * struct tadb_entry - TA database entry
350 * @prop: properties of TA
351 * @file_number: encrypted TA is stored in <file_number>.ta
352 * @iv: Initialization vector of the authentication crypto
353 * @tag: Tag used to validate the authentication encrypted TA
354 * @key: Key used to decrypt the TA
355 */
356 struct tadb_entry {
357 struct tee_tadb_property prop;
358 uint32_t file_number;
359 uint8_t iv[TADB_IV_SIZE];
360 uint8_t tag[TADB_TAG_SIZE];
361 uint8_t key[TADB_KEY_SIZE];
362 };
363
364Entries where the ``UUID`` consists of zeros only are not valid and are
365ignored. The ``file_number`` field represents that name of the file stored
366in the REE filesystem. The filename is made from the decimal string
367representation of ``file_number`` with ``.ta`` appended, or if it was to be
368printed: ``printf("%u.ta", file_number)``.
369
370The TA is decrypted using the authentication encryption algorithm AES-GCM
371initialized with the ``iv`` and ``key`` fields, the ``tag`` field is used
372when finalizing the decryption
373
374A TA is looked up in the TA database by opening ``dirf.db`` and scanning
375through the elements which are of type ``struct tadb_entry`` until a
376matching UUID is found.
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200377
Jens Wiklandera6315cd2019-11-19 14:34:30 +0100378Loading and preparing TA for execution
379**************************************
380
381User mode TAs are loaded into final memory in the same way using the user
382mode ELF loader ``ldelf``. The different TA locations has a common
383interface towards ``ldelf`` which makes the user mode operations identical
384regarless of how the TA is stored.
385
386The TA is loaded into secure memory in :ref:`prepare_ta`.
387
388.. _prepare_ta:
389
390.. figure:: ../images/trusted_applications/prepare_ta.png
391 :figclass: align-center
392
393 Preparing TA for execution
394
395After ``ldelf`` has returned with a TA prepared for execution it still
396remains in memory to serve the TA if dlopen() and friends are used.
397``ldelf`` is also used to dump stack trace and detailed memory mappings if
398a TA is terminated via an abort.
399
400A high level view of the entire flow from the client application in Linux
401user space where a session is opened to a TA is given in
402:ref:`open_session`.
403
404.. _open_session:
405
406.. figure:: ../images/trusted_applications/open_session.png
407 :figclass: align-center
408
409 Open session to a TA
410
411
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200412.. _ta_properties:
413
414TA Properties
415*************
416This section give a more in depth description of the TA properties (see
417:ref:`build_trusted_applications` also).
418
Etienne Carriere2f459d12019-03-11 11:58:05 +0100419GlobalPlatform Properties
420=========================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200421Standard TA properties must be defined through property flag in macro
422``TA_FLAGS`` in ``user_ta_header_defines.h``
423
Etienne Carriere2f459d12019-03-11 11:58:05 +0100424Single Instance
425---------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200426``"gpd.ta.singleInstance"`` is a boolean property of the TA. This property
427defines if one instance of the TA must be created and will receive all open
428session request, or if a new specific TA instance must be created for each
429incoming open session request. OP-TEE TA flag ``TA_FLAG_SINGLE_INSTANCE`` sets
430to configuration of this property. The boolean property is set to ``true`` if
431``TA_FLAGS`` sets bit ``TA_FLAG_SINGLE_INSTANCE``, otherwise the boolean
432property is set to ``false``.
433
Etienne Carriere2f459d12019-03-11 11:58:05 +0100434Multi-session
435-------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200436``"gpd.ta.multiSession"`` is a boolean property of the TA. This property defines
437if the TA instance can handle several sessions. If disabled, TA instance support
438only one session. In such case, if the TA already has a opened session, any open
439session request will return with a busy error status.
440
441.. note::
442
443 This property is **meaningless** if TA is **NOT** SingleInstance TA.
444
445OP-TEE TA flag ``TA_FLAG_MULTI_SESSION`` sets to configuration of this property.
446The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
447``TA_FLAG_MULTI_SESSION``, otherwise the boolean property is set to ``false``.
448
Etienne Carriere2f459d12019-03-11 11:58:05 +0100449Keep Alive
450----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200451``"gpd.ta.instanceKeepAlive"`` is a boolean property of the TA. This property
452defines if the TA instance created must be destroyed or not when all sessions
453opened towards the TA are closed. If the property is enabled, TA instance, once
454created (at 1st open session request), is never removed unless the TEE itself is
455restarted (boot/reboot).
456
457.. note::
458
459 This property is **meaningless** if TA is **NOT** SingleInstance TA.
460
461OP-TEE TA flag ``TA_FLAG_INSTANCE_KEEP_ALIVE`` sets to configuration of this
462property. The boolean property is set to ``true`` if ``TA_FLAGS`` sets bit
463``TA_FLAG_INSTANCE_KEEP_ALIVE``, otherwise the boolean property is set to
464``false``.
465
Etienne Carriere2f459d12019-03-11 11:58:05 +0100466Heap Size
467---------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200468``"gpd.ta.dataSize"`` is a 32bit integer property of the TA. This property
469defines the size in bytes of the TA allocation pool, in which ``TEE_Malloc()``
470and friends allocate memory. The value of the property must be defined by the
471macro ``TA_DATA_SIZE`` in ``user_ta_header_defines.h`` (see
472:ref:`build_ta_properties`).
473
Etienne Carriere2f459d12019-03-11 11:58:05 +0100474Stack Size
475----------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200476``"gpd.ta.stackSize"`` is a 32bit integer property of the TA. This property
477defines the size in bytes of the stack used for TA execution. The value of the
478property must be defined by the macro ``TA_STACK_SIZE`` in
479``user_ta_header_defines.h`` (see :ref:`build_ta_properties`).
480
Etienne Carriere2f459d12019-03-11 11:58:05 +0100481Property Extensions
482===================
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200483
Etienne Carriere2f459d12019-03-11 11:58:05 +0100484Secure Data Path Flag
485---------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200486``TA_FLAG_SECURE_DATA_PATH`` is a bit flag supported by ``TA_FLAGS``. This
487property flag claims the secure data support from the OP-TEE OS for the TA.
488Refer to the OP-TEE OS for secure data path support. TAs that do not set
489``TA_FLAG_SECURE_DATA_PATH`` in the value of ``TA_FLAGS`` will **not** be able
490to handle memory reference invocation parameters that relate to secure data path
491buffers.
492
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100493.. _ta_property_cache_maintenance:
Etienne Carriere2f459d12019-03-11 11:58:05 +0100494
495Cache maintenance Flag
496----------------------
Joakim Bech8e5c5b32018-10-25 08:18:32 +0200497``TA_FLAG_CACHE_MAINTENANCE`` is a bit flag supported by ``TA_FLAGS``. This
Etienne Carriere5b99ccc2019-03-11 11:57:30 +0100498property flag, when enabled, allows Trusted Applciation to use the cache
499maintenance API extension of the Internal Core API described in
500:ref:`extensions_cache_maintenance`. TAs that do not set
501``TA_FLAG_CACHE_MAINTENANCE`` in the value of their ``TA_FLAGS`` will not be
502able to call the cache maintenance API.
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100503
Etienne Carriere2f459d12019-03-11 11:58:05 +0100504Deprecated Property Flags
505-------------------------
Etienne Carriere0ab5cc22019-03-11 10:44:13 +0100506Older versions of OP-TEE used to define extended property flags that are
507deprecated and meaningless to current OP-TEE. These are ``TA_FLAG_USER_MODE``,
508``TA_FLAG_EXEC_DDR`` and ``TA_FLAG_REMAP_SUPPORT``.
Etienne Carriere2f459d12019-03-11 11:58:05 +0100509
510.. _ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
511.. _early TA commit: https://github.com/OP-TEE/optee_os/commit/d0c636148b3a