aboutsummaryrefslogtreecommitdiff
path: root/core/tee/se/apdu.c
blob: c721c101327ee62712348189f491c54a91f5b8bc (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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <tee_api_types.h>
#include <trace.h>

#include <tee/se/apdu.h>
#include <tee/se/util.h>

#include "apdu_priv.h"

/*
 * APDU format, [..] means optional fields
 *
 * CMD_APDU: CLA, INS, P1, P2, [LC, DATA, LE]
 * RESP_APDU: [DATA], SW1, SW2
 *
 */
#define CMD_APDU_SIZE(lc) ((lc) + 4)
#define RESP_APDU_SIZE(le) ((le) + 2)

struct cmd_apdu *alloc_cmd_apdu(uint8_t cla, uint8_t ins, uint8_t p1,
		uint8_t p2, uint8_t lc, uint8_t le, uint8_t *data)
{
	size_t apdu_length = CMD_APDU_SIZE(lc);
	size_t total_length;
	struct cmd_apdu *apdu;
	uint8_t *buf;

	/*
	 * check if we need to reserve space for LC/LE
	 * (both fields are optional)
	 */
	if (lc)
		apdu_length++;
	if (le)
		apdu_length++;

	total_length = sizeof(struct cmd_apdu) + apdu_length;
	apdu = malloc(total_length);

	if (!apdu)
		return NULL;

	apdu->base.length = apdu_length;
	apdu->base.data_buf = (uint8_t *)(apdu + 1);
	apdu->base.refcnt = 1;

	buf = apdu->base.data_buf;
	buf[CLA] = cla;
	buf[INS] = ins;
	buf[P1] = p1;
	buf[P2] = p2;
	if (lc)
		buf[LC] = lc;
	if (data != NULL)
		memmove(&buf[CDATA], data, lc);
	if (le)
		buf[CDATA + lc + OFF_LE] = le;

	return apdu;
}

struct cmd_apdu *alloc_cmd_apdu_from_buf(uint8_t *buf, size_t length)
{
	struct cmd_apdu *apdu = malloc(sizeof(struct cmd_apdu));

	if (!apdu)
		return NULL;
	apdu->base.length = length;
	apdu->base.data_buf = buf;
	apdu->base.refcnt = 1;
	return apdu;
}

struct resp_apdu *alloc_resp_apdu(uint8_t le)
{
	size_t total_length = sizeof(struct resp_apdu) + RESP_APDU_SIZE(le);
	struct resp_apdu *apdu;

	apdu = malloc(total_length);

	if (!apdu)
		return NULL;

	apdu->base.length = RESP_APDU_SIZE(le);
	apdu->base.data_buf = (uint8_t *)(apdu + 1);
	apdu->base.refcnt = 1;

	return apdu;
}

uint8_t *resp_apdu_get_data(struct resp_apdu *apdu)
{
	assert(apdu);
	return apdu->resp_data;
}

size_t resp_apdu_get_data_len(struct resp_apdu *apdu)
{
	assert(apdu);
	return apdu->resp_data_len;
}

uint8_t resp_apdu_get_sw1(struct resp_apdu *apdu)
{
	assert(apdu);
	return apdu->sw1;
}

uint8_t resp_apdu_get_sw2(struct resp_apdu *apdu)
{
	assert(apdu);
	return apdu->sw2;
}

uint8_t *apdu_get_data(struct apdu_base *apdu)
{
	assert(apdu);
	return apdu->data_buf;
}
size_t apdu_get_length(struct apdu_base *apdu)
{
	assert(apdu);
	return apdu->length;
}
int apdu_get_refcnt(struct apdu_base *apdu)
{
	assert(apdu);
	return apdu->refcnt;
}
void apdu_acquire(struct apdu_base *apdu)
{
	assert(apdu);
	apdu->refcnt++;
}
void apdu_release(struct apdu_base *apdu)
{
	assert(apdu);
	apdu->refcnt--;
	if (apdu->refcnt == 0)
		free(apdu);
}

void parse_resp_apdu(struct resp_apdu *apdu)
{
	uint8_t *buf = apdu->base.data_buf;
	/* resp data length =  resp buf length - SW1 - SW2 */
	apdu->resp_data_len = apdu->base.length - 2;
	if (apdu->resp_data_len > 0)
		apdu->resp_data = &buf[RDATA];
	else
		apdu->resp_data = NULL;
	apdu->sw1 = buf[RDATA + apdu->resp_data_len + OFF_SW1];
	apdu->sw2 = buf[RDATA + apdu->resp_data_len + OFF_SW2];
}