xref: /optee_os/ta/pkcs11/src/attributes.c (revision 36bb435f5be571b4b4c0792de1cea85013afa728)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <compiler.h>
8 #include <pkcs11_ta.h>
9 #include <stddef.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <string_ext.h>
13 #include <tee_internal_api.h>
14 #include <tee_internal_api_extensions.h>
15 #include <trace.h>
16 #include <util.h>
17 
18 #include "attributes.h"
19 #include "pkcs11_helpers.h"
20 #include "serializer.h"
21 
22 enum pkcs11_rc init_attributes_head(struct obj_attrs **head)
23 {
24 	*head = TEE_Malloc(sizeof(**head), TEE_MALLOC_FILL_ZERO);
25 	if (!*head)
26 		return PKCS11_CKR_DEVICE_MEMORY;
27 
28 	return PKCS11_CKR_OK;
29 }
30 
31 enum pkcs11_rc add_attribute(struct obj_attrs **head, uint32_t attribute,
32 			     void *data, size_t size)
33 {
34 	size_t buf_len = sizeof(struct obj_attrs) + (*head)->attrs_size;
35 	char **bstart = (void *)head;
36 	enum pkcs11_rc rc = PKCS11_CKR_OK;
37 	uint32_t data32 = 0;
38 
39 	data32 = attribute;
40 	rc = serialize(bstart, &buf_len, &data32, sizeof(uint32_t));
41 	if (rc)
42 		return rc;
43 
44 	data32 = size;
45 	rc = serialize(bstart, &buf_len, &data32, sizeof(uint32_t));
46 	if (rc)
47 		return rc;
48 
49 	rc = serialize(bstart, &buf_len, data, size);
50 	if (rc)
51 		return rc;
52 
53 	/* Alloced buffer is always well aligned */
54 	head = (void *)bstart;
55 	(*head)->attrs_size += 2 * sizeof(uint32_t) + size;
56 	(*head)->attrs_count++;
57 
58 	return rc;
59 }
60 
61 static enum pkcs11_rc _remove_attribute(struct obj_attrs **head,
62 					uint32_t attribute, bool empty)
63 {
64 	struct obj_attrs *h = *head;
65 	char *cur = NULL;
66 	char *end = NULL;
67 	size_t next_off = 0;
68 
69 	/* Let's find the target attribute */
70 	cur = (char *)h + sizeof(struct obj_attrs);
71 	end = cur + h->attrs_size;
72 	for (; cur < end; cur += next_off) {
73 		struct pkcs11_attribute_head pkcs11_ref = { };
74 
75 		TEE_MemMove(&pkcs11_ref, cur, sizeof(pkcs11_ref));
76 		next_off = sizeof(pkcs11_ref) + pkcs11_ref.size;
77 
78 		if (pkcs11_ref.id != attribute)
79 			continue;
80 
81 		if (empty && pkcs11_ref.size)
82 			return PKCS11_CKR_FUNCTION_FAILED;
83 
84 		TEE_MemMove(cur, cur + next_off, end - (cur + next_off));
85 
86 		h->attrs_count--;
87 		h->attrs_size -= next_off;
88 		end -= next_off;
89 		next_off = 0;
90 
91 		return PKCS11_CKR_OK;
92 	}
93 
94 	DMSG("Attribute %s (%#x) not found", id2str_attr(attribute), attribute);
95 	return PKCS11_RV_NOT_FOUND;
96 }
97 
98 enum pkcs11_rc remove_empty_attribute(struct obj_attrs **head,
99 				      uint32_t attribute)
100 {
101 	return _remove_attribute(head, attribute, true /* empty */);
102 }
103 
104 void get_attribute_ptrs(struct obj_attrs *head, uint32_t attribute,
105 			void **attr, uint32_t *attr_size, size_t *count)
106 {
107 	char *cur = (char *)head + sizeof(struct obj_attrs);
108 	char *end = cur + head->attrs_size;
109 	size_t next_off = 0;
110 	size_t max_found = *count;
111 	size_t found = 0;
112 	void **attr_ptr = attr;
113 	uint32_t *attr_size_ptr = attr_size;
114 
115 	for (; cur < end; cur += next_off) {
116 		/* Structure aligned copy of the pkcs11_ref in the object */
117 		struct pkcs11_attribute_head pkcs11_ref = { };
118 
119 		TEE_MemMove(&pkcs11_ref, cur, sizeof(pkcs11_ref));
120 		next_off = sizeof(pkcs11_ref) + pkcs11_ref.size;
121 
122 		if (pkcs11_ref.id != attribute)
123 			continue;
124 
125 		found++;
126 
127 		if (!max_found)
128 			continue;	/* only count matching attributes */
129 
130 		if (attr) {
131 			if (pkcs11_ref.size)
132 				*attr_ptr++ = cur + sizeof(pkcs11_ref);
133 			else
134 				*attr_ptr++ = NULL;
135 		}
136 
137 		if (attr_size)
138 			*attr_size_ptr++ = pkcs11_ref.size;
139 
140 		if (found == max_found)
141 			break;
142 	}
143 
144 	/* Sanity */
145 	if (cur > end) {
146 		DMSG("Exceeding serial object length");
147 		TEE_Panic(0);
148 	}
149 
150 	*count = found;
151 }
152 
153 enum pkcs11_rc get_attribute_ptr(struct obj_attrs *head, uint32_t attribute,
154 				 void **attr_ptr, uint32_t *attr_size)
155 {
156 	size_t count = 1;
157 
158 	get_attribute_ptrs(head, attribute, attr_ptr, attr_size, &count);
159 
160 	if (!count)
161 		return PKCS11_RV_NOT_FOUND;
162 
163 	if (count != 1)
164 		return PKCS11_CKR_GENERAL_ERROR;
165 
166 	return PKCS11_CKR_OK;
167 }
168 
169 enum pkcs11_rc get_attribute(struct obj_attrs *head, uint32_t attribute,
170 			     void *attr, uint32_t *attr_size)
171 {
172 	enum pkcs11_rc rc = PKCS11_CKR_OK;
173 	void *attr_ptr = NULL;
174 	uint32_t size = 0;
175 
176 	rc = get_attribute_ptr(head, attribute, &attr_ptr, &size);
177 	if (rc)
178 		return rc;
179 
180 	if (attr_size && *attr_size != size) {
181 		*attr_size = size;
182 		/* This reuses buffer-to-small for any bad size matching */
183 		return PKCS11_CKR_BUFFER_TOO_SMALL;
184 	}
185 
186 	if (attr)
187 		TEE_MemMove(attr, attr_ptr, size);
188 
189 	if (attr_size)
190 		*attr_size = size;
191 
192 	return PKCS11_CKR_OK;
193 }
194 
195 bool get_bool(struct obj_attrs *head, uint32_t attribute)
196 {
197 	enum pkcs11_rc rc = PKCS11_CKR_OK;
198 	uint8_t bbool = 0;
199 	uint32_t size = sizeof(bbool);
200 
201 	rc = get_attribute(head, attribute, &bbool, &size);
202 
203 	if (rc == PKCS11_RV_NOT_FOUND)
204 		return false;
205 
206 	assert(rc == PKCS11_CKR_OK);
207 	return bbool;
208 }
209 
210 #if CFG_TEE_TA_LOG_LEVEL > 0
211 /*
212  * Debug: dump CK attribute array to output trace
213  */
214 #define ATTR_TRACE_FMT	"%s attr %s / %s\t(0x%04"PRIx32" %"PRIu32"-byte"
215 #define ATTR_FMT_0BYTE	ATTR_TRACE_FMT ")"
216 #define ATTR_FMT_1BYTE	ATTR_TRACE_FMT ": %02x)"
217 #define ATTR_FMT_2BYTE	ATTR_TRACE_FMT ": %02x %02x)"
218 #define ATTR_FMT_3BYTE	ATTR_TRACE_FMT ": %02x %02x %02x)"
219 #define ATTR_FMT_4BYTE	ATTR_TRACE_FMT ": %02x %02x %02x %02x)"
220 #define ATTR_FMT_ARRAY	ATTR_TRACE_FMT ": %02x %02x %02x %02x ...)"
221 
222 static void __trace_attributes(char *prefix, void *src, void *end)
223 {
224 	size_t next_off = 0;
225 	char *prefix2 = NULL;
226 	size_t prefix_len = strlen(prefix);
227 	char *cur = src;
228 
229 	/* append 4 spaces to the prefix plus terminal '\0' */
230 	prefix2 = TEE_Malloc(prefix_len + 1 + 4, TEE_MALLOC_FILL_ZERO);
231 	if (!prefix2)
232 		return;
233 
234 	TEE_MemMove(prefix2, prefix, prefix_len + 1);
235 	TEE_MemFill(prefix2 + prefix_len, ' ', 4);
236 	*(prefix2 + prefix_len + 4) = '\0';
237 
238 	for (; cur < (char *)end; cur += next_off) {
239 		struct pkcs11_attribute_head pkcs11_ref = { };
240 		uint8_t data[4] = { 0 };
241 
242 		TEE_MemMove(&pkcs11_ref, cur, sizeof(pkcs11_ref));
243 		TEE_MemMove(&data[0], cur + sizeof(pkcs11_ref),
244 			    MIN(pkcs11_ref.size, sizeof(data)));
245 
246 		next_off = sizeof(pkcs11_ref) + pkcs11_ref.size;
247 
248 		switch (pkcs11_ref.size) {
249 		case 0:
250 			IMSG_RAW(ATTR_FMT_0BYTE,
251 				 prefix, id2str_attr(pkcs11_ref.id), "*",
252 				 pkcs11_ref.id, pkcs11_ref.size);
253 			break;
254 		case 1:
255 			IMSG_RAW(ATTR_FMT_1BYTE,
256 				 prefix, id2str_attr(pkcs11_ref.id),
257 				 id2str_attr_value(pkcs11_ref.id,
258 						   pkcs11_ref.size,
259 						   cur + sizeof(pkcs11_ref)),
260 				 pkcs11_ref.id, pkcs11_ref.size, data[0]);
261 			break;
262 		case 2:
263 			IMSG_RAW(ATTR_FMT_2BYTE,
264 				 prefix, id2str_attr(pkcs11_ref.id),
265 				 id2str_attr_value(pkcs11_ref.id,
266 						   pkcs11_ref.size,
267 						   cur + sizeof(pkcs11_ref)),
268 				 pkcs11_ref.id, pkcs11_ref.size, data[0],
269 				 data[1]);
270 			break;
271 		case 3:
272 			IMSG_RAW(ATTR_FMT_3BYTE,
273 				 prefix, id2str_attr(pkcs11_ref.id),
274 				 id2str_attr_value(pkcs11_ref.id,
275 						   pkcs11_ref.size,
276 						   cur + sizeof(pkcs11_ref)),
277 				 pkcs11_ref.id, pkcs11_ref.size,
278 				 data[0], data[1], data[2]);
279 			break;
280 		case 4:
281 			IMSG_RAW(ATTR_FMT_4BYTE,
282 				 prefix, id2str_attr(pkcs11_ref.id),
283 				 id2str_attr_value(pkcs11_ref.id,
284 						   pkcs11_ref.size,
285 						   cur + sizeof(pkcs11_ref)),
286 				 pkcs11_ref.id, pkcs11_ref.size,
287 				 data[0], data[1], data[2], data[3]);
288 			break;
289 		default:
290 			IMSG_RAW(ATTR_FMT_ARRAY,
291 				 prefix, id2str_attr(pkcs11_ref.id),
292 				 id2str_attr_value(pkcs11_ref.id,
293 						   pkcs11_ref.size,
294 						   cur + sizeof(pkcs11_ref)),
295 				 pkcs11_ref.id, pkcs11_ref.size,
296 				 data[0], data[1], data[2], data[3]);
297 			break;
298 		}
299 
300 		switch (pkcs11_ref.id) {
301 		case PKCS11_CKA_WRAP_TEMPLATE:
302 		case PKCS11_CKA_UNWRAP_TEMPLATE:
303 		case PKCS11_CKA_DERIVE_TEMPLATE:
304 			if (pkcs11_ref.size)
305 				trace_attributes(prefix2,
306 						 cur + sizeof(pkcs11_ref));
307 			break;
308 		default:
309 			break;
310 		}
311 	}
312 
313 	/* Sanity */
314 	if (cur != end)
315 		EMSG("Warning: unexpected alignment in object attributes");
316 
317 	TEE_Free(prefix2);
318 }
319 
320 void trace_attributes(const char *prefix, void *ref)
321 {
322 	struct obj_attrs head = { };
323 	char *pre = NULL;
324 
325 	TEE_MemMove(&head, ref, sizeof(head));
326 
327 	if (!head.attrs_count)
328 		return;
329 
330 	pre = TEE_Malloc(prefix ? strlen(prefix) + 2 : 2, TEE_MALLOC_FILL_ZERO);
331 	if (!pre) {
332 		EMSG("%s: out of memory", prefix);
333 		return;
334 	}
335 
336 	if (prefix)
337 		TEE_MemMove(pre, prefix, strlen(prefix));
338 
339 	IMSG_RAW("%s,--- (serial object) Attributes list --------", pre);
340 	IMSG_RAW("%s| %"PRIu32" item(s) - %"PRIu32" bytes",
341 		 pre, head.attrs_count, head.attrs_size);
342 
343 	pre[prefix ? strlen(prefix) : 0] = '|';
344 	__trace_attributes(pre, (char *)ref + sizeof(head),
345 			   (char *)ref + sizeof(head) + head.attrs_size);
346 
347 	IMSG_RAW("%s`-----------------------", prefix ? prefix : "");
348 
349 	TEE_Free(pre);
350 }
351 #endif /*CFG_TEE_TA_LOG_LEVEL*/
352