xref: /optee_os/ta/pkcs11/src/attributes.c (revision 9fc2442cc66c279cb962c90c4375746fc9b28bb9)
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 bool attributes_match_reference(struct obj_attrs *candidate,
211 				struct obj_attrs *ref)
212 {
213 	size_t count = ref->attrs_count;
214 	unsigned char *ref_attr = ref->attrs;
215 	uint32_t rc = PKCS11_CKR_GENERAL_ERROR;
216 
217 	if (!ref->attrs_count) {
218 		DMSG("Empty reference match all");
219 		return true;
220 	}
221 
222 	for (count = 0; count < ref->attrs_count; count++) {
223 		struct pkcs11_attribute_head pkcs11_ref = { };
224 		void *value = NULL;
225 		uint32_t size = 0;
226 
227 		TEE_MemMove(&pkcs11_ref, ref_attr, sizeof(pkcs11_ref));
228 
229 		rc = get_attribute_ptr(candidate, pkcs11_ref.id, &value, &size);
230 
231 		if (rc || !value || size != pkcs11_ref.size ||
232 		    TEE_MemCompare(ref_attr + sizeof(pkcs11_ref), value, size))
233 			return false;
234 
235 		ref_attr += sizeof(pkcs11_ref) + pkcs11_ref.size;
236 	}
237 
238 	return true;
239 }
240 
241 #if CFG_TEE_TA_LOG_LEVEL > 0
242 /*
243  * Debug: dump CK attribute array to output trace
244  */
245 #define ATTR_TRACE_FMT	"%s attr %s / %s\t(0x%04"PRIx32" %"PRIu32"-byte"
246 #define ATTR_FMT_0BYTE	ATTR_TRACE_FMT ")"
247 #define ATTR_FMT_1BYTE	ATTR_TRACE_FMT ": %02x)"
248 #define ATTR_FMT_2BYTE	ATTR_TRACE_FMT ": %02x %02x)"
249 #define ATTR_FMT_3BYTE	ATTR_TRACE_FMT ": %02x %02x %02x)"
250 #define ATTR_FMT_4BYTE	ATTR_TRACE_FMT ": %02x %02x %02x %02x)"
251 #define ATTR_FMT_ARRAY	ATTR_TRACE_FMT ": %02x %02x %02x %02x ...)"
252 
253 static void __trace_attributes(char *prefix, void *src, void *end)
254 {
255 	size_t next_off = 0;
256 	char *prefix2 = NULL;
257 	size_t prefix_len = strlen(prefix);
258 	char *cur = src;
259 
260 	/* append 4 spaces to the prefix plus terminal '\0' */
261 	prefix2 = TEE_Malloc(prefix_len + 1 + 4, TEE_MALLOC_FILL_ZERO);
262 	if (!prefix2)
263 		return;
264 
265 	TEE_MemMove(prefix2, prefix, prefix_len + 1);
266 	TEE_MemFill(prefix2 + prefix_len, ' ', 4);
267 	*(prefix2 + prefix_len + 4) = '\0';
268 
269 	for (; cur < (char *)end; cur += next_off) {
270 		struct pkcs11_attribute_head pkcs11_ref = { };
271 		uint8_t data[4] = { 0 };
272 
273 		TEE_MemMove(&pkcs11_ref, cur, sizeof(pkcs11_ref));
274 		TEE_MemMove(&data[0], cur + sizeof(pkcs11_ref),
275 			    MIN(pkcs11_ref.size, sizeof(data)));
276 
277 		next_off = sizeof(pkcs11_ref) + pkcs11_ref.size;
278 
279 		switch (pkcs11_ref.size) {
280 		case 0:
281 			IMSG_RAW(ATTR_FMT_0BYTE,
282 				 prefix, id2str_attr(pkcs11_ref.id), "*",
283 				 pkcs11_ref.id, pkcs11_ref.size);
284 			break;
285 		case 1:
286 			IMSG_RAW(ATTR_FMT_1BYTE,
287 				 prefix, id2str_attr(pkcs11_ref.id),
288 				 id2str_attr_value(pkcs11_ref.id,
289 						   pkcs11_ref.size,
290 						   cur + sizeof(pkcs11_ref)),
291 				 pkcs11_ref.id, pkcs11_ref.size, data[0]);
292 			break;
293 		case 2:
294 			IMSG_RAW(ATTR_FMT_2BYTE,
295 				 prefix, id2str_attr(pkcs11_ref.id),
296 				 id2str_attr_value(pkcs11_ref.id,
297 						   pkcs11_ref.size,
298 						   cur + sizeof(pkcs11_ref)),
299 				 pkcs11_ref.id, pkcs11_ref.size, data[0],
300 				 data[1]);
301 			break;
302 		case 3:
303 			IMSG_RAW(ATTR_FMT_3BYTE,
304 				 prefix, id2str_attr(pkcs11_ref.id),
305 				 id2str_attr_value(pkcs11_ref.id,
306 						   pkcs11_ref.size,
307 						   cur + sizeof(pkcs11_ref)),
308 				 pkcs11_ref.id, pkcs11_ref.size,
309 				 data[0], data[1], data[2]);
310 			break;
311 		case 4:
312 			IMSG_RAW(ATTR_FMT_4BYTE,
313 				 prefix, id2str_attr(pkcs11_ref.id),
314 				 id2str_attr_value(pkcs11_ref.id,
315 						   pkcs11_ref.size,
316 						   cur + sizeof(pkcs11_ref)),
317 				 pkcs11_ref.id, pkcs11_ref.size,
318 				 data[0], data[1], data[2], data[3]);
319 			break;
320 		default:
321 			IMSG_RAW(ATTR_FMT_ARRAY,
322 				 prefix, id2str_attr(pkcs11_ref.id),
323 				 id2str_attr_value(pkcs11_ref.id,
324 						   pkcs11_ref.size,
325 						   cur + sizeof(pkcs11_ref)),
326 				 pkcs11_ref.id, pkcs11_ref.size,
327 				 data[0], data[1], data[2], data[3]);
328 			break;
329 		}
330 
331 		switch (pkcs11_ref.id) {
332 		case PKCS11_CKA_WRAP_TEMPLATE:
333 		case PKCS11_CKA_UNWRAP_TEMPLATE:
334 		case PKCS11_CKA_DERIVE_TEMPLATE:
335 			if (pkcs11_ref.size)
336 				trace_attributes(prefix2,
337 						 cur + sizeof(pkcs11_ref));
338 			break;
339 		default:
340 			break;
341 		}
342 	}
343 
344 	/* Sanity */
345 	if (cur != end)
346 		EMSG("Warning: unexpected alignment in object attributes");
347 
348 	TEE_Free(prefix2);
349 }
350 
351 void trace_attributes(const char *prefix, void *ref)
352 {
353 	struct obj_attrs head = { };
354 	char *pre = NULL;
355 
356 	TEE_MemMove(&head, ref, sizeof(head));
357 
358 	if (!head.attrs_count)
359 		return;
360 
361 	pre = TEE_Malloc(prefix ? strlen(prefix) + 2 : 2, TEE_MALLOC_FILL_ZERO);
362 	if (!pre) {
363 		EMSG("%s: out of memory", prefix);
364 		return;
365 	}
366 
367 	if (prefix)
368 		TEE_MemMove(pre, prefix, strlen(prefix));
369 
370 	IMSG_RAW("%s,--- (serial object) Attributes list --------", pre);
371 	IMSG_RAW("%s| %"PRIu32" item(s) - %"PRIu32" bytes",
372 		 pre, head.attrs_count, head.attrs_size);
373 
374 	pre[prefix ? strlen(prefix) : 0] = '|';
375 	__trace_attributes(pre, (char *)ref + sizeof(head),
376 			   (char *)ref + sizeof(head) + head.attrs_size);
377 
378 	IMSG_RAW("%s`-----------------------", prefix ? prefix : "");
379 
380 	TEE_Free(pre);
381 }
382 #endif /*CFG_TEE_TA_LOG_LEVEL*/
383