aboutsummaryrefslogtreecommitdiff
path: root/core/tee/se/session.c
blob: 2394ea3d8145d7d453de2279440ed869e8b5c577 (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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
#include <kernel/mutex.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <trace.h>

#include <tee/se/reader.h>
#include <tee/se/session.h>
#include <tee/se/channel.h>
#include <tee/se/iso7816.h>

#include "session_priv.h"
#include "channel_priv.h"

struct tee_se_session *tee_se_session_alloc(
		struct tee_se_reader_proxy *proxy)
{
	struct tee_se_session *s;

	assert(proxy);
	s = malloc(sizeof(struct tee_se_session));
	if (s) {
		TAILQ_INIT(&s->channels);
		s->reader_proxy = proxy;
	}
	return s;
}

void tee_se_session_free(struct tee_se_session *s)
{
	free(s);
}

bool tee_se_session_is_channel_exist(struct tee_se_session *s,
		struct tee_se_channel *c)
{
	struct tee_se_channel *c1;

	TAILQ_FOREACH(c1, &s->channels, link) {
		if (c1 == c)
			return true;
	}
	return false;
}

TEE_Result tee_se_session_get_atr(struct tee_se_session *s,
		uint8_t **atr, size_t *atr_len)
{
	assert(s && atr && atr_len);

	return tee_se_reader_get_atr(s->reader_proxy, atr, atr_len);
}

TEE_Result tee_se_session_open_basic_channel(struct tee_se_session *s,
		struct tee_se_aid *aid, struct tee_se_channel **channel)
{
	struct tee_se_channel *c;
	TEE_Result ret;

	assert(s && channel && !*channel);

	if (tee_se_reader_is_basic_channel_locked(s->reader_proxy)) {
		*channel = NULL;
		return TEE_ERROR_NOT_SUPPORTED;
	}

	c = tee_se_channel_alloc(s, 0);
	if (!c)
		return TEE_ERROR_OUT_OF_MEMORY;

	if (aid) {
		ret = iso7816_select(c, aid);
		if (ret != TEE_SUCCESS)
			goto err_free_channel;
	}

	tee_se_reader_lock_basic_channel(s->reader_proxy);
	*channel = c;
	TAILQ_INSERT_TAIL(&s->channels, c, link);

	return TEE_SUCCESS;

err_free_channel:
	tee_se_channel_free(c);
	return ret;
}

TEE_Result tee_se_session_open_logical_channel(struct tee_se_session *s,
		struct tee_se_aid *aid, struct tee_se_channel **channel)
{
	int channel_id;
	struct tee_se_channel *c;
	TEE_Result ret;

	assert(s && channel && !*channel);

	ret = iso7816_open_available_logical_channel(s, &channel_id);
	if (ret != TEE_SUCCESS)
		return ret;

	c = tee_se_channel_alloc(s, channel_id);
	if (!c)
		goto err_close_channel;

	if (aid != NULL) {
		ret = iso7816_select(c, aid);
		if (ret != TEE_SUCCESS)
			goto err_free_channel;
	}

	*channel = c;
	TAILQ_INSERT_TAIL(&s->channels, c, link);

	return TEE_SUCCESS;

err_free_channel:
	tee_se_channel_free(c);
err_close_channel:
	iso7816_close_logical_channel(s, channel_id);

	return ret;
}

void tee_se_session_close_channel(struct tee_se_session *s,
		struct tee_se_channel *c)
{
	int channel_id;

	assert(s && c);
	channel_id = tee_se_channel_get_id(c);
	if (channel_id > 0) {
		iso7816_close_logical_channel(s, channel_id);
	} else {
		tee_se_reader_unlock_basic_channel(s->reader_proxy);
	}

	TAILQ_REMOVE(&s->channels, c, link);
	tee_se_channel_free(c);
}

TEE_Result tee_se_session_transmit(struct tee_se_session *s,
		struct cmd_apdu *c, struct resp_apdu *r)
{
	struct tee_se_reader_proxy *h = s->reader_proxy;

	/*
	 * This call might block the caller.
	 * The reader proxy will make sure only 1 session
	 * is transmitting. Others should wait until the
	 * activating transation finished.
	 */
	return iso7816_exchange_apdu(h, c, r);
}

void tee_se_session_close(struct tee_se_session *s)
{
	struct tee_se_channel *c;

	assert(s);

	TAILQ_FOREACH(c, &s->channels, link)
		tee_se_session_close_channel(s, c);

	tee_se_reader_detach(s->reader_proxy);

	tee_se_session_free(s);
}