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