aboutsummaryrefslogtreecommitdiff
path: root/core/arch/arm/pta/se_api_self_tests.c
blob: b292fe14b9a6e09323efa14d636d0ea4523b729a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 */

#include <compiler.h>
#include <kernel/pseudo_ta.h>
#include <tee_api_types.h>
#include <tee_api_defines.h>
#include <trace.h>

#include <tee/se/manager.h>
#include <tee/se/reader.h>
#include <tee/se/session.h>
#include <tee/se/iso7816.h>
#include <tee/se/aid.h>
#include <tee/se/apdu.h>
#include <tee/se/channel.h>
#include <tee/se/util.h>

#include <stdlib.h>
#include <string.h>

#include "aid_priv.h"
#include "apdu_priv.h"
#include "reader_priv.h"


#define TA_NAME		"se_api_self_tests.ta"

#define MAX_READERS	10

#define CMD_SELF_TESTS	0

#define SE_API_SELF_TEST_UUID \
		{ 0xAEB79790, 0x6F03, 0x11E4,  \
			{ 0x98, 0x03, 0x08, 0x00, 0x20, 0x0C, 0x9A, 0x66 } }

#define ASSERT(expr) \
	do { \
		if (!(expr)) { \
			EMSG("assertion '%s' failed at %s:%d (func '%s')", \
				#expr, __FILE__, __LINE__, __func__); \
			return TEE_ERROR_GENERIC; \
		} \
	} while (0)

#define CHECK(ret) \
	do { \
		if (ret != TEE_SUCCESS) \
			return ret; \
	} while (0)

/*
 * Trusted Application Entry Points
 */

static TEE_Result test_reader(struct tee_se_reader_proxy **handle)
{
	TEE_Result ret;
	uint8_t cmd[] = { ISO7816_CLA, MANAGE_CHANNEL_CMD,
		OPEN_CHANNEL, OPEN_NEXT_AVAILABLE };
	uint8_t resp[3];
	size_t resp_size = sizeof(resp);
	const int expected_channel_id = 1;

	DMSG("entry");
	/* transmit should fail since no one attached to the reader */
	ret = tee_se_reader_transmit(handle[0], cmd, sizeof(cmd),
			resp, &resp_size);
	ASSERT(ret == TEE_ERROR_BAD_STATE);

	ret = tee_se_reader_attach(handle[0]);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_reader_attach(handle[0]);
	ASSERT(ret == TEE_SUCCESS);

	/* referenced by 2 owners */
	ASSERT(2 == tee_se_reader_get_refcnt(handle[0]));

	ret = tee_se_reader_transmit(handle[0], cmd, sizeof(cmd),
				resp, &resp_size);
	ASSERT(ret == TEE_SUCCESS);
	ASSERT(resp[0] == expected_channel_id &&
		resp[1] == CMD_OK_SW1 && resp[2] == CMD_OK_SW2);

	tee_se_reader_detach(handle[0]);

	ASSERT(1 == tee_se_reader_get_refcnt(handle[0]));

	tee_se_reader_detach(handle[0]);
	DMSG("exit");

	return TEE_SUCCESS;
}

static TEE_Result test_aid(struct tee_se_reader_proxy **proxies)
{
	struct tee_se_session *s = NULL;
	struct tee_se_channel *b = NULL, *l = NULL;
	struct tee_se_aid *aid = NULL;
	TEE_Result ret;

	DMSG("entry");
	ret = tee_se_aid_create("D0000CAFE00001", &aid);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_reader_open_session(proxies[0], &s);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_session_open_basic_channel(s, aid, &b);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_session_open_logical_channel(s, aid, &l);
	ASSERT(ret == TEE_SUCCESS);

	ASSERT(tee_se_aid_get_refcnt(aid) == 3);

	tee_se_session_close_channel(s, b);
	tee_se_session_close_channel(s, l);

	ASSERT(tee_se_aid_get_refcnt(aid) == 1);

	tee_se_session_close(s);
	tee_se_aid_release(aid);
	DMSG("exit");

	return TEE_SUCCESS;
}

static TEE_Result test_session(struct tee_se_reader_proxy **proxies)
{
	struct tee_se_channel *c1 = NULL, *c2 = NULL;
	struct tee_se_session *s1 = NULL, *s2 = NULL;
	TEE_Result ret;

	DMSG("entry");
	ret = tee_se_reader_open_session(proxies[0], &s1);
	ASSERT(ret == TEE_SUCCESS);

	/* should success, multiple sessions open by different user */
	ret = tee_se_reader_open_session(proxies[0], &s2);
	ASSERT(ret == TEE_SUCCESS);

	/* open basic channel on s1 (should success) */
	ret = tee_se_session_open_basic_channel(s1, NULL, &c1);
	ASSERT(ret == TEE_SUCCESS);

	/* open basic channel on s2
	 * (should fail, basic channel is locked by s1)
	 */
	ret = tee_se_session_open_basic_channel(s2, NULL, &c2);
	ASSERT(ret == TEE_ERROR_NOT_SUPPORTED);
	ASSERT(c2 == NULL);

	/* close basic channel on s1 */
	tee_se_session_close_channel(s1, c1);
	c1 = NULL;

	/* open basic channel on s2 (this time should success) */
	ret = tee_se_session_open_basic_channel(s1, NULL, &c2);
	ASSERT(ret == TEE_SUCCESS);

	/* close basic channel on s2 */
	tee_se_session_close_channel(s2, c2);
	c2 = NULL;

	/* open logical channel on s1 and s2 (both should success) */
	ret = tee_se_session_open_logical_channel(s1, NULL, &c1);
	ASSERT(ret == TEE_SUCCESS);
	ret = tee_se_session_open_logical_channel(s2, NULL, &c2);
	ASSERT(ret == TEE_SUCCESS);

	/* clean up */
	tee_se_session_close_channel(s1, c1);
	tee_se_session_close_channel(s2, c2);

	tee_se_session_close(s1);
	tee_se_session_close(s2);
	DMSG("exit");

	return TEE_SUCCESS;
}

static TEE_Result test_select_resp(struct tee_se_reader_proxy **proxies)
{
	struct tee_se_aid *aid = NULL;
	struct tee_se_session *s = NULL;
	struct tee_se_channel *c = NULL;
	struct resp_apdu *resp;
	TEE_Result ret;

	DMSG("entry");
	ret = tee_se_aid_create("D0000CAFE00001", &aid);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_reader_open_session(proxies[0], &s);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_session_open_logical_channel(s, aid, &c);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_channel_get_select_response(c, &resp);
	ASSERT(ret == TEE_SUCCESS);

	ASSERT((resp_apdu_get_sw1(resp) == CMD_OK_SW1) &&
			(resp_apdu_get_sw2(resp) == CMD_OK_SW2));

	/*
	 * the ownership of resp apdu should be the channel
	 * and it should be the only owner
	 */
	ASSERT(apdu_get_refcnt(to_apdu_base(resp)) == 1);

	/* increase the reference counter of resp apdu */
	apdu_acquire(to_apdu_base(resp));

	/* clean up */
	tee_se_session_close_channel(s, c);

	/* channel should release resp apdu when closed */
	ASSERT(apdu_get_refcnt(to_apdu_base(resp)) == 1);
	apdu_release(to_apdu_base(resp));

	tee_se_session_close(s);
	tee_se_aid_release(aid);
	DMSG("exit");

	return TEE_SUCCESS;
}

/*
 * The JAVA Card Simulator (jcardsim.jar) built-in applet(s):
 *
 * AID					|Type
 * -------------------------------------+----------------------
 * D0000CAFE00001			| MultiSelectable
 * (default selected on basic channel)	|
 * -------------------------------------+----------------------
 * D0000CAFE00002			| Non-MultiSelectable
 * -------------------------------------+----------------------
 *
 */
static TEE_Result test_logical_channel(struct tee_se_reader_proxy **proxies)
{
	struct tee_se_channel *channel[MAX_LOGICAL_CHANNEL] = { NULL };
	struct tee_se_aid *aid = NULL;
	struct tee_se_session *s = NULL;
	TEE_Result ret;
	int i;

	DMSG("entry");
	ret = tee_se_reader_open_session(proxies[0], &s);
	ASSERT(ret == TEE_SUCCESS);

	/*
	 * test open logical channels based on AID selected on basic channel
	 * (D0000CAFE00001 is default selected on basic channel,
	 * this call should success since D0000CAFE00001 is MultiSelectable,
	 * upon open, each logical channel should select D0000CAFE00001)
	 */
	for (i = 1; i < MAX_LOGICAL_CHANNEL; i ++) {
		ret = tee_se_session_open_logical_channel(s, NULL, &channel[i]);
		ASSERT(ret == TEE_SUCCESS);
	}

	/*
	 * should fail on next open
	 * (exceeds maximum logical channel number)
	 */
	ret = tee_se_session_open_logical_channel(s, NULL, &channel[0]);
	ASSERT(ret == TEE_ERROR_NOT_SUPPORTED);

	/* close 3 channels */
	for (i = 1; i < 4; i++) {
		tee_se_session_close_channel(s, channel[i]);
		channel[i] = NULL;
	}

	/* re-open 3 channels (should success) */
	for (i = 1; i < 4; i++) {
		ret = tee_se_session_open_logical_channel(s, NULL, &channel[i]);
		ASSERT(ret == TEE_SUCCESS);
	}

	/* logical channel 1 select D0000CAFE00002 (should success) */
	tee_se_aid_create("D0000CAFE00002", &aid);
	ret = tee_se_channel_select(channel[1], aid);
	ASSERT(ret == TEE_SUCCESS);

	/* logical channel 2 select D0000CAFE00002
	 * (should fail since D0000CAFE00002 is not MultiSelectable)
	 */
	ret = tee_se_channel_select(channel[2], aid);
	ASSERT(ret == TEE_ERROR_NOT_SUPPORTED);

	/* clean up */
	for (i = 1; i < MAX_LOGICAL_CHANNEL; i++)
		tee_se_session_close_channel(s, channel[i]);
	tee_se_session_close(s);
	tee_se_aid_release(aid);
	DMSG("exit");

	return TEE_SUCCESS;
}

static TEE_Result verify_result(struct resp_apdu *apdu, const char *data)
{
	size_t str_length = strlen(data);
	size_t byte_length = strlen(data) / 2;
	uint8_t *resp_data = resp_apdu_get_data(apdu);
	size_t resp_len = resp_apdu_get_data_len(apdu);
	uint8_t bytes[byte_length];
	size_t i = 0;

	ASSERT(resp_len == byte_length);

	hex_decode(data, str_length, bytes);
	while (i < resp_len) {
		ASSERT(bytes[i] == resp_data[i]);
		i++;
	}
	return TEE_SUCCESS;
}

static TEE_Result test_transmit(struct tee_se_reader_proxy **proxies)
{
	struct tee_se_channel *c1 = NULL, *c2 = NULL;
	struct tee_se_session *s1 = NULL, *s2 = NULL;
	struct tee_se_aid *full_aid = NULL, *partial_aid = NULL;
	struct cmd_apdu *cmd;
	struct resp_apdu *resp;
	size_t tx_buf_len = 0, rx_buf_len = 7;
	TEE_Result ret;

	DMSG("entry");
	ret = tee_se_aid_create("D0000CAFE00001", &full_aid);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_aid_create("D0000CAFE0000", &partial_aid);
	ASSERT(ret == TEE_SUCCESS);

	cmd = alloc_cmd_apdu(ISO7816_CLA, 0xFF, 0x0, 0x0,
			tx_buf_len, rx_buf_len, NULL);
	ASSERT(cmd);
	resp = alloc_resp_apdu(rx_buf_len);
	ASSERT(resp);

	ret = tee_se_reader_open_session(proxies[0], &s1);
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_reader_open_session(proxies[0], &s2);
	ASSERT(ret == TEE_SUCCESS);

	/* open logical channel on s1 (given full aid) */
	ret = tee_se_session_open_logical_channel(s1, full_aid, &c1);
	ASSERT(ret == TEE_SUCCESS);

	/* should route to D0000CAFE00001 */
	ret = tee_se_channel_transmit(c1, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);

	/* select next should fail (full aid given) */
	ret = tee_se_channel_select_next(c1);
	ASSERT(ret == TEE_ERROR_ITEM_NOT_FOUND);

	/* open logical channel on s2 (given partial aid) */
	ret = tee_se_session_open_logical_channel(s2, partial_aid, &c2);
	ASSERT(ret == TEE_SUCCESS);

	/* should route to D0000CAFE00001 */
	ret = tee_se_channel_transmit(c2, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);
	ret = verify_result(resp, "D0000CAFE00001");
	ASSERT(ret == TEE_SUCCESS);

	/* select next should success (select D0000CAFE00002) */
	ret = tee_se_channel_select_next(c2);
	ASSERT(ret == TEE_SUCCESS);

	/* should route to D0000CAFE00002 */
	ret = tee_se_channel_transmit(c2, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);
	ret = verify_result(resp, "D0000CAFE00002");
	ASSERT(ret == TEE_SUCCESS);

	/* select next should success (select D0000CAFE00001) */
	ret = tee_se_channel_select_next(c2);
	ASSERT(ret == TEE_SUCCESS);

	/* should route to D0000CAFE00001 */
	ret = tee_se_channel_transmit(c2, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);
	ret = verify_result(resp, "D0000CAFE00001");
	ASSERT(ret == TEE_SUCCESS);

	/*
	 * test route to the same applet in a row from different channel
	 * (both should success)
	 */
	ret = tee_se_channel_transmit(c1, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);
	ret = verify_result(resp, "D0000CAFE00001");
	ASSERT(ret == TEE_SUCCESS);

	ret = tee_se_channel_transmit(c2, cmd, resp);
	ASSERT(ret == TEE_SUCCESS);
	ret = verify_result(resp, "D0000CAFE00001");
	ASSERT(ret == TEE_SUCCESS);

	/* clean up */
	tee_se_session_close_channel(s1, c1);
	tee_se_session_close_channel(s2, c2);

	tee_se_session_close(s1);
	tee_se_session_close(s2);

	tee_se_aid_release(full_aid);
	tee_se_aid_release(partial_aid);
	DMSG("exit");

	return TEE_SUCCESS;
}

static TEE_Result se_api_self_tests(uint32_t nParamTypes __unused,
				    TEE_Param pParams[TEE_NUM_PARAMS] __unused)
{
	size_t size = MAX_READERS;
	TEE_Result ret;
	struct tee_se_reader_proxy **proxies =
		malloc(sizeof(void *) * MAX_READERS);

	tee_se_manager_get_readers(proxies, &size);

	ret = test_aid(proxies);
	CHECK(ret);

	ret = test_select_resp(proxies);
	CHECK(ret);

	ret = test_session(proxies);
	CHECK(ret);

	ret = test_logical_channel(proxies);
	CHECK(ret);

	ret = test_transmit(proxies);
	CHECK(ret);

	ret = test_reader(proxies);
	CHECK(ret);

	free(proxies);

	return TEE_SUCCESS;
}

static TEE_Result invoke_command(void *pSessionContext __unused,
		uint32_t nCommandID, uint32_t nParamTypes,
		TEE_Param pParams[TEE_NUM_PARAMS])
{
	DMSG("command entry point for static ta \"%s\"", TA_NAME);

	switch (nCommandID) {
	case CMD_SELF_TESTS:
		return se_api_self_tests(nParamTypes, pParams);
	default:
		break;
	}
	return TEE_ERROR_BAD_PARAMETERS;
}

pseudo_ta_register(.uuid = SE_API_SELF_TEST_UUID, .name = TA_NAME,
		   .flags = PTA_DEFAULT_FLAGS,
		   .invoke_command_entry_point = invoke_command);