blob: 50411b49ec0615b255f5e36c0247e75eaf02448a [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
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 <UniquePtr.h>
18
19#include <gtest/gtest.h>
20
Shawn Willdenda8485e2014-08-17 08:00:01 -060021#include "google_keymaster_test_utils.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060022#include "google_keymaster_utils.h"
23#include "google_softkeymaster.h"
Shawn Willdenda8485e2014-08-17 08:00:01 -060024#include "keymaster_tags.h"
Shawn Willden128ffe02014-08-06 12:31:33 -060025
26int main(int argc, char** argv) {
27 ::testing::InitGoogleTest(&argc, argv);
28 int result = RUN_ALL_TESTS();
29 return result;
30}
31
32namespace keymaster {
33namespace test {
34
Shawn Willdenda8485e2014-08-17 08:00:01 -060035/**
36 * Serialize and deserialize a message.
37 */
38template <typename Message> Message* round_trip(const Message& message, size_t expected_size) {
39 size_t size = message.SerializedSize();
40 EXPECT_EQ(expected_size, size);
41 if (size == 0)
42 return NULL;
Shawn Willden128ffe02014-08-06 12:31:33 -060043
44 UniquePtr<uint8_t[]> buf(new uint8_t[size]);
Shawn Willdenda8485e2014-08-17 08:00:01 -060045 EXPECT_EQ(buf.get() + size, message.Serialize(buf.get(), buf.get() + size));
Shawn Willden128ffe02014-08-06 12:31:33 -060046
Shawn Willdenda8485e2014-08-17 08:00:01 -060047 Message* deserialized = new Message;
Shawn Willden58e1a542014-08-08 21:58:29 -060048 const uint8_t* p = buf.get();
Shawn Willdenda8485e2014-08-17 08:00:01 -060049 EXPECT_TRUE(deserialized->Deserialize(&p, p + size));
50 EXPECT_EQ((ptrdiff_t)size, p - buf.get());
51 return deserialized;
Shawn Willden128ffe02014-08-06 12:31:33 -060052}
53
Shawn Willdenda8485e2014-08-17 08:00:01 -060054class EmptyKeymasterResponse : public KeymasterResponse {
55 size_t NonErrorSerializedSize() const { return 1; }
56 uint8_t* NonErrorSerialize(uint8_t* buf, const uint8_t* /* end */) const {
57 *buf++ = 0;
58 return buf;
59 }
60 bool NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
61 if (*buf_ptr >= end)
62 return false;
63 EXPECT_EQ(0, **buf_ptr);
64 (*buf_ptr)++;
65 return true;
66 }
67};
68
69TEST(RoundTrip, EmptyKeymasterResponse) {
70 EmptyKeymasterResponse msg;
71 msg.error = KM_ERROR_OK;
72
73 UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(msg, 5));
74}
75
76TEST(RoundTrip, EmptyKeymasterResponseError) {
77 EmptyKeymasterResponse msg;
78 msg.error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
79
80 UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(msg, 4));
81}
82
83TEST(RoundTrip, SupportedAlgorithmsResponse) {
84 SupportedAlgorithmsResponse rsp;
85 keymaster_algorithm_t algorithms[] = {KM_ALGORITHM_RSA, KM_ALGORITHM_DSA, KM_ALGORITHM_ECDSA};
86 rsp.error = KM_ERROR_OK;
87 rsp.algorithms = dup_array(algorithms);
88 rsp.algorithms_length = array_length(algorithms);
89
90 UniquePtr<SupportedAlgorithmsResponse> deserialized(round_trip(rsp, 20));
91 EXPECT_EQ(array_length(algorithms), deserialized->algorithms_length);
92 EXPECT_EQ(0, memcmp(deserialized->algorithms, algorithms, array_size(algorithms)));
93}
94
95TEST(RoundTrip, SupportedResponse) {
96 SupportedResponse<keymaster_digest_t> rsp;
97 keymaster_digest_t digests[] = {KM_DIGEST_NONE, KM_DIGEST_MD5, KM_DIGEST_SHA1};
98 rsp.error = KM_ERROR_OK;
99 rsp.SetResults(digests);
100
101 UniquePtr<SupportedResponse<keymaster_digest_t>> deserialized(round_trip(rsp, 20));
102 EXPECT_EQ(array_length(digests), deserialized->results_length);
103 EXPECT_EQ(0, memcmp(deserialized->results, digests, array_size(digests)));
104}
105
106static keymaster_key_param_t params[] = {
107 Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN), Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
108 Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA), Authorization(TAG_USER_ID, 7),
109 Authorization(TAG_USER_AUTH_ID, 8), Authorization(TAG_APPLICATION_ID, "app_id", 6),
110 Authorization(TAG_AUTH_TIMEOUT, 300),
111};
Shawn Willden128ffe02014-08-06 12:31:33 -0600112uint8_t TEST_DATA[] = "a key blob";
113
Shawn Willdenda8485e2014-08-17 08:00:01 -0600114TEST(RoundTrip, GenerateKeyRequest) {
115 GenerateKeyRequest req;
116 req.key_description.Reinitialize(params, array_length(params));
117 UniquePtr<GenerateKeyRequest> deserialized(round_trip(req, 78));
118 EXPECT_EQ(deserialized->key_description, req.key_description);
119}
120
121TEST(RoundTrip, GenerateKeyResponse) {
Shawn Willden128ffe02014-08-06 12:31:33 -0600122 GenerateKeyResponse rsp;
123 rsp.error = KM_ERROR_OK;
124 rsp.key_blob.key_material = dup_array(TEST_DATA);
125 rsp.key_blob.key_material_size = array_length(TEST_DATA);
126 rsp.enforced.Reinitialize(params, array_length(params));
127
Shawn Willdenf2282b32014-08-25 06:49:54 -0600128 UniquePtr<GenerateKeyResponse> deserialized(round_trip(rsp, 109));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600129 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
130 EXPECT_EQ(deserialized->enforced, rsp.enforced);
131 EXPECT_EQ(deserialized->unenforced, rsp.unenforced);
Shawn Willden128ffe02014-08-06 12:31:33 -0600132}
133
Shawn Willdenda8485e2014-08-17 08:00:01 -0600134TEST(RoundTrip, GenerateKeyResponseTestError) {
Shawn Willden128ffe02014-08-06 12:31:33 -0600135 GenerateKeyResponse rsp;
136 rsp.error = KM_ERROR_UNSUPPORTED_ALGORITHM;
137 rsp.key_blob.key_material = dup_array(TEST_DATA);
138 rsp.key_blob.key_material_size = array_length(TEST_DATA);
139 rsp.enforced.Reinitialize(params, array_length(params));
140
Shawn Willdenda8485e2014-08-17 08:00:01 -0600141 UniquePtr<GenerateKeyResponse> deserialized(round_trip(rsp, 4));
142 EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, deserialized->error);
143 EXPECT_EQ(0U, deserialized->enforced.size());
144 EXPECT_EQ(0U, deserialized->unenforced.size());
145 EXPECT_EQ(0U, deserialized->key_blob.key_material_size);
146}
Shawn Willden128ffe02014-08-06 12:31:33 -0600147
Shawn Willdenda8485e2014-08-17 08:00:01 -0600148TEST(RoundTrip, GetKeyCharacteristicsRequest) {
149 GetKeyCharacteristicsRequest req;
150 req.additional_params.Reinitialize(params, array_length(params));
151 req.SetKeyMaterial("foo", 3);
Shawn Willden128ffe02014-08-06 12:31:33 -0600152
Shawn Willdenf2282b32014-08-25 06:49:54 -0600153 UniquePtr<GetKeyCharacteristicsRequest> deserialized(round_trip(req, 85));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600154 EXPECT_EQ(7U, deserialized->additional_params.size());
155 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
156 EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
157}
158
159TEST(RoundTrip, GetKeyCharacteristicsResponse) {
160 GetKeyCharacteristicsResponse msg;
161 msg.error = KM_ERROR_OK;
162 msg.enforced.Reinitialize(params, array_length(params));
163 msg.unenforced.Reinitialize(params, array_length(params));
164
Shawn Willdenf2282b32014-08-25 06:49:54 -0600165 UniquePtr<GetKeyCharacteristicsResponse> deserialized(round_trip(msg, 160));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600166 EXPECT_EQ(msg.enforced, deserialized->enforced);
167 EXPECT_EQ(msg.unenforced, deserialized->unenforced);
168}
169
170TEST(RoundTrip, BeginOperationRequest) {
171 BeginOperationRequest msg;
172 msg.purpose = KM_PURPOSE_SIGN;
173 msg.SetKeyMaterial("foo", 3);
174 msg.additional_params.Reinitialize(params, array_length(params));
175
176 UniquePtr<BeginOperationRequest> deserialized(round_trip(msg, 89));
177 EXPECT_EQ(KM_PURPOSE_SIGN, deserialized->purpose);
178 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
179 EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
180 EXPECT_EQ(msg.additional_params, deserialized->additional_params);
181}
182
183TEST(RoundTrip, BeginOperationResponse) {
184 BeginOperationResponse msg;
185 msg.error = KM_ERROR_OK;
186 msg.op_handle = 0xDEADBEEF;
187
188 UniquePtr<BeginOperationResponse> deserialized(round_trip(msg, 12));
189 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
190 EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
191}
192
193TEST(RoundTrip, BeginOperationResponseError) {
194 BeginOperationResponse msg;
195 msg.error = KM_ERROR_INVALID_OPERATION_HANDLE;
196 msg.op_handle = 0xDEADBEEF;
197
198 UniquePtr<BeginOperationResponse> deserialized(round_trip(msg, 4));
199 EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, deserialized->error);
200}
201
202TEST(RoundTrip, UpdateOperationRequest) {
203 UpdateOperationRequest msg;
204 msg.op_handle = 0xDEADBEEF;
205 msg.input.Reinitialize("foo", 3);
206
207 UniquePtr<UpdateOperationRequest> deserialized(round_trip(msg, 15));
208 EXPECT_EQ(3U, deserialized->input.available_read());
209 EXPECT_EQ(0, memcmp(deserialized->input.peek_read(), "foo", 3));
210}
211
212TEST(RoundTrip, UpdateOperationResponse) {
213 UpdateOperationResponse msg;
214 msg.error = KM_ERROR_OK;
215 msg.output.Reinitialize("foo", 3);
216
217 UniquePtr<UpdateOperationResponse> deserialized(round_trip(msg, 11));
218 EXPECT_EQ(KM_ERROR_OK, deserialized->error);
219 EXPECT_EQ(3U, deserialized->output.available_read());
220 EXPECT_EQ(0, memcmp(deserialized->output.peek_read(), "foo", 3));
221}
222
223TEST(RoundTrip, FinishOperationRequest) {
224 FinishOperationRequest msg;
225 msg.op_handle = 0xDEADBEEF;
226 msg.signature.Reinitialize("bar", 3);
227
228 UniquePtr<FinishOperationRequest> deserialized(round_trip(msg, 15));
229 EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
230 EXPECT_EQ(3U, deserialized->signature.available_read());
231 EXPECT_EQ(0, memcmp(deserialized->signature.peek_read(), "bar", 3));
232}
233
234TEST(Round_Trip, FinishOperationResponse) {
235 FinishOperationResponse msg;
236 msg.error = KM_ERROR_OK;
237 msg.output.Reinitialize("foo", 3);
238
239 UniquePtr<FinishOperationResponse> deserialized(round_trip(msg, 11));
240 EXPECT_EQ(msg.error, deserialized->error);
241 EXPECT_EQ(msg.output.available_read(), deserialized->output.available_read());
242 EXPECT_EQ(0, memcmp(msg.output.peek_read(), deserialized->output.peek_read(),
243 msg.output.available_read()));
244}
245
246TEST(RoundTrip, ImportKeyRequest) {
247 ImportKeyRequest msg;
Shawn Willden437fbd12014-08-20 11:59:49 -0600248 msg.key_description.Reinitialize(params, array_length(params));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600249 msg.key_format = KM_KEY_FORMAT_X509;
250 msg.SetKeyMaterial("foo", 3);
251
Shawn Willdenf2282b32014-08-25 06:49:54 -0600252 UniquePtr<ImportKeyRequest> deserialized(round_trip(msg, 89));
Shawn Willden437fbd12014-08-20 11:59:49 -0600253 EXPECT_EQ(msg.key_description, deserialized->key_description);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600254 EXPECT_EQ(msg.key_format, deserialized->key_format);
255 EXPECT_EQ(msg.key_data_length, deserialized->key_data_length);
256 EXPECT_EQ(0, memcmp(msg.key_data, deserialized->key_data, msg.key_data_length));
257}
258
259TEST(RoundTrip, ImportKeyResponse) {
260 ImportKeyResponse msg;
261 msg.error = KM_ERROR_OK;
262 msg.SetKeyMaterial("foo", 3);
263 msg.enforced.Reinitialize(params, array_length(params));
264 msg.unenforced.Reinitialize(params, array_length(params));
265
Shawn Willdenf2282b32014-08-25 06:49:54 -0600266 UniquePtr<ImportKeyResponse> deserialized(round_trip(msg, 167));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600267 EXPECT_EQ(msg.error, deserialized->error);
268 EXPECT_EQ(msg.key_blob.key_material_size, deserialized->key_blob.key_material_size);
269 EXPECT_EQ(0, memcmp(msg.key_blob.key_material, deserialized->key_blob.key_material,
270 msg.key_blob.key_material_size));
271 EXPECT_EQ(msg.enforced, deserialized->enforced);
272 EXPECT_EQ(msg.unenforced, deserialized->unenforced);
273}
274
275TEST(RoundTrip, ExportKeyRequest) {
276 ExportKeyRequest msg;
277 msg.additional_params.Reinitialize(params, array_length(params));
278 msg.key_format = KM_KEY_FORMAT_X509;
279 msg.SetKeyMaterial("foo", 3);
280
Shawn Willdenf2282b32014-08-25 06:49:54 -0600281 UniquePtr<ExportKeyRequest> deserialized(round_trip(msg, 89));
Shawn Willdenda8485e2014-08-17 08:00:01 -0600282 EXPECT_EQ(msg.additional_params, deserialized->additional_params);
283 EXPECT_EQ(msg.key_format, deserialized->key_format);
284 EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
285 EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
286}
287
288TEST(RoundTrip, ExportKeyResponse) {
289 ExportKeyResponse msg;
290 msg.error = KM_ERROR_OK;
291 msg.SetKeyMaterial("foo", 3);
292
293 UniquePtr<ExportKeyResponse> deserialized(round_trip(msg, 11));
294 EXPECT_EQ(3U, deserialized->key_data_length);
295 EXPECT_EQ(0, memcmp("foo", deserialized->key_data, 3));
Shawn Willden128ffe02014-08-06 12:31:33 -0600296}
297
Shawn Willdenf2282b32014-08-25 06:49:54 -0600298uint8_t msgbuf[] = {
299 220, 88, 183, 255, 71, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
300 0, 173, 0, 0, 0, 228, 174, 98, 187, 191, 135, 253, 200, 51, 230, 114, 247, 151, 109,
301 237, 79, 87, 32, 94, 5, 204, 46, 154, 30, 91, 6, 103, 148, 254, 129, 65, 171, 228,
302 167, 224, 163, 9, 15, 206, 90, 58, 11, 205, 55, 211, 33, 87, 178, 149, 91, 28, 236,
303 218, 112, 231, 34, 82, 82, 134, 103, 137, 115, 27, 156, 102, 159, 220, 226, 89, 42, 25,
304 37, 9, 84, 239, 76, 161, 198, 72, 167, 163, 39, 91, 148, 191, 17, 191, 87, 169, 179,
305 136, 10, 194, 154, 4, 40, 107, 109, 61, 161, 20, 176, 247, 13, 214, 106, 229, 45, 17,
306 5, 60, 189, 64, 39, 166, 208, 14, 57, 25, 140, 148, 25, 177, 246, 189, 43, 181, 88,
307 204, 29, 126, 224, 100, 143, 93, 60, 57, 249, 55, 0, 87, 83, 227, 224, 166, 59, 214,
308 81, 144, 129, 58, 6, 57, 46, 254, 232, 41, 220, 209, 230, 167, 138, 158, 94, 180, 125,
309 247, 26, 162, 116, 238, 202, 187, 100, 65, 13, 180, 44, 245, 159, 83, 161, 176, 58, 72,
310 236, 109, 105, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311 0, 11, 0, 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0,
312 0, 32, 3, 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0,
313 1, 0, 0, 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112,
314 1, 246, 1, 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145,
315 1, 0, 96, 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0,
316 0, 0, 0, 0, 190, 2, 0, 16, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0,
317 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 0, 0, 0, 0, 0, 11, 0,
318 0, 0, 98, 0, 0, 0, 1, 0, 0, 32, 2, 0, 0, 0, 1, 0, 0, 32, 3,
319 0, 0, 0, 2, 0, 0, 16, 1, 0, 0, 0, 3, 0, 0, 48, 0, 1, 0, 0,
320 200, 0, 0, 80, 3, 0, 0, 0, 0, 0, 0, 0, 244, 1, 0, 112, 1, 246, 1,
321 0, 112, 1, 189, 2, 0, 96, 144, 178, 236, 250, 255, 255, 255, 255, 145, 1, 0, 96,
322 144, 226, 33, 60, 222, 2, 0, 0, 189, 2, 0, 96, 0, 0, 0, 0, 0, 0, 0,
323 0, 190, 2, 0, 16, 1, 0, 0, 0,
324};
325
326/*
327 * These tests don't have any assertions or expectations. They just try to parse garbage, to see if
328 * the result will be a crash. This is especially informative when run under Valgrind memcheck.
329 */
330
331template <typename Message> void parse_garbage() {
332 Message msg;
333 const uint8_t* end = msgbuf + array_length(msgbuf);
334 for (size_t i = 0; i < array_length(msgbuf); ++i) {
335 const uint8_t* begin = msgbuf + i;
336 const uint8_t* p = begin;
337 msg.Deserialize(&p, end);
338 }
339}
340
341#define GARBAGE_TEST(Message) \
342 TEST(GarbageTest, Message) { parse_garbage<Message>(); }
343
344GARBAGE_TEST(SupportedAlgorithmsResponse)
345GARBAGE_TEST(GenerateKeyRequest);
346GARBAGE_TEST(GenerateKeyResponse);
347GARBAGE_TEST(GetKeyCharacteristicsRequest);
348GARBAGE_TEST(GetKeyCharacteristicsResponse);
349GARBAGE_TEST(BeginOperationRequest);
350GARBAGE_TEST(BeginOperationResponse);
351GARBAGE_TEST(UpdateOperationRequest);
352GARBAGE_TEST(UpdateOperationResponse);
353GARBAGE_TEST(FinishOperationRequest);
354GARBAGE_TEST(FinishOperationResponse);
355// GARBAGE_TEST(AddEntropyRequest);
356GARBAGE_TEST(ImportKeyRequest);
357GARBAGE_TEST(ImportKeyResponse);
358GARBAGE_TEST(ExportKeyRequest);
359GARBAGE_TEST(ExportKeyResponse);
360// GARBAGE_TEST(RescopeRequest);
361// GARBAGE_TEST(RescopeResponse);
362
363// The macro doesn't work on this one.
364TEST(GarbageTest, SupportedResponse) {
365 parse_garbage<SupportedResponse<keymaster_digest_t>>();
366}
367
Shawn Willden128ffe02014-08-06 12:31:33 -0600368} // namespace test
369} // namespace keymaster