xref: /rk3399_rockchip-uboot/common/attestation_key.c (revision b27ae02dfdf0e26d23901e9b898629d6ec470a60)
1 /*
2  * Copyright 2018, Rockchip Electronics Co., Ltd
3  * qiujian, <qiujian@rock-chips.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include "attestation_key.h"
9 
10 #include <common.h>
11 #include <malloc.h>
12 
13 #include <keymaster.h>
14 
15 /* attestation data offset */
16 #define ATTESTATION_DATA_OFFSET  65536
17 
18 /* block size */
19 #define ATTESTATION_DATA_BLOCK_SIZE 512
20 
21 /* attestation data block offset */
22 #define ATTESTATION_DATA_BLOCK_OFFSET (ATTESTATION_DATA_OFFSET / ATTESTATION_DATA_BLOCK_SIZE)
23 
24 #define ATAP_BLOB_LEN_MAX 2048
25 #define ATAP_CERT_CHAIN_LEN_MAX 8192
26 #define ATAP_CERT_CHAIN_ENTRIES_MAX 8
27 #define CA_HEADER_LEN 16
28 
29 /*
30  * Name of the attestation key file is
31  * ATTESTATION_KEY_PREFIX.%algorithm,
32  * which include PrivateKey and CertificateChain,
33  * where algorithm is either "EC" or "RSA"
34  */
35 #define ATTESTATION_KEY_FILE "AttestationKey"
36 
37 /*
38  * Name of the attestation key file is
39  * ATTESTATION_KEY_PREFIX.%algorithm,
40  * where algorithm is either "EC" or "RSA"
41  */
42 #define ATTESTATION_KEY_PREFIX "PrivateKey"
43 
44 /*
45  * Name of the attestation certificate file is
46  * ATTESTATION_CERT_PREFIX.%algorithm.%index,
47  * where index is the index within the certificate chain.
48  */
49 #define ATTESTATION_CERT_PREFIX "CertificateChain"
50 
51 /* Maximum file name size.*/
52 #define STORAGE_ID_LENGTH_MAX  64
53 
54 typedef enum{
55 	KM_ALGORITHM_RSA = 1,
56 	KM_ALGORITHM_EC = 3,
57 } keymaster_algorithm_t;
58 
59 typedef struct {
60 	uint8_t *data;
61 	uint32_t data_length;
62 } atap_blob;
63 
64 typedef struct {
65 	atap_blob entries[ATAP_CERT_CHAIN_ENTRIES_MAX];
66 	uint32_t entry_count;
67 } atap_certchain;
68 
69 uint32_t write_to_keymaster(uint8_t *filename, uint32_t filename_size,
70 			    uint8_t *data, uint32_t data_size);
71 
72 static const char *get_keyslot_str(keymaster_algorithm_t key_type)
73 {
74 	switch (key_type) {
75 	case KM_ALGORITHM_RSA:
76 		return "RSA";
77 	case KM_ALGORITHM_EC:
78 		return "EC";
79 	default:
80 		return "";
81 	}
82 }
83 
84 static void free_blob(atap_blob blob)
85 {
86 	if (blob.data)
87 		free(blob.data);
88 
89 	blob.data_length = 0;
90 }
91 
92 static void free_cert_chain(atap_certchain cert_chain)
93 {
94 	unsigned int i = 0;
95 
96 	for (i = 0; i < cert_chain.entry_count; ++i) {
97 		if (cert_chain.entries[i].data)
98 			free(cert_chain.entries[i].data);
99 		cert_chain.entries[i].data_length = 0;
100 	}
101 	memset(&cert_chain, 0, sizeof(atap_certchain));
102 }
103 
104 static void copy_from_buf(uint8_t **buf_ptr, void *data, uint32_t data_size)
105 {
106 	memcpy(data, *buf_ptr, data_size);
107 	*buf_ptr += data_size;
108 }
109 
110 static void copy_uint32_from_buf(uint8_t **buf_ptr, uint32_t *x)
111 {
112 	copy_from_buf(buf_ptr, x, sizeof(uint32_t));
113 }
114 
115 static bool copy_blob_from_buf(uint8_t **buf_ptr, atap_blob *blob)
116 {
117 	memset(blob, 0, sizeof(atap_blob));
118 	copy_uint32_from_buf(buf_ptr, &blob->data_length);
119 
120 	if (blob->data_length > ATAP_BLOB_LEN_MAX)
121 		return false;
122 
123 	if (blob->data_length) {
124 		blob->data = (uint8_t *) malloc(blob->data_length);
125 		if (blob->data == NULL)
126 			return false;
127 
128 		copy_from_buf(buf_ptr, blob->data, blob->data_length);
129 	}
130 	return true;
131 }
132 
133 static bool copy_cert_chain_from_buf(uint8_t **buf_ptr,
134 					atap_certchain *cert_chain)
135 {
136 	uint32_t cert_chain_size = 0;
137 	int32_t bytes_remaining = 0;
138 	size_t i = 0;
139 	bool retval = true;
140 
141 	memset(cert_chain, 0, sizeof(atap_certchain));
142 
143 	/* Copy size of cert chain, as it is a Variable field. */
144 	copy_from_buf(buf_ptr, &cert_chain_size, sizeof(cert_chain_size));
145 
146 	if (cert_chain_size > ATAP_CERT_CHAIN_LEN_MAX)
147 		return false;
148 
149 	if (cert_chain_size == 0)
150 		return true;
151 
152 	bytes_remaining = cert_chain_size;
153 	for (i = 0; i < ATAP_CERT_CHAIN_ENTRIES_MAX; ++i) {
154 		if (!copy_blob_from_buf(buf_ptr, &cert_chain->entries[i])) {
155 			retval = false;
156 			break;
157 		}
158 
159 		++cert_chain->entry_count;
160 		bytes_remaining -= (sizeof(uint32_t) +
161 					cert_chain->entries[i].data_length);
162 
163 		if (bytes_remaining <= 0) {
164 			retval = (bytes_remaining == 0);
165 			break;
166 		}
167 	}
168 	if (retval == false)
169 		free_cert_chain(*cert_chain);
170 
171 	return retval;
172 }
173 
174 /* validate attestation data head. */
175 static bool validate_ca_header(const uint8_t *buf, uint32_t buf_size)
176 {
177 
178 	if (buf[0] != 'C' || buf[1] != 'A' || buf[2] != 0)
179 		return false;
180 
181 	uint32_t data_size;
182 
183 	memcpy(&data_size, buf + 3, sizeof(uint32_t));
184 
185 	if (data_size <= 0 || data_size > ATTESTATION_DATA_OFFSET) {
186 		printf("invalide data_size:%d\n", data_size);
187 		return false;
188 	}
189 
190 	uint32_t real_size;
191 
192 	memcpy(&real_size, buf + 3 + sizeof(uint32_t), sizeof(uint32_t));
193 	if (real_size <= 0 || real_size > data_size) {
194 		printf("invalide real_size:%d\n", real_size);
195 		return false;
196 	}
197 	return true;
198 }
199 
200 /* write key to security storage. */
201 static uint32_t write_key(keymaster_algorithm_t key_type,
202 			  unsigned char *key_name,
203 			  const uint8_t *key, uint32_t key_size)
204 {
205 	char key_file[STORAGE_ID_LENGTH_MAX] = {0};
206 
207 	snprintf(key_file, STORAGE_ID_LENGTH_MAX, "%s.%s", key_name,
208 		 get_keyslot_str(key_type));
209 	write_to_keymaster((uint8_t *)key_file, strlen(key_file),
210 				(uint8_t *)key, key_size);
211 	return 0;
212 }
213 
214 /* write cert to security storage. */
215 static uint32_t write_cert(keymaster_algorithm_t key_type, const uint8_t *cert,
216 				uint32_t cert_size, uint32_t index)
217 {
218 	char cert_file[STORAGE_ID_LENGTH_MAX] = {0};
219 
220 	snprintf(cert_file, STORAGE_ID_LENGTH_MAX, "%s.%s.%d", ATTESTATION_CERT_PREFIX,
221 		get_keyslot_str(key_type), index);
222 	write_to_keymaster((uint8_t *)cert_file, strlen(cert_file),
223 				(uint8_t *)cert, cert_size);
224 	return 0;
225 }
226 
227 /* write cert chain length to security storage. */
228 static uint32_t write_cert_chain_length(keymaster_algorithm_t key_type,
229 				uint8_t chain_len)
230 {
231 	char cert_chain_length_file[STORAGE_ID_LENGTH_MAX] = {0};
232 	uint8_t data = chain_len;
233 	uint32_t len = 1;
234 
235 	snprintf(cert_chain_length_file, STORAGE_ID_LENGTH_MAX, "%s.%s.length",
236 		ATTESTATION_CERT_PREFIX, get_keyslot_str(key_type));
237 	write_to_keymaster((uint8_t *)cert_chain_length_file,
238 				strlen(cert_chain_length_file), &data, len);
239 
240 	return 0;
241 }
242 
243 atap_result load_attestation_key(struct blk_desc *dev_desc,
244 				disk_partition_t *misc_partition)
245 {
246 	int ret;
247 	unsigned char key_name[STORAGE_ID_LENGTH_MAX] = {0};
248 
249 	if (!dev_desc) {
250 		printf("%s: Could not find device\n", __func__);
251 		return ATAP_RESULT_ERROR_DEVICE_NOT_FOUND;
252 	}
253 
254 	if (misc_partition == NULL) {
255 		printf("misc partition not found!\n");
256 		return ATAP_RESULT_ERROR_PARTITION_NOT_FOUND;
257 	}
258 
259 	/* get attestation data offset from misc partition */
260 	lbaint_t key_offset = misc_partition->start +
261 				misc_partition->size - ATTESTATION_DATA_BLOCK_OFFSET;
262 
263 	/* read ca head from attestation data offset */
264 	uint8_t ca_headr[ATTESTATION_DATA_BLOCK_SIZE];
265 
266 	ret = blk_dread(dev_desc, key_offset, 1, ca_headr);
267 	if (ret != 1) {
268 		printf("failed to read ca head from misc\n");
269 		return ATAP_RESULT_ERROR_BLOCK_READ;
270 	}
271 
272 	if (!validate_ca_header(ca_headr, sizeof(ca_headr))) {
273 		debug("ca head not found\n");
274 		return ATAP_RESULT_ERROR_INVALID_HEAD;
275 	}
276 
277 	/* get attestation data size from ca head */
278 	uint32_t real_size;
279 
280 	memcpy(&real_size, ca_headr + 3 + sizeof(uint32_t), sizeof(uint32_t));
281 
282 	/* calculate real block size of attestation data */
283 	int real_block_num = real_size / ATTESTATION_DATA_BLOCK_SIZE;
284 
285 	if (real_size % ATTESTATION_DATA_BLOCK_SIZE != 0)
286 		real_block_num++;
287 
288 	unsigned char keybuf[ATTESTATION_DATA_OFFSET] = {0};
289 
290 	/* check block size */
291 	if (real_block_num <= 0 || real_block_num > ATTESTATION_DATA_BLOCK_OFFSET) {
292 		printf("invalidate real_block_num:%d\n", real_block_num);
293 		return ATAP_RESULT_ERROR_INVALID_BLOCK_NUM;
294 	}
295 
296 	/* read all attestation data from misc */
297 	if (blk_dread(dev_desc, key_offset, real_block_num, keybuf) != real_block_num) {
298 		printf("failed to read misc key\n");
299 		return ATAP_RESULT_ERROR_BLOCK_READ;
300 	}
301 
302 	/* read device id from buf*/
303 	uint32_t device_id_size = 0;
304 	uint8_t device_id[32] = {0};
305 
306 	memcpy(&device_id_size, keybuf + 16, sizeof(uint32_t));
307 	if (device_id_size < 0 || device_id_size > sizeof(device_id)) {
308 		printf("invalidate device_id_size:%d\n", device_id_size);
309 		return ATAP_RESULT_ERROR_INVALID_DEVICE_ID;
310 	}
311 
312 	memcpy(device_id, keybuf + 16 + sizeof(uint32_t), device_id_size);
313 	debug("device_id:%s\n", device_id);
314 
315 	/* read algorithm from buf */
316 	uint8_t *key_buf = keybuf + 16 + sizeof(uint32_t) + device_id_size;
317 	uint32_t algorithm;
318 
319 	copy_uint32_from_buf(&key_buf, &algorithm);
320 	debug("\n algorithm:%d\n", algorithm);
321 
322 	/* read rsa private key */
323 	atap_blob key;
324 
325 	if (copy_blob_from_buf(&key_buf, &key) == false) {
326 		printf("copy_blob_from_buf failed!\n");
327 		return ATAP_RESULT_ERROR_BUF_COPY;
328 	}
329 	/* write rsa private key to security storage*/
330 	memcpy(key_name, ATTESTATION_KEY_PREFIX,
331 	       sizeof(ATTESTATION_KEY_PREFIX));
332 	write_key(KM_ALGORITHM_RSA, key_name, key.data, key.data_length);
333 
334 	/* read rsa cert chain */
335 	atap_certchain certchain;
336 
337 	if (copy_cert_chain_from_buf(&key_buf, &certchain) == false) {
338 		printf("copy_cert_chain_from_buf failed!\n");
339 		return ATAP_RESULT_ERROR_BUF_COPY;
340 	}
341 
342 	/* write rsa cert chain size to security storage*/
343 	write_cert_chain_length(KM_ALGORITHM_RSA,
344 				(uint8_t) certchain.entry_count);
345 
346 	/* write rsa cert chain data to security storage*/
347 	int i = 0;
348 
349 	for (i = 0; i < certchain.entry_count; ++i) {
350 		write_cert(KM_ALGORITHM_RSA, certchain.entries[i].data,
351 			certchain.entries[i].data_length, i);
352 	}
353 
354 	/* read ec algorithm */
355 	copy_uint32_from_buf(&key_buf, &algorithm);
356 	debug("\n algorithm:%d\n", algorithm);
357 
358 	/* read ec private key */
359 	free_blob(key);
360 	if (copy_blob_from_buf(&key_buf, &key) == false) {
361 		printf("copy_blob_from_buf failed!\n");
362 		return ATAP_RESULT_ERROR_BUF_COPY;
363 	}
364 
365 	/* write ec private key to security storage*/
366 	write_key(KM_ALGORITHM_EC, key_name, key.data, key.data_length);
367 
368 	/* read ec cert chain */
369 	free_cert_chain(certchain);
370 	if (copy_cert_chain_from_buf(&key_buf, &certchain) == false) {
371 		printf("copy_cert_chain_from_buf failed!\n");
372 		return ATAP_RESULT_ERROR_BUF_COPY;
373 	}
374 	/* write ec cert chain size to security storage*/
375 	write_cert_chain_length(KM_ALGORITHM_EC,
376 					(uint8_t) certchain.entry_count);
377 
378 	/* write ec cert chain to security storage*/
379 	for (i = 0; i < certchain.entry_count; ++i) {
380 		write_cert(KM_ALGORITHM_EC, certchain.entries[i].data,
381 			certchain.entries[i].data_length, i);
382 	}
383 
384 	memset(keybuf, 0, sizeof(keybuf));
385 
386 	/* wipe attestation data from misc*/
387 	if (blk_dwrite(dev_desc, key_offset, real_block_num, keybuf) != real_block_num) {
388 		printf("StorageWriteLba failed\n");
389 		return ATAP_RESULT_ERROR_BLOCK_WRITE;
390 	}
391 
392 	return ATAP_RESULT_OK;
393 }
394 
395 atap_result read_key_data(uint8_t **key_buf, uint8_t *key_data,
396 			  uint32_t *key_data_length)
397 {
398 	atap_blob key;
399 	atap_certchain certchain;
400 
401 	 /* read private key */
402 	if (copy_blob_from_buf(key_buf, &key) == false) {
403 		printf("copy_blob_from_buf failed!\n");
404 		return ATAP_RESULT_ERROR_BUF_COPY;
405 	}
406 	memcpy(key_data, &key.data_length, sizeof(uint32_t));
407 	memcpy(key_data + 4, key.data, key.data_length);
408 	*key_data_length = 4 + key.data_length;
409 
410 	/* read certchain */
411 	if (copy_cert_chain_from_buf(key_buf, &certchain) == false) {
412 		printf("copy_cert_chain_from_buf failed!\n");
413 		return ATAP_RESULT_ERROR_BUF_COPY;
414 	}
415 	memcpy(key_data + *key_data_length,
416 	       &certchain.entry_count, sizeof(uint32_t));
417 	*key_data_length += 4;
418 	for (int i = 0; i < certchain.entry_count; ++i) {
419 		memcpy(key_data + *key_data_length,
420 		       &certchain.entries[i].data_length, sizeof(uint32_t));
421 		*key_data_length += 4;
422 		memcpy(key_data + *key_data_length, certchain.entries[i].data,
423 		       certchain.entries[i].data_length);
424 		*key_data_length += certchain.entries[i].data_length;
425 	}
426 
427 	free_blob(key);
428 	free_cert_chain(certchain);
429 
430 	return 0;
431 }
432 
433 atap_result write_attestation_key_to_secure_storage(uint8_t *received_data,
434 						    uint32_t len)
435 {
436 	unsigned char keybuf[ATTESTATION_DATA_OFFSET] = {0};
437 	unsigned char key_name[STORAGE_ID_LENGTH_MAX] = {0};
438 	uint32_t device_id_size = 0;
439 	uint8_t device_id[32] = {0};
440 	uint8_t *key_buf = NULL;
441 	uint32_t algorithm;
442 	uint8_t *key_data;
443 	uint32_t key_data_length = 0;
444 
445 	/* skip the tag(4 byte) and the size of key(4 byte) */
446 	memcpy(keybuf, received_data + 8, ATTESTATION_DATA_OFFSET);
447 	key_data = malloc(ATTESTATION_DATA_OFFSET);
448 	/* read device id from keybuf */
449 	memcpy(&device_id_size, keybuf + CA_HEADER_LEN, sizeof(uint32_t));
450 	if (device_id_size < 0 || device_id_size > sizeof(device_id)) {
451 		printf("invalidate device_id_size:%d\n", device_id_size);
452 		return ATAP_RESULT_ERROR_INVALID_DEVICE_ID;
453 	}
454 	memcpy(device_id, keybuf + CA_HEADER_LEN + sizeof(uint32_t),
455 	       device_id_size);
456 	printf("device_id:%s\n", device_id);
457 
458 	memcpy(key_name, ATTESTATION_KEY_FILE, sizeof(ATTESTATION_KEY_FILE));
459 	/* read algorithm(RSA) from keybuf */
460 	key_buf = keybuf + CA_HEADER_LEN + sizeof(uint32_t) + device_id_size;
461 	copy_uint32_from_buf(&key_buf, &algorithm);
462 	printf("\n algorithm: %d\n", algorithm);
463 	/* read rsa key and certchain */
464 	read_key_data(&key_buf, key_data, &key_data_length);
465 	printf("write attestation key: RSA\n");
466 	write_key(KM_ALGORITHM_RSA, key_name, key_data, key_data_length);
467 
468 	/* read algorithm(EC) from keybuf */
469 	copy_uint32_from_buf(&key_buf, &algorithm);
470 	printf("\n algorithm: %d\n", algorithm);
471 	/* read ec key and certchain */
472 	read_key_data(&key_buf, key_data, &key_data_length);
473 	printf("write attestation key: EC\n");
474 	write_key(KM_ALGORITHM_EC, key_name, key_data, key_data_length);
475 
476 	memset(keybuf, 0, sizeof(keybuf));
477 	free(key_data);
478 
479 	return ATAP_RESULT_OK;
480 }
481