1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 // For more information on the Android Profile for DICE, see docs/android.md.
16
17 #include "dice/android.h"
18
19 #include <string.h>
20
21 #include "dice/cbor_reader.h"
22 #include "dice/cbor_writer.h"
23 #include "dice/dice.h"
24 #include "dice/ops.h"
25 #include "dice/ops/trait/cose.h"
26
27 // Completely gratuitous bit twiddling.
PopulationCount(uint32_t n)28 static size_t PopulationCount(uint32_t n) {
29 n = n - ((n >> 1) & 0x55555555);
30 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
31 return (((n + (n >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
32 }
33
DiceAndroidFormatConfigDescriptor(const DiceAndroidConfigValues * config_values,size_t buffer_size,uint8_t * buffer,size_t * actual_size)34 DiceResult DiceAndroidFormatConfigDescriptor(
35 const DiceAndroidConfigValues* config_values, size_t buffer_size,
36 uint8_t* buffer, size_t* actual_size) {
37 static const int64_t kComponentNameLabel = -70002;
38 static const int64_t kComponentVersionLabel = -70003;
39 static const int64_t kResettableLabel = -70004;
40 static const int64_t kSecurityVersionLabel = -70005;
41 static const int64_t kRkpVmMarkerLabel = -70006;
42
43 // AndroidConfigDescriptor = {
44 // ? -70002 : tstr, ; Component name
45 // ? -70003 : int, ; Component version
46 // ? -70004 : null, ; Resettable
47 // }
48 struct CborOut out;
49 CborOutInit(buffer, buffer_size, &out);
50 CborWriteMap(PopulationCount(config_values->configs), &out);
51 if (config_values->configs & DICE_ANDROID_CONFIG_COMPONENT_NAME &&
52 config_values->component_name) {
53 CborWriteInt(kComponentNameLabel, &out);
54 CborWriteTstr(config_values->component_name, &out);
55 }
56 if (config_values->configs & DICE_ANDROID_CONFIG_COMPONENT_VERSION) {
57 CborWriteInt(kComponentVersionLabel, &out);
58 CborWriteUint(config_values->component_version, &out);
59 }
60 if (config_values->configs & DICE_ANDROID_CONFIG_RESETTABLE) {
61 CborWriteInt(kResettableLabel, &out);
62 CborWriteNull(&out);
63 }
64 if (config_values->configs & DICE_ANDROID_CONFIG_SECURITY_VERSION) {
65 CborWriteInt(kSecurityVersionLabel, &out);
66 CborWriteUint(config_values->security_version, &out);
67 }
68 if (config_values->configs & DICE_ANDROID_CONFIG_RKP_VM_MARKER) {
69 CborWriteInt(kRkpVmMarkerLabel, &out);
70 CborWriteNull(&out);
71 }
72 *actual_size = CborOutSize(&out);
73 if (CborOutOverflowed(&out)) {
74 return kDiceResultBufferTooSmall;
75 }
76 return kDiceResultOk;
77 }
78
DiceAndroidMainFlow(void * context,const uint8_t current_cdi_attest[DICE_CDI_SIZE],const uint8_t current_cdi_seal[DICE_CDI_SIZE],const uint8_t * chain,size_t chain_size,const DiceInputValues * input_values,size_t buffer_size,uint8_t * buffer,size_t * actual_size,uint8_t next_cdi_attest[DICE_CDI_SIZE],uint8_t next_cdi_seal[DICE_CDI_SIZE])79 DiceResult DiceAndroidMainFlow(void* context,
80 const uint8_t current_cdi_attest[DICE_CDI_SIZE],
81 const uint8_t current_cdi_seal[DICE_CDI_SIZE],
82 const uint8_t* chain, size_t chain_size,
83 const DiceInputValues* input_values,
84 size_t buffer_size, uint8_t* buffer,
85 size_t* actual_size,
86 uint8_t next_cdi_attest[DICE_CDI_SIZE],
87 uint8_t next_cdi_seal[DICE_CDI_SIZE]) {
88 DiceResult result;
89 enum CborReadResult res;
90 struct CborIn in;
91 size_t chain_item_count;
92
93 // The Android DICE chain has a more detailed internal structure, but those
94 // details aren't relevant to the work of this function.
95 //
96 // DiceCertChain = [
97 // COSE_Key, ; Root public key
98 // + COSE_Sign1, ; DICE chain entries
99 // ]
100 CborInInit(chain, chain_size, &in);
101 res = CborReadArray(&in, &chain_item_count);
102 if (res != CBOR_READ_RESULT_OK) {
103 return kDiceResultInvalidInput;
104 }
105
106 if (chain_item_count < 2 || chain_item_count == SIZE_MAX) {
107 // There should at least be the public key and one entry.
108 return kDiceResultInvalidInput;
109 }
110
111 // Measure the existing chain entries.
112 size_t chain_items_offset = CborInOffset(&in);
113 for (size_t chain_pos = 0; chain_pos < chain_item_count; ++chain_pos) {
114 res = CborReadSkip(&in);
115 if (res != CBOR_READ_RESULT_OK) {
116 return kDiceResultInvalidInput;
117 }
118 }
119 size_t chain_items_size = CborInOffset(&in) - chain_items_offset;
120
121 // Copy to the new buffer, with space in the chain for one more entry.
122 struct CborOut out;
123 CborOutInit(buffer, buffer_size, &out);
124 CborWriteArray(chain_item_count + 1, &out);
125 size_t new_chain_prefix_size = CborOutSize(&out);
126 if (CborOutOverflowed(&out) ||
127 chain_items_size > buffer_size - new_chain_prefix_size) {
128 // Continue with an empty buffer to measure the required size.
129 buffer_size = 0;
130 } else {
131 memcpy(buffer + new_chain_prefix_size, chain + chain_items_offset,
132 chain_items_size);
133 buffer += new_chain_prefix_size + chain_items_size;
134 buffer_size -= new_chain_prefix_size + chain_items_size;
135 }
136
137 size_t certificate_size;
138 result = DiceMainFlow(context, current_cdi_attest, current_cdi_seal,
139 input_values, buffer_size, buffer, &certificate_size,
140 next_cdi_attest, next_cdi_seal);
141 *actual_size = new_chain_prefix_size + chain_items_size + certificate_size;
142 return result;
143 }
144
DiceAndroidMainFlowWithNewDiceChain(void * context,const uint8_t current_cdi_attest[DICE_CDI_SIZE],const uint8_t current_cdi_seal[DICE_CDI_SIZE],const DiceInputValues * input_values,size_t buffer_size,uint8_t * buffer,size_t * chain_size,uint8_t next_cdi_attest[DICE_CDI_SIZE],uint8_t next_cdi_seal[DICE_CDI_SIZE])145 static DiceResult DiceAndroidMainFlowWithNewDiceChain(
146 void* context, const uint8_t current_cdi_attest[DICE_CDI_SIZE],
147 const uint8_t current_cdi_seal[DICE_CDI_SIZE],
148 const DiceInputValues* input_values, size_t buffer_size, uint8_t* buffer,
149 size_t* chain_size, uint8_t next_cdi_attest[DICE_CDI_SIZE],
150 uint8_t next_cdi_seal[DICE_CDI_SIZE]) {
151 uint8_t current_cdi_private_key_seed[DICE_PRIVATE_KEY_SEED_SIZE];
152 uint8_t attestation_public_key[DICE_PUBLIC_KEY_SIZE];
153 uint8_t attestation_private_key[DICE_PRIVATE_KEY_SIZE];
154 // Derive an asymmetric private key seed from the current attestation CDI
155 // value.
156 DiceResult result = DiceDeriveCdiPrivateKeySeed(context, current_cdi_attest,
157 current_cdi_private_key_seed);
158 if (result != kDiceResultOk) {
159 goto out;
160 }
161 // Derive attestation key pair.
162 result = DiceKeypairFromSeed(context, current_cdi_private_key_seed,
163 attestation_public_key, attestation_private_key);
164 if (result != kDiceResultOk) {
165 goto out;
166 }
167
168 // Consruct the DICE chain from the attestation public key and the next CDI
169 // certificate.
170 struct CborOut out;
171 CborOutInit(buffer, buffer_size, &out);
172 CborWriteArray(2, &out);
173 size_t encoded_size_used = CborOutSize(&out);
174 if (CborOutOverflowed(&out)) {
175 // Continue with an empty buffer to measure the required size.
176 buffer_size = 0;
177 } else {
178 buffer += encoded_size_used;
179 buffer_size -= encoded_size_used;
180 }
181
182 size_t encoded_pub_key_size = 0;
183 result = DiceCoseEncodePublicKey(context, attestation_public_key, buffer_size,
184 buffer, &encoded_pub_key_size);
185 if (result == kDiceResultOk) {
186 buffer += encoded_pub_key_size;
187 buffer_size -= encoded_pub_key_size;
188 } else if (result == kDiceResultBufferTooSmall) {
189 // Continue with an empty buffer to measure the required size.
190 buffer_size = 0;
191 } else {
192 goto out;
193 }
194
195 result = DiceMainFlow(context, current_cdi_attest, current_cdi_seal,
196 input_values, buffer_size, buffer, chain_size,
197 next_cdi_attest, next_cdi_seal);
198 *chain_size += encoded_size_used + encoded_pub_key_size;
199 if (result != kDiceResultOk) {
200 return result;
201 }
202
203 out:
204 DiceClearMemory(context, sizeof(current_cdi_private_key_seed),
205 current_cdi_private_key_seed);
206 DiceClearMemory(context, sizeof(attestation_private_key),
207 attestation_private_key);
208
209 return result;
210 }
211
212 // AndroidDiceHandover = {
213 // 1 : bstr .size 32, ; CDI_Attest
214 // 2 : bstr .size 32, ; CDI_Seal
215 // ? 3 : DiceCertChain, ; Android DICE chain
216 // }
217 static const int64_t kCdiAttestLabel = 1;
218 static const int64_t kCdiSealLabel = 2;
219 static const int64_t kDiceChainLabel = 3;
220
DiceAndroidHandoverMainFlow(void * context,const uint8_t * handover,size_t handover_size,const DiceInputValues * input_values,size_t buffer_size,uint8_t * buffer,size_t * actual_size)221 DiceResult DiceAndroidHandoverMainFlow(void* context, const uint8_t* handover,
222 size_t handover_size,
223 const DiceInputValues* input_values,
224 size_t buffer_size, uint8_t* buffer,
225 size_t* actual_size) {
226 DiceResult result;
227 const uint8_t* current_cdi_attest;
228 const uint8_t* current_cdi_seal;
229 const uint8_t* chain;
230 size_t chain_size;
231
232 result =
233 DiceAndroidHandoverParse(handover, handover_size, ¤t_cdi_attest,
234 ¤t_cdi_seal, &chain, &chain_size);
235 if (result != kDiceResultOk) {
236 return kDiceResultInvalidInput;
237 }
238
239 // Write the new handover data.
240 struct CborOut out;
241 CborOutInit(buffer, buffer_size, &out);
242 CborWriteMap(/*num_pairs=*/3, &out);
243 CborWriteInt(kCdiAttestLabel, &out);
244 uint8_t* next_cdi_attest = CborAllocBstr(DICE_CDI_SIZE, &out);
245 CborWriteInt(kCdiSealLabel, &out);
246 uint8_t* next_cdi_seal = CborAllocBstr(DICE_CDI_SIZE, &out);
247 CborWriteInt(kDiceChainLabel, &out);
248
249 uint8_t ignored_cdi_attest[DICE_CDI_SIZE];
250 uint8_t ignored_cdi_seal[DICE_CDI_SIZE];
251 if (CborOutOverflowed(&out)) {
252 // Continue with an empty buffer and placeholders for the output CDIs to
253 // measure the required size.
254 buffer_size = 0;
255 next_cdi_attest = ignored_cdi_attest;
256 next_cdi_seal = ignored_cdi_seal;
257 } else {
258 buffer += CborOutSize(&out);
259 buffer_size -= CborOutSize(&out);
260 }
261
262 if (chain_size != 0) {
263 // If the DICE chain is present in the handover, append the next certificate
264 // to the existing DICE chain.
265 result = DiceAndroidMainFlow(context, current_cdi_attest, current_cdi_seal,
266 chain, chain_size, input_values, buffer_size,
267 buffer, &chain_size, next_cdi_attest,
268 next_cdi_seal);
269 } else {
270 // If DICE chain is not present in the handover, construct the DICE chain
271 // from the public key derived from the current CDI attest and the next CDI
272 // certificate.
273 result = DiceAndroidMainFlowWithNewDiceChain(
274 context, current_cdi_attest, current_cdi_seal, input_values,
275 buffer_size, buffer, &chain_size, next_cdi_attest, next_cdi_seal);
276 }
277 *actual_size = CborOutSize(&out) + chain_size;
278 return result;
279 }
280
DiceAndroidHandoverParse(const uint8_t * handover,size_t handover_size,const uint8_t ** cdi_attest,const uint8_t ** cdi_seal,const uint8_t ** chain,size_t * chain_size)281 DiceResult DiceAndroidHandoverParse(const uint8_t* handover,
282 size_t handover_size,
283 const uint8_t** cdi_attest,
284 const uint8_t** cdi_seal,
285 const uint8_t** chain, size_t* chain_size) {
286 // Extract details from the handover data.
287 struct CborIn in;
288 int64_t label;
289 size_t num_pairs;
290 size_t item_size;
291 CborInInit(handover, handover_size, &in);
292 if (CborReadMap(&in, &num_pairs) != CBOR_READ_RESULT_OK || num_pairs < 2 ||
293 // Read the attestation CDI.
294 CborReadInt(&in, &label) != CBOR_READ_RESULT_OK ||
295 label != kCdiAttestLabel ||
296 CborReadBstr(&in, &item_size, cdi_attest) != CBOR_READ_RESULT_OK ||
297 item_size != DICE_CDI_SIZE ||
298 // Read the sealing CDI.
299 CborReadInt(&in, &label) != CBOR_READ_RESULT_OK ||
300 label != kCdiSealLabel ||
301 CborReadBstr(&in, &item_size, cdi_seal) != CBOR_READ_RESULT_OK ||
302 item_size != DICE_CDI_SIZE) {
303 return kDiceResultInvalidInput;
304 }
305
306 *chain = NULL;
307 *chain_size = 0;
308 if (num_pairs >= 3 && CborReadInt(&in, &label) == CBOR_READ_RESULT_OK) {
309 if (label == kDiceChainLabel) {
310 // Calculate the DICE chain size, if it is present in the handover object.
311 size_t chain_start = CborInOffset(&in);
312 if (CborReadSkip(&in) != CBOR_READ_RESULT_OK) {
313 return kDiceResultInvalidInput;
314 }
315 *chain = handover + chain_start;
316 *chain_size = CborInOffset(&in) - chain_start;
317 }
318 }
319
320 return kDiceResultOk;
321 }
322