blob: 749a5657689707f36309a8f162ac022cdf7336a1 [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 Willdenda8485e2014-08-17 08:00:01 -0600128 UniquePtr<GenerateKeyResponse> deserialized(round_trip(rsp, 117));
129 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 Willdenda8485e2014-08-17 08:00:01 -0600153 UniquePtr<GetKeyCharacteristicsRequest> deserialized(round_trip(req, 89));
154 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
165 UniquePtr<GetKeyCharacteristicsResponse> deserialized(round_trip(msg, 168));
166 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;
248 msg.additional_params.Reinitialize(params, array_length(params));
249 msg.key_format = KM_KEY_FORMAT_X509;
250 msg.SetKeyMaterial("foo", 3);
251
252 UniquePtr<ImportKeyRequest> deserialized(round_trip(msg, 93));
253 EXPECT_EQ(msg.additional_params, deserialized->additional_params);
254 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
266 UniquePtr<ImportKeyResponse> deserialized(round_trip(msg, 175));
267 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
281 UniquePtr<ExportKeyRequest> deserialized(round_trip(msg, 93));
282 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
298} // namespace test
299} // namespace keymaster