1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <keymaster/UniquePtr.h>
18
19 #include <gtest/gtest.h>
20
21 #include <keymaster/android_keymaster.h>
22 #include <keymaster/android_keymaster_utils.h>
23 #include <keymaster/keymaster_tags.h>
24
25 #include "android_keymaster_test_utils.h"
26 #include "fuzzers/serializable_types.h"
27
28 namespace keymaster {
29 namespace test {
30
31 /**
32 * Serialize and deserialize a message.
33 */
34 template <typename Message>
round_trip(int32_t ver,const Message & message,size_t expected_size)35 Message* round_trip(int32_t ver, const Message& message, size_t expected_size) {
36 size_t size = message.SerializedSize();
37 EXPECT_EQ(expected_size, size);
38 if (size == 0) return nullptr;
39
40 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
41 EXPECT_EQ(buf.get() + size, message.Serialize(buf.get(), buf.get() + size));
42
43 Message* deserialized = new Message(ver);
44 const uint8_t* p = buf.get();
45 EXPECT_TRUE(deserialized->Deserialize(&p, p + size));
46 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
47 return deserialized;
48 }
49
TEST(RoundTrip,EmptyKeymasterResponse)50 TEST(RoundTrip, EmptyKeymasterResponse) {
51 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
52 EmptyKeymasterResponse msg(ver);
53 msg.error = KM_ERROR_OK;
54
55 UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(ver, msg, 4));
56 }
57 }
58
TEST(RoundTrip,EmptyKeymasterResponseError)59 TEST(RoundTrip, EmptyKeymasterResponseError) {
60 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
61 EmptyKeymasterResponse msg(ver);
62 msg.error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
63
64 UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(ver, msg, 4));
65 }
66 }
67
68 static keymaster_key_param_t params[] = {
69 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
70 Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
71 Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA),
72 Authorization(TAG_USER_ID, 7),
73 Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD),
74 Authorization(TAG_APPLICATION_ID, "app_id", 6),
75 Authorization(TAG_AUTH_TIMEOUT, 300),
76 };
77 uint8_t TEST_DATA[] = "a key blob";
78
TEST(RoundTrip,GenerateKeyRequest)79 TEST(RoundTrip, GenerateKeyRequest) {
80 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
81 GenerateKeyRequest req(ver);
82 req.key_description.Reinitialize(params, array_length(params));
83 req.attestation_signing_key_blob =
84 KeymasterKeyBlob(reinterpret_cast<const uint8_t*>("foo"), 3);
85 req.attest_key_params.Reinitialize(params, array_length(params));
86 req.issuer_subject = KeymasterBlob(reinterpret_cast<const uint8_t*>("bar"), 3);
87
88 UniquePtr<GenerateKeyRequest> deserialized(round_trip(ver, req, ver < 4 ? 78 : 170));
89 EXPECT_EQ(deserialized->key_description, req.key_description);
90 if (ver < 4) {
91 EXPECT_EQ(0U, deserialized->attestation_signing_key_blob.key_material_size);
92 } else {
93 EXPECT_EQ(3U, deserialized->attestation_signing_key_blob.key_material_size);
94 EXPECT_EQ(0, memcmp(req.attestation_signing_key_blob.key_material,
95 deserialized->attestation_signing_key_blob.key_material,
96 deserialized->attestation_signing_key_blob.key_material_size));
97 EXPECT_EQ(deserialized->attest_key_params, req.attest_key_params);
98 EXPECT_EQ(0, memcmp(req.issuer_subject.data, deserialized->issuer_subject.data,
99 deserialized->issuer_subject.data_length));
100 }
101 }
102 }
103
TEST(RoundTrip,GenerateKeyResponse)104 TEST(RoundTrip, GenerateKeyResponse) {
105 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
106 GenerateKeyResponse rsp(ver);
107 rsp.error = KM_ERROR_OK;
108 rsp.key_blob.key_material = dup_array(TEST_DATA);
109 rsp.key_blob.key_material_size = array_length(TEST_DATA);
110 rsp.enforced.Reinitialize(params, array_length(params));
111
112 rsp.certificate_chain = CertificateChain(3);
113 rsp.certificate_chain.entries[0] = {dup_buffer("foo", 3), 3};
114 rsp.certificate_chain.entries[1] = {dup_buffer("bar", 3), 3};
115 rsp.certificate_chain.entries[2] = {dup_buffer("baz", 3), 3};
116
117 UniquePtr<GenerateKeyResponse> deserialized;
118 if (ver < 4) {
119 deserialized.reset(round_trip(ver, rsp, 109));
120 } else {
121 deserialized.reset(round_trip(ver, rsp, 134));
122 }
123
124 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
125 EXPECT_EQ(deserialized->enforced, rsp.enforced);
126 EXPECT_EQ(deserialized->unenforced, rsp.unenforced);
127
128 keymaster_cert_chain_t* chain = &deserialized->certificate_chain;
129 if (ver < 4) {
130 EXPECT_EQ(nullptr, chain->entries);
131 } else {
132 EXPECT_NE(nullptr, chain->entries);
133 EXPECT_EQ(3U, chain->entry_count);
134 EXPECT_EQ(3U, chain->entries[0].data_length);
135 EXPECT_EQ(0, memcmp("foo", chain->entries[0].data, 3));
136 EXPECT_EQ(3U, chain->entries[1].data_length);
137 EXPECT_EQ(0, memcmp("bar", chain->entries[1].data, 3));
138 EXPECT_EQ(3U, chain->entries[2].data_length);
139 EXPECT_EQ(0, memcmp("baz", chain->entries[2].data, 3));
140 }
141 }
142 }
143
TEST(RoundTrip,GenerateKeyResponseTestError)144 TEST(RoundTrip, GenerateKeyResponseTestError) {
145 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
146 GenerateKeyResponse rsp(ver);
147 rsp.error = KM_ERROR_UNSUPPORTED_ALGORITHM;
148 rsp.key_blob.key_material = dup_array(TEST_DATA);
149 rsp.key_blob.key_material_size = array_length(TEST_DATA);
150 rsp.enforced.Reinitialize(params, array_length(params));
151
152 UniquePtr<GenerateKeyResponse> deserialized(round_trip(ver, rsp, 4));
153 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, deserialized->error);
154 EXPECT_EQ(0U, deserialized->enforced.size());
155 EXPECT_EQ(0U, deserialized->unenforced.size());
156 EXPECT_EQ(0U, deserialized->key_blob.key_material_size);
157 }
158 }
159
TEST(RoundTrip,GenerateRkpKeyRequest)160 TEST(RoundTrip, GenerateRkpKeyRequest) {
161 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
162 GenerateRkpKeyRequest req(ver);
163 req.test_mode = true;
164
165 UniquePtr<GenerateRkpKeyRequest> deserialized(round_trip(ver, req, 1));
166 EXPECT_EQ(deserialized->test_mode, req.test_mode);
167 }
168 }
169
TEST(RoundTrip,GenerateRkpKeyResponse)170 TEST(RoundTrip, GenerateRkpKeyResponse) {
171 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
172 GenerateRkpKeyResponse rsp(ver);
173 rsp.error = KM_ERROR_OK;
174 rsp.key_blob.key_material = dup_array(TEST_DATA);
175 rsp.key_blob.key_material_size = array_length(TEST_DATA);
176 rsp.maced_public_key.data = dup_array(TEST_DATA);
177 rsp.maced_public_key.data_length = array_length(TEST_DATA);
178
179 UniquePtr<GenerateRkpKeyResponse> deserialized;
180 deserialized.reset(round_trip(ver, rsp, 34));
181
182 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
183 EXPECT_EQ(deserialized->key_blob.key_material_size, rsp.key_blob.key_material_size);
184 EXPECT_EQ(0, std::memcmp(deserialized->key_blob.key_material, rsp.key_blob.key_material,
185 deserialized->key_blob.key_material_size));
186 EXPECT_EQ(deserialized->maced_public_key.data_length, rsp.maced_public_key.data_length);
187 EXPECT_EQ(0, std::memcmp(deserialized->maced_public_key.data, rsp.maced_public_key.data,
188 deserialized->maced_public_key.data_length));
189 }
190 }
191
TEST(RoundTrip,GenerateCsrRequest)192 TEST(RoundTrip, GenerateCsrRequest) {
193 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
194 GenerateCsrRequest req(ver);
195 req.test_mode = true;
196 req.num_keys = 2;
197 req.keys_to_sign_array = new KeymasterBlob[req.num_keys];
198 for (size_t i = 0; i < req.num_keys; i++) {
199 req.SetKeyToSign(i, dup_array(TEST_DATA), array_length(TEST_DATA));
200 }
201 req.SetEndpointEncCertChain(dup_array(TEST_DATA), array_length(TEST_DATA));
202 req.SetChallenge(dup_array(TEST_DATA), array_length(TEST_DATA));
203 UniquePtr<GenerateCsrRequest> deserialized(round_trip(ver, req, 65));
204 EXPECT_EQ(deserialized->test_mode, req.test_mode);
205 EXPECT_EQ(deserialized->num_keys, req.num_keys);
206 for (int i = 0; i < (int)req.num_keys; i++) {
207 EXPECT_EQ(deserialized->keys_to_sign_array[i].data_length,
208 req.keys_to_sign_array[i].data_length);
209 EXPECT_EQ(0, std::memcmp(deserialized->keys_to_sign_array[i].data,
210 req.keys_to_sign_array[i].data,
211 req.keys_to_sign_array[i].data_length));
212 }
213 EXPECT_EQ(deserialized->endpoint_enc_cert_chain.data_length,
214 req.endpoint_enc_cert_chain.data_length);
215 EXPECT_EQ(0, std::memcmp(deserialized->endpoint_enc_cert_chain.data,
216 req.endpoint_enc_cert_chain.data,
217 req.endpoint_enc_cert_chain.data_length));
218 EXPECT_EQ(deserialized->challenge.data_length, req.challenge.data_length);
219 EXPECT_EQ(0, std::memcmp(deserialized->challenge.data, req.challenge.data,
220 req.challenge.data_length));
221 }
222 }
223
TEST(RoundTrip,GenerateCsrResponse)224 TEST(RoundTrip, GenerateCsrResponse) {
225 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
226 GenerateCsrResponse rsp(ver);
227 rsp.error = KM_ERROR_OK;
228 rsp.keys_to_sign_mac.data = dup_array(TEST_DATA);
229 rsp.keys_to_sign_mac.data_length = array_length(TEST_DATA);
230 rsp.device_info_blob.data = dup_array(TEST_DATA);
231 rsp.device_info_blob.data_length = array_length(TEST_DATA);
232 rsp.protected_data_blob.data = dup_array(TEST_DATA);
233 rsp.protected_data_blob.data_length = array_length(TEST_DATA);
234
235 UniquePtr<GenerateCsrResponse> deserialized;
236 deserialized.reset(round_trip(ver, rsp, 49));
237
238 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
239 EXPECT_EQ(deserialized->keys_to_sign_mac.data_length, rsp.keys_to_sign_mac.data_length);
240 EXPECT_EQ(0, std::memcmp(deserialized->keys_to_sign_mac.data, rsp.keys_to_sign_mac.data,
241 deserialized->keys_to_sign_mac.data_length));
242 EXPECT_EQ(deserialized->device_info_blob.data_length, rsp.device_info_blob.data_length);
243 EXPECT_EQ(0, std::memcmp(deserialized->device_info_blob.data, rsp.device_info_blob.data,
244 deserialized->device_info_blob.data_length));
245 EXPECT_EQ(deserialized->protected_data_blob.data_length,
246 rsp.protected_data_blob.data_length);
247 EXPECT_EQ(0,
248 std::memcmp(deserialized->protected_data_blob.data, rsp.protected_data_blob.data,
249 deserialized->protected_data_blob.data_length));
250 }
251 }
252
TEST(RoundTrip,GenerateCsrV2Request)253 TEST(RoundTrip, GenerateCsrV2Request) {
254 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
255 GenerateCsrV2Request req(ver);
256 EXPECT_TRUE(req.InitKeysToSign(2));
257 for (size_t i = 0; i < req.num_keys; i++) {
258 req.SetKeyToSign(i, dup_array(TEST_DATA), array_length(TEST_DATA));
259 }
260 req.SetChallenge(dup_array(TEST_DATA), array_length(TEST_DATA));
261 UniquePtr<GenerateCsrV2Request> deserialized(round_trip(ver, req, 49));
262 EXPECT_EQ(deserialized->num_keys, req.num_keys);
263 for (int i = 0; i < (int)req.num_keys; i++) {
264 EXPECT_EQ(deserialized->keys_to_sign_array[i].data_length,
265 req.keys_to_sign_array[i].data_length);
266 EXPECT_EQ(0, std::memcmp(deserialized->keys_to_sign_array[i].data,
267 req.keys_to_sign_array[i].data,
268 req.keys_to_sign_array[i].data_length));
269 }
270 EXPECT_EQ(deserialized->challenge.data_length, req.challenge.data_length);
271 EXPECT_EQ(0, std::memcmp(deserialized->challenge.data, req.challenge.data,
272 req.challenge.data_length));
273 }
274 }
275
TEST(RoundTrip,GenerateCsrV2Response)276 TEST(RoundTrip, GenerateCsrV2Response) {
277 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
278 GenerateCsrV2Response rsp(ver);
279 rsp.error = KM_ERROR_OK;
280 rsp.csr.data = dup_array(TEST_DATA);
281 rsp.csr.data_length = array_length(TEST_DATA);
282
283 UniquePtr<GenerateCsrV2Response> deserialized;
284 deserialized.reset(round_trip(ver, rsp, 19));
285
286 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
287 EXPECT_EQ(deserialized->csr.data_length, rsp.csr.data_length);
288 EXPECT_EQ(0,
289 std::memcmp(deserialized->csr.data, rsp.csr.data, deserialized->csr.data_length));
290 }
291 }
292
TEST(RoundTrip,GetKeyCharacteristicsRequest)293 TEST(RoundTrip, GetKeyCharacteristicsRequest) {
294 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
295 GetKeyCharacteristicsRequest req(ver);
296 req.additional_params.Reinitialize(params, array_length(params));
297 req.SetKeyMaterial("foo", 3);
298
299 UniquePtr<GetKeyCharacteristicsRequest> deserialized(round_trip(ver, req, 85));
300 EXPECT_EQ(7U, deserialized->additional_params.size());
301 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
302 EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
303 }
304 }
305
TEST(RoundTrip,GetKeyCharacteristicsResponse)306 TEST(RoundTrip, GetKeyCharacteristicsResponse) {
307 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
308 GetKeyCharacteristicsResponse msg(ver);
309 msg.error = KM_ERROR_OK;
310 msg.enforced.Reinitialize(params, array_length(params));
311 msg.unenforced.Reinitialize(params, array_length(params));
312
313 UniquePtr<GetKeyCharacteristicsResponse> deserialized(round_trip(ver, msg, 160));
314 EXPECT_EQ(msg.enforced, deserialized->enforced);
315 EXPECT_EQ(msg.unenforced, deserialized->unenforced);
316 }
317 }
318
TEST(RoundTrip,BeginOperationRequest)319 TEST(RoundTrip, BeginOperationRequest) {
320 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
321 BeginOperationRequest msg(ver);
322 msg.purpose = KM_PURPOSE_SIGN;
323 msg.SetKeyMaterial("foo", 3);
324 msg.additional_params.Reinitialize(params, array_length(params));
325
326 UniquePtr<BeginOperationRequest> deserialized(round_trip(ver, msg, 89));
327 EXPECT_EQ(KM_PURPOSE_SIGN, deserialized->purpose);
328 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
329 EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
330 EXPECT_EQ(msg.additional_params, deserialized->additional_params);
331 }
332 }
333
TEST(RoundTrip,BeginOperationResponse)334 TEST(RoundTrip, BeginOperationResponse) {
335 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
336 BeginOperationResponse msg(ver);
337 msg.error = KM_ERROR_OK;
338 msg.op_handle = 0xDEADBEEF;
339 msg.output_params.push_back(Authorization(TAG_NONCE, "foo", 3));
340
341 UniquePtr<BeginOperationResponse> deserialized;
342 switch (ver) {
343 case 0:
344 deserialized.reset(round_trip(ver, msg, 12));
345 break;
346 case 1:
347 case 2:
348 case 3:
349 case 4:
350 deserialized.reset(round_trip(ver, msg, 39));
351 break;
352 default:
353 FAIL();
354 }
355
356 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
357 EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
358
359 switch (ver) {
360 case 0:
361 EXPECT_EQ(0U, deserialized->output_params.size());
362 break;
363 case 1:
364 case 2:
365 case 3:
366 case 4:
367 EXPECT_EQ(msg.output_params, deserialized->output_params);
368 break;
369 default:
370 FAIL();
371 }
372 }
373 }
374
TEST(RoundTrip,BeginOperationResponseError)375 TEST(RoundTrip, BeginOperationResponseError) {
376 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
377 BeginOperationResponse msg(ver);
378 msg.error = KM_ERROR_INVALID_OPERATION_HANDLE;
379 msg.op_handle = 0xDEADBEEF;
380
381 UniquePtr<BeginOperationResponse> deserialized(round_trip(ver, msg, 4));
382 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, deserialized->error);
383 }
384 }
385
TEST(RoundTrip,UpdateOperationRequest)386 TEST(RoundTrip, UpdateOperationRequest) {
387 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
388 UpdateOperationRequest msg(ver);
389 msg.op_handle = 0xDEADBEEF;
390 msg.input.Reinitialize("foo", 3);
391
392 UniquePtr<UpdateOperationRequest> deserialized;
393 switch (ver) {
394 case 0:
395 deserialized.reset(round_trip(ver, msg, 15));
396 break;
397 case 1:
398 case 2:
399 case 3:
400 case 4:
401 deserialized.reset(round_trip(ver, msg, 27));
402 break;
403 default:
404 FAIL();
405 }
406 EXPECT_EQ(3U, deserialized->input.available_read());
407 EXPECT_EQ(0, memcmp(deserialized->input.peek_read(), "foo", 3));
408 }
409 }
410
TEST(RoundTrip,UpdateOperationResponse)411 TEST(RoundTrip, UpdateOperationResponse) {
412 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
413 UpdateOperationResponse msg(ver);
414 msg.error = KM_ERROR_OK;
415 msg.output.Reinitialize("foo", 3);
416 msg.input_consumed = 99;
417 msg.output_params.push_back(TAG_APPLICATION_ID, "bar", 3);
418
419 UniquePtr<UpdateOperationResponse> deserialized;
420 switch (ver) {
421 case 0:
422 deserialized.reset(round_trip(ver, msg, 11));
423 break;
424 case 1:
425 deserialized.reset(round_trip(ver, msg, 15));
426 break;
427 case 2:
428 case 3:
429 case 4:
430 deserialized.reset(round_trip(ver, msg, 42));
431 break;
432 default:
433 FAIL();
434 }
435 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
436 EXPECT_EQ(3U, deserialized->output.available_read());
437 EXPECT_EQ(0, memcmp(deserialized->output.peek_read(), "foo", 3));
438
439 switch (ver) {
440 case 0:
441 EXPECT_EQ(0U, deserialized->input_consumed);
442 break;
443 case 1:
444 EXPECT_EQ(99U, deserialized->input_consumed);
445 break;
446 case 2:
447 case 3:
448 case 4:
449 EXPECT_EQ(99U, deserialized->input_consumed);
450 EXPECT_EQ(1U, deserialized->output_params.size());
451 break;
452 default:
453 FAIL();
454 }
455 }
456 }
457
TEST(RoundTrip,FinishOperationRequest)458 TEST(RoundTrip, FinishOperationRequest) {
459 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
460 FinishOperationRequest msg(ver);
461 msg.op_handle = 0xDEADBEEF;
462 msg.signature.Reinitialize("bar", 3);
463 msg.input.Reinitialize("baz", 3);
464
465 UniquePtr<FinishOperationRequest> deserialized;
466 switch (ver) {
467 case 0:
468 deserialized.reset(round_trip(ver, msg, 15));
469 break;
470 case 1:
471 case 2:
472 deserialized.reset(round_trip(ver, msg, 27));
473 break;
474 case 3:
475 case 4:
476 deserialized.reset(round_trip(ver, msg, 34));
477 break;
478 default:
479 FAIL();
480 }
481 EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
482 EXPECT_EQ(3U, deserialized->signature.available_read());
483 EXPECT_EQ(0, memcmp(deserialized->signature.peek_read(), "bar", 3));
484 }
485 }
486
TEST(Round_Trip,FinishOperationResponse)487 TEST(Round_Trip, FinishOperationResponse) {
488 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
489 FinishOperationResponse msg(ver);
490 msg.error = KM_ERROR_OK;
491 msg.output.Reinitialize("foo", 3);
492
493 UniquePtr<FinishOperationResponse> deserialized;
494 switch (ver) {
495 case 0:
496 case 1:
497 deserialized.reset(round_trip(ver, msg, 11));
498 break;
499 case 2:
500 case 3:
501 case 4:
502 deserialized.reset(round_trip(ver, msg, 23));
503 break;
504 default:
505 FAIL();
506 }
507 EXPECT_EQ(msg.error, deserialized->error);
508 EXPECT_EQ(msg.output.available_read(), deserialized->output.available_read());
509 EXPECT_EQ(0, memcmp(msg.output.peek_read(), deserialized->output.peek_read(),
510 msg.output.available_read()));
511 }
512 }
513
TEST(RoundTrip,ImportKeyRequest)514 TEST(RoundTrip, ImportKeyRequest) {
515 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
516 ImportKeyRequest msg(ver);
517 msg.key_description.Reinitialize(params, array_length(params));
518 msg.key_format = KM_KEY_FORMAT_X509;
519 msg.key_data = KeymasterKeyBlob(reinterpret_cast<const uint8_t*>("foo"), 3);
520 msg.attestation_signing_key_blob =
521 KeymasterKeyBlob(reinterpret_cast<const uint8_t*>("bar"), 3);
522 msg.attest_key_params.Reinitialize(params, array_length(params));
523 msg.issuer_subject = KeymasterBlob(reinterpret_cast<const uint8_t*>("bar"), 3);
524
525 UniquePtr<ImportKeyRequest> deserialized(round_trip(ver, msg, ver < 4 ? 89 : 181));
526 EXPECT_EQ(msg.key_description, deserialized->key_description);
527 EXPECT_EQ(msg.key_format, deserialized->key_format);
528 EXPECT_EQ(msg.key_data.key_material_size, deserialized->key_data.key_material_size);
529 EXPECT_EQ(0, memcmp(msg.key_data.key_material, deserialized->key_data.key_material,
530 msg.key_data.key_material_size));
531 if (ver < 4) {
532 EXPECT_EQ(0U, deserialized->attestation_signing_key_blob.key_material_size);
533 } else {
534 EXPECT_EQ(3U, deserialized->attestation_signing_key_blob.key_material_size);
535 EXPECT_EQ(0, memcmp(msg.attestation_signing_key_blob.key_material,
536 deserialized->attestation_signing_key_blob.key_material,
537 msg.attestation_signing_key_blob.key_material_size));
538 EXPECT_EQ(deserialized->attest_key_params, msg.attest_key_params);
539 EXPECT_EQ(0, memcmp(msg.issuer_subject.data, deserialized->issuer_subject.data,
540 deserialized->issuer_subject.data_length));
541 }
542 }
543 }
544
TEST(RoundTrip,ImportKeyResponse)545 TEST(RoundTrip, ImportKeyResponse) {
546 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
547 ImportKeyResponse msg(ver);
548 msg.error = KM_ERROR_OK;
549 msg.SetKeyMaterial("foo", 3);
550 msg.enforced.Reinitialize(params, array_length(params));
551 msg.unenforced.Reinitialize(params, array_length(params));
552
553 msg.certificate_chain = CertificateChain(3);
554 msg.certificate_chain.entries[0] = {dup_buffer("foo", 3), 3};
555 msg.certificate_chain.entries[1] = {dup_buffer("bar", 3), 3};
556 msg.certificate_chain.entries[2] = {dup_buffer("baz", 3), 3};
557
558 UniquePtr<ImportKeyResponse> deserialized;
559 if (ver < 4) {
560 deserialized.reset(round_trip(ver, msg, 167));
561 } else {
562 deserialized.reset(round_trip(ver, msg, 192));
563 }
564
565 EXPECT_EQ(msg.error, deserialized->error);
566 EXPECT_EQ(msg.key_blob.key_material_size, deserialized->key_blob.key_material_size);
567 EXPECT_EQ(0, memcmp(msg.key_blob.key_material, deserialized->key_blob.key_material,
568 msg.key_blob.key_material_size));
569 EXPECT_EQ(msg.enforced, deserialized->enforced);
570 EXPECT_EQ(msg.unenforced, deserialized->unenforced);
571
572 keymaster_cert_chain_t* chain = &deserialized->certificate_chain;
573 if (ver < 4) {
574 EXPECT_EQ(nullptr, chain->entries);
575 } else {
576 EXPECT_NE(nullptr, chain->entries);
577 EXPECT_EQ(3U, chain->entry_count);
578 EXPECT_EQ(3U, chain->entries[0].data_length);
579 EXPECT_EQ(0, memcmp("foo", chain->entries[0].data, 3));
580 EXPECT_EQ(3U, chain->entries[1].data_length);
581 EXPECT_EQ(0, memcmp("bar", chain->entries[1].data, 3));
582 EXPECT_EQ(3U, chain->entries[2].data_length);
583 EXPECT_EQ(0, memcmp("baz", chain->entries[2].data, 3));
584 }
585 }
586 }
587
TEST(RoundTrip,ExportKeyRequest)588 TEST(RoundTrip, ExportKeyRequest) {
589 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
590 ExportKeyRequest msg(ver);
591 msg.additional_params.Reinitialize(params, array_length(params));
592 msg.key_format = KM_KEY_FORMAT_X509;
593 msg.SetKeyMaterial("foo", 3);
594
595 UniquePtr<ExportKeyRequest> deserialized(round_trip(ver, msg, 89));
596 EXPECT_EQ(msg.additional_params, deserialized->additional_params);
597 EXPECT_EQ(msg.key_format, deserialized->key_format);
598 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
599 EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
600 }
601 }
602
TEST(RoundTrip,ExportKeyResponse)603 TEST(RoundTrip, ExportKeyResponse) {
604 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
605 ExportKeyResponse msg(ver);
606 msg.error = KM_ERROR_OK;
607 msg.SetKeyMaterial("foo", 3);
608
609 UniquePtr<ExportKeyResponse> deserialized(round_trip(ver, msg, 11));
610 EXPECT_EQ(3U, deserialized->key_data_length);
611 EXPECT_EQ(0, memcmp("foo", deserialized->key_data, 3));
612 }
613 }
614
TEST(RoundTrip,DeleteKeyRequest)615 TEST(RoundTrip, DeleteKeyRequest) {
616 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
617 DeleteKeyRequest msg(ver);
618 msg.SetKeyMaterial("foo", 3);
619
620 UniquePtr<DeleteKeyRequest> deserialized(round_trip(ver, msg, 7));
621 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
622 EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
623 }
624 }
625
TEST(RoundTrip,DeleteAllKeysRequest)626 TEST(RoundTrip, DeleteAllKeysRequest) {
627 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
628 DeleteAllKeysRequest msg(ver);
629 UniquePtr<DeleteAllKeysRequest> deserialized(round_trip(ver, msg, 0));
630 }
631 }
632
TEST(RoundTrip,GetVersionRequest)633 TEST(RoundTrip, GetVersionRequest) {
634 GetVersionRequest msg(0);
635
636 size_t size = msg.SerializedSize();
637 ASSERT_EQ(0U, size);
638
639 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
640 EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
641
642 GetVersionRequest deserialized;
643 const uint8_t* p = buf.get();
644 EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
645 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
646 }
647
TEST(RoundTrip,GetVersionResponse)648 TEST(RoundTrip, GetVersionResponse) {
649 GetVersionResponse msg(0);
650 msg.error = KM_ERROR_OK;
651 msg.major_ver = 9;
652 msg.minor_ver = 98;
653 msg.subminor_ver = 38;
654
655 size_t size = msg.SerializedSize();
656 ASSERT_EQ(7U, size);
657
658 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
659 EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
660
661 GetVersionResponse deserialized;
662 const uint8_t* p = buf.get();
663 EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
664 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
665 EXPECT_EQ(9U, msg.major_ver);
666 EXPECT_EQ(98U, msg.minor_ver);
667 EXPECT_EQ(38U, msg.subminor_ver);
668 }
669
TEST(RoundTrip,GetVersion2Request)670 TEST(RoundTrip, GetVersion2Request) {
671 GetVersion2Request msg;
672
673 msg.max_message_version = 0xDEADBEEF;
674 size_t size = msg.SerializedSize();
675 ASSERT_EQ(4U, size);
676
677 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
678 EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
679
680 GetVersion2Request deserialized;
681 const uint8_t* p = buf.get();
682 EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
683 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
684 EXPECT_EQ(0xDEADBEEF, msg.max_message_version);
685 }
686
TEST(RoundTrip,GetVersion2Response)687 TEST(RoundTrip, GetVersion2Response) {
688 GetVersion2Response msg;
689 msg.error = KM_ERROR_OK;
690 msg.km_version = KmVersion::KEYMINT_1;
691 msg.km_date = 20121900;
692
693 size_t size = msg.SerializedSize();
694 ASSERT_EQ(16U, size);
695
696 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
697 EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
698
699 GetVersion2Response deserialized;
700 const uint8_t* p = buf.get();
701 EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
702 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
703 EXPECT_EQ(KmVersion::KEYMINT_1, msg.km_version);
704 EXPECT_EQ(20121900U, msg.km_date);
705 }
706
TEST(RoundTrip,ConfigureRequest)707 TEST(RoundTrip, ConfigureRequest) {
708 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
709 ConfigureRequest req(ver);
710 req.os_version = 1;
711 req.os_patchlevel = 1;
712
713 UniquePtr<ConfigureRequest> deserialized(round_trip(ver, req, 8));
714 EXPECT_EQ(deserialized->os_version, req.os_version);
715 EXPECT_EQ(deserialized->os_patchlevel, req.os_patchlevel);
716 }
717 }
718
TEST(RoundTrip,ConfigureResponse)719 TEST(RoundTrip, ConfigureResponse) {
720 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
721 ConfigureResponse rsp(ver);
722 UniquePtr<ConfigureResponse> deserialized(round_trip(ver, rsp, 4));
723 }
724 }
725
TEST(RoundTrip,ConfigureVendorPatchlevelRequest)726 TEST(RoundTrip, ConfigureVendorPatchlevelRequest) {
727 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
728 ConfigureVendorPatchlevelRequest req(ver);
729 req.vendor_patchlevel = 2;
730
731 UniquePtr<ConfigureVendorPatchlevelRequest> deserialized(round_trip(ver, req, 4));
732 EXPECT_EQ(deserialized->vendor_patchlevel, req.vendor_patchlevel);
733 }
734 }
735
TEST(RoundTrip,ConfigureVendorPatchlevelResponse)736 TEST(RoundTrip, ConfigureVendorPatchlevelResponse) {
737 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
738 ConfigureVendorPatchlevelResponse rsp(ver);
739 UniquePtr<ConfigureVendorPatchlevelResponse> deserialized(round_trip(ver, rsp, 4));
740 }
741 }
742
TEST(RoundTrip,ConfigureBootPatchlevelRequest)743 TEST(RoundTrip, ConfigureBootPatchlevelRequest) {
744 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
745 ConfigureBootPatchlevelRequest req(ver);
746 req.boot_patchlevel = 2;
747
748 UniquePtr<ConfigureBootPatchlevelRequest> deserialized(round_trip(ver, req, 4));
749 EXPECT_EQ(deserialized->boot_patchlevel, req.boot_patchlevel);
750 }
751 }
752
TEST(RoundTrip,ConfigureBootPatchlevelResponse)753 TEST(RoundTrip, ConfigureBootPatchlevelResponse) {
754 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
755 ConfigureBootPatchlevelResponse rsp(ver);
756 UniquePtr<ConfigureBootPatchlevelResponse> deserialized(round_trip(ver, rsp, 4));
757 }
758 }
759
TEST(RoundTrip,ConfigureVerifiedBootInfoRequest)760 TEST(RoundTrip, ConfigureVerifiedBootInfoRequest) {
761 for (int32_t ver = 0; ver <= kMaxMessageVersion; ++ver) {
762 ConfigureVerifiedBootInfoRequest req(ver, "super", "duper", {1, 2, 3, 4, 5, 6});
763
764 UniquePtr<ConfigureVerifiedBootInfoRequest> deserialized(round_trip(ver, req, 28));
765 ASSERT_NE(deserialized, nullptr);
766 EXPECT_EQ(deserialized->boot_state, req.boot_state);
767 EXPECT_EQ(deserialized->bootloader_state, req.bootloader_state);
768 EXPECT_EQ(deserialized->vbmeta_digest, req.vbmeta_digest);
769 }
770 }
771
TEST(RoundTrip,ConfigureVerifiedBootInfoResponse)772 TEST(RoundTrip, ConfigureVerifiedBootInfoResponse) {
773 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
774 ConfigureVerifiedBootInfoResponse rsp(ver);
775 UniquePtr<ConfigureVerifiedBootInfoResponse> deserialized(round_trip(ver, rsp, 4));
776 }
777 }
778
TEST(RoundTrip,AddEntropyRequest)779 TEST(RoundTrip, AddEntropyRequest) {
780 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
781 AddEntropyRequest msg(ver);
782 msg.random_data.Reinitialize("foo", 3);
783
784 UniquePtr<AddEntropyRequest> deserialized(round_trip(ver, msg, 7));
785 EXPECT_EQ(3U, deserialized->random_data.available_read());
786 EXPECT_EQ(0, memcmp("foo", deserialized->random_data.peek_read(), 3));
787 }
788 }
789
TEST(RoundTrip,AbortOperationRequest)790 TEST(RoundTrip, AbortOperationRequest) {
791 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
792 AbortOperationRequest msg(ver);
793 UniquePtr<AbortOperationRequest> deserialized(round_trip(ver, msg, 8));
794 }
795 }
796
TEST(RoundTrip,AttestKeyRequest)797 TEST(RoundTrip, AttestKeyRequest) {
798 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
799 AttestKeyRequest msg(ver);
800 msg.SetKeyMaterial("foo", 3);
801 msg.attest_params.Reinitialize(params, array_length(params));
802
803 UniquePtr<AttestKeyRequest> deserialized(round_trip(ver, msg, 85));
804 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
805 EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
806 EXPECT_EQ(msg.attest_params, deserialized->attest_params);
807 }
808 }
809
TEST(RoundTrip,AttestKeyResponse)810 TEST(RoundTrip, AttestKeyResponse) {
811 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
812 AttestKeyResponse msg(ver);
813 msg.error = KM_ERROR_OK;
814 msg.certificate_chain = CertificateChain(3);
815 EXPECT_TRUE(!!msg.certificate_chain.entries);
816 msg.certificate_chain.entries[0] = {dup_buffer("foo", 3), 3};
817 msg.certificate_chain.entries[1] = {dup_buffer("bar", 3), 3};
818 msg.certificate_chain.entries[2] = {dup_buffer("baz", 3), 3};
819
820 UniquePtr<AttestKeyResponse> deserialized(round_trip(ver, msg, 29));
821 keymaster_cert_chain_t* chain = &deserialized->certificate_chain;
822
823 EXPECT_NE(nullptr, chain->entries);
824 EXPECT_EQ(3U, chain->entry_count);
825 EXPECT_EQ(3U, chain->entries[0].data_length);
826 EXPECT_EQ(0, memcmp("foo", chain->entries[0].data, 3));
827 EXPECT_EQ(3U, chain->entries[1].data_length);
828 EXPECT_EQ(0, memcmp("bar", chain->entries[1].data, 3));
829 EXPECT_EQ(3U, chain->entries[2].data_length);
830 EXPECT_EQ(0, memcmp("baz", chain->entries[2].data, 3));
831 }
832 }
833
TEST(RoundTrip,UpgradeKeyRequest)834 TEST(RoundTrip, UpgradeKeyRequest) {
835 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
836 UpgradeKeyRequest msg(ver);
837 msg.SetKeyMaterial("foo", 3);
838 msg.upgrade_params.Reinitialize(params, array_length(params));
839
840 UniquePtr<UpgradeKeyRequest> deserialized(round_trip(ver, msg, 85));
841 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
842 EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
843 EXPECT_EQ(msg.upgrade_params, deserialized->upgrade_params);
844 }
845 }
846
TEST(RoundTrip,UpgradeKeyResponse)847 TEST(RoundTrip, UpgradeKeyResponse) {
848 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
849 UpgradeKeyResponse req(ver);
850 req.error = KM_ERROR_OK;
851 req.upgraded_key.key_material = dup_array(TEST_DATA);
852 req.upgraded_key.key_material_size = array_length(TEST_DATA);
853
854 UniquePtr<UpgradeKeyResponse> deserialized(round_trip(ver, req, 19));
855 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
856 EXPECT_EQ(req.upgraded_key.key_material_size, deserialized->upgraded_key.key_material_size);
857 EXPECT_EQ(0, memcmp(req.upgraded_key.key_material, deserialized->upgraded_key.key_material,
858 req.upgraded_key.key_material_size));
859 }
860 }
861
TEST(RoundTrip,GenerateTimestampTokenRequest)862 TEST(RoundTrip, GenerateTimestampTokenRequest) {
863 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
864 GenerateTimestampTokenRequest msg(ver);
865 msg.challenge = 1;
866 UniquePtr<GenerateTimestampTokenRequest> deserialized(round_trip(ver, msg, 8));
867 EXPECT_EQ(1U, deserialized->challenge);
868 }
869 }
870
TEST(RoundTrip,GenerateTimestampTokenResponse)871 TEST(RoundTrip, GenerateTimestampTokenResponse) {
872 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
873 GenerateTimestampTokenResponse msg(ver);
874 msg.error = KM_ERROR_OK;
875 msg.token.challenge = 1;
876 msg.token.timestamp = 2;
877 msg.token.security_level = KM_SECURITY_LEVEL_SOFTWARE;
878 msg.token.mac.data = dup_array(TEST_DATA);
879 msg.token.mac.data_length = array_length(TEST_DATA);
880 UniquePtr<GenerateTimestampTokenResponse> deserialized(round_trip(ver, msg, 39));
881 EXPECT_EQ(1U, deserialized->token.challenge);
882 EXPECT_EQ(2U, deserialized->token.timestamp);
883 EXPECT_EQ(KM_SECURITY_LEVEL_SOFTWARE, deserialized->token.security_level);
884 EXPECT_EQ(msg.token.mac.data_length, deserialized->token.mac.data_length);
885 EXPECT_EQ(
886 0, memcmp(msg.token.mac.data, deserialized->token.mac.data, msg.token.mac.data_length));
887 }
888 }
889
TEST(RoundTrip,GetRootOfTrustRequest)890 TEST(RoundTrip, GetRootOfTrustRequest) {
891 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
892 std::vector<uint8_t> challenge{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
893 GetRootOfTrustRequest msg(ver, challenge);
894
895 UniquePtr<GetRootOfTrustRequest> deserialized(round_trip(ver, msg, 20));
896 EXPECT_EQ(deserialized->challenge, challenge);
897 }
898 }
899
TEST(RoundTrip,GetRootOfTrustResponse)900 TEST(RoundTrip, GetRootOfTrustResponse) {
901 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
902 std::vector<uint8_t> rootOfTrust{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
903 GetRootOfTrustResponse msg(ver, rootOfTrust);
904 msg.error = KM_ERROR_OK;
905
906 UniquePtr<GetRootOfTrustResponse> deserialized(round_trip(ver, msg, 24));
907 EXPECT_EQ(deserialized->rootOfTrust, rootOfTrust);
908 }
909 }
910
TEST(RoundTrip,GetHwInfoResponse)911 TEST(RoundTrip, GetHwInfoResponse) {
912 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
913 GetHwInfoResponse rsp(ver);
914 rsp.error = KM_ERROR_OK;
915 rsp.version = 17;
916 rsp.rpcAuthorName = "AAAAA";
917 rsp.supportedEekCurve = 48;
918 rsp.uniqueId = "BBBBB";
919 rsp.supportedNumKeysInCsr = 549;
920
921 UniquePtr<GetHwInfoResponse> deserialized;
922 deserialized.reset(round_trip(ver, rsp, 34));
923
924 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
925 EXPECT_EQ(deserialized->version, rsp.version);
926 EXPECT_EQ(deserialized->rpcAuthorName, rsp.rpcAuthorName);
927 EXPECT_EQ(deserialized->supportedEekCurve, rsp.supportedEekCurve);
928 EXPECT_EQ(deserialized->uniqueId, rsp.uniqueId);
929 EXPECT_EQ(deserialized->supportedNumKeysInCsr, rsp.supportedNumKeysInCsr);
930 }
931 }
932
933 #define SET_ATTESTATION_ID(x) msg.x.Reinitialize(#x, strlen(#x))
934
check_id(const Buffer & id,const char * value)935 void check_id(const Buffer& id, const char* value) {
936 auto len = strlen(value);
937 EXPECT_EQ(id.available_read(), len) << "On " << value;
938 EXPECT_TRUE(memcmp(id.peek_read(), value, len) == 0) << "On " << value;
939 }
940
941 #define CHECK_ID(x) check_id(deserialized->x, #x);
942
TEST(RoundTrip,SetAttestationIdsRequest)943 TEST(RoundTrip, SetAttestationIdsRequest) {
944 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
945 SetAttestationIdsRequest msg(ver);
946 SET_ATTESTATION_ID(brand);
947 SET_ATTESTATION_ID(device);
948 SET_ATTESTATION_ID(product);
949 SET_ATTESTATION_ID(serial);
950 SET_ATTESTATION_ID(imei);
951 SET_ATTESTATION_ID(meid);
952 SET_ATTESTATION_ID(manufacturer);
953 SET_ATTESTATION_ID(model);
954
955 UniquePtr<SetAttestationIdsRequest> deserialized(round_trip(ver, msg, 81));
956 ASSERT_TRUE(deserialized);
957 CHECK_ID(brand);
958 CHECK_ID(device);
959 CHECK_ID(product);
960 CHECK_ID(serial);
961 CHECK_ID(imei);
962 CHECK_ID(model);
963 }
964 }
965
TEST(RoundTrip,SetAttestationIdsKM3Request)966 TEST(RoundTrip, SetAttestationIdsKM3Request) {
967 for (int ver = 0; ver <= kMaxMessageVersion; ++ver) {
968 SetAttestationIdsKM3Request msg(ver);
969 SET_ATTESTATION_ID(base.brand);
970 SET_ATTESTATION_ID(base.device);
971 SET_ATTESTATION_ID(base.product);
972 SET_ATTESTATION_ID(base.serial);
973 SET_ATTESTATION_ID(base.imei);
974 SET_ATTESTATION_ID(base.meid);
975 SET_ATTESTATION_ID(base.manufacturer);
976 SET_ATTESTATION_ID(base.model);
977 SET_ATTESTATION_ID(second_imei);
978
979 UniquePtr<SetAttestationIdsKM3Request> deserialized(round_trip(ver, msg, 136));
980 ASSERT_TRUE(deserialized);
981 CHECK_ID(base.brand);
982 CHECK_ID(base.device);
983 CHECK_ID(base.product);
984 CHECK_ID(base.serial);
985 CHECK_ID(base.imei);
986 CHECK_ID(base.model);
987 CHECK_ID(second_imei);
988 }
989 }
990
TEST(Serialize,ShortBuffer)991 TEST(Serialize, ShortBuffer) {
992 for (int typ = 0; typ <= static_cast<int>(keymaster::SerializableType::kMaxValue); typ++) {
993 // Get a default-constructed object of the relevant serializable type, and ask it
994 // how much space it needs for serialization.
995 auto stype = static_cast<keymaster::SerializableType>(typ);
996 std::unique_ptr<keymaster::Serializable> ser = keymaster::getSerializable(stype);
997 uint16_t expected_size = ser->SerializedSize();
998
999 // Perform serialization of the object into variously sized buffers, mostly too small.
1000 // There's no mechanism for indicating failure, so this test mostly just checks for
1001 // memory errors (and so is a good candidate for running under ASAN/HWASAN).
1002 for (uint16_t actual_size = 0; actual_size <= expected_size; actual_size++) {
1003 std::unique_ptr<uint8_t[]> out_buf =
1004 std::unique_ptr<uint8_t[]>(new uint8_t[actual_size]);
1005 uint8_t* next = ser->Serialize(out_buf.get(), out_buf.get() + actual_size);
1006 EXPECT_TRUE(next <= out_buf.get() + actual_size)
1007 << "Serialization of " << typ << " returned a next offset beyond end+1";
1008 }
1009 }
1010 }
1011
1012 uint8_t msgbuf[] = {
1013 220, 88, 183, 255, 71, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1014 0, 173, 0, 0, 0, 228, 174, 98, 187, 191, 135, 253, 200, 51, 230, 114, 247, 151, 109,
1015 237, 79, 87, 32, 94, 5, 204, 46, 154, 30, 91, 6, 103, 148, 254, 129, 65, 171, 228,
1016 167, 224, 163, 9, 15, 206, 90, 58, 11, 205, 55, 211, 33, 87, 178, 149, 91, 28, 236,
1017 218, 112, 231, 34, 82, 82, 134, 103, 137, 115, 27, 156, 102, 159, 220, 226, 89, 42, 25,
1018 37, 9, 84, 239, 76, 161, 198, 72, 167, 163, 39, 91, 148, 191, 17, 191, 87, 169, 179,
1019 136, 10, 194, 154, 4, 40, 107, 109, 61, 161, 20, 176, 247, 13, 214, 106, 229, 45, 17,
1020 5, 60, 189, 64, 39, 166, 208, 14, 57, 25, 140, 148, 25, 177, 246, 189, 43, 181, 88,
1021 204, 29, 126, 224, 100, 143, 93, 60, 57, 249, 55, 0, 87, 83, 227, 224, 166, 59, 214,
1022 81, 144, 129, 58, 6, 57, 46, 254, 232, 41, 220, 209, 230, 167, 138, 158, 94, 180, 125,
1023 247, 26, 162, 116, 238, 202, 187, 100, 65, 13, 180, 44, 245, 159, 83, 161, 176, 58, 72,
1024 236, 109, 105, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1025 0, 11, 0, 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0,
1026 0, 32, 3, 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0,
1027 1, 0, 0, 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112,
1028 1, 246, 1, 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145,
1029 1, 0, 96, 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0,
1030 0, 0, 0, 0, 190, 2, 0, 16, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0,
1031 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 0, 0, 0, 0, 0, 11, 0,
1032 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0, 0, 32, 3,
1033 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0, 1, 0, 0,
1034 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112, 1, 246, 1,
1035 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145, 1, 0, 96,
1036 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0, 0, 0, 0,
1037 0, 190, 2, 0, 16, 1, 0, 0, 0,
1038 };
1039
1040 /*
1041 * These tests don't have any assertions or expectations. They just try to parse garbage, to see if
1042 * the result will be a crash. This is especially informative when run under Valgrind memcheck.
1043 */
1044
parse_garbage()1045 template <typename Message> void parse_garbage() {
1046 for (int32_t ver = 0; ver <= kMaxMessageVersion; ++ver) {
1047 Message msg(ver);
1048 const uint8_t* end = msgbuf + array_length(msgbuf);
1049 for (size_t i = 0; i < array_length(msgbuf); ++i) {
1050 const uint8_t* begin = msgbuf + i;
1051 const uint8_t* p = begin;
1052 msg.Deserialize(&p, end);
1053 }
1054 }
1055
1056 time_t now = time(nullptr);
1057 std::cout << "Seeding rand() with " << now << " for fuzz test." << std::endl;
1058 srand(now);
1059
1060 // Fill large buffer with random bytes.
1061 const int kBufSize = 10000;
1062 UniquePtr<uint8_t[]> buf(new uint8_t[kBufSize]);
1063 for (size_t i = 0; i < kBufSize; ++i)
1064 buf[i] = static_cast<uint8_t>(rand());
1065
1066 for (uint32_t ver = 0; ver < kMaxMessageVersion; ++ver) {
1067 Message msg(ver);
1068 const uint8_t* end = buf.get() + kBufSize;
1069 for (size_t i = 0; i < kBufSize; ++i) {
1070 const uint8_t* begin = buf.get() + i;
1071 const uint8_t* p = begin;
1072 msg.Deserialize(&p, end);
1073 }
1074 }
1075 }
1076
1077 #define GARBAGE_TEST(Message) \
1078 TEST(GarbageTest, Message) { \
1079 parse_garbage<Message>(); \
1080 }
1081
1082 GARBAGE_TEST(AbortOperationRequest);
1083 GARBAGE_TEST(EmptyKeymasterResponse);
1084 GARBAGE_TEST(AddEntropyRequest);
1085 GARBAGE_TEST(BeginOperationRequest);
1086 GARBAGE_TEST(BeginOperationResponse);
1087 GARBAGE_TEST(DeleteAllKeysRequest);
1088 GARBAGE_TEST(DeleteKeyRequest);
1089 GARBAGE_TEST(ExportKeyRequest);
1090 GARBAGE_TEST(ExportKeyResponse);
1091 GARBAGE_TEST(FinishOperationRequest);
1092 GARBAGE_TEST(FinishOperationResponse);
1093 GARBAGE_TEST(GenerateKeyRequest);
1094 GARBAGE_TEST(GenerateKeyResponse);
1095 GARBAGE_TEST(GetKeyCharacteristicsRequest);
1096 GARBAGE_TEST(GetKeyCharacteristicsResponse);
1097 GARBAGE_TEST(ImportKeyRequest);
1098 GARBAGE_TEST(ImportKeyResponse);
1099 GARBAGE_TEST(UpdateOperationRequest);
1100 GARBAGE_TEST(UpdateOperationResponse);
1101 GARBAGE_TEST(AttestKeyRequest);
1102 GARBAGE_TEST(AttestKeyResponse);
1103 GARBAGE_TEST(UpgradeKeyRequest);
1104 GARBAGE_TEST(UpgradeKeyResponse);
1105 GARBAGE_TEST(GenerateTimestampTokenRequest);
1106 GARBAGE_TEST(GenerateTimestampTokenResponse);
1107 GARBAGE_TEST(SetAttestationIdsRequest);
1108 GARBAGE_TEST(SetAttestationIdsKM3Request);
1109 GARBAGE_TEST(ConfigureVerifiedBootInfoRequest);
1110 GARBAGE_TEST(GetRootOfTrustRequest);
1111 GARBAGE_TEST(GetRootOfTrustResponse);
1112
1113 } // namespace test
1114
1115 } // namespace keymaster
1116