blob: dea11babbe1730d82c8e83ddb9ca1678b7fb2ff8 [file] [log] [blame]
Shawn Willden128ffe02014-08-06 12:31:33 -06001/*
2 * Copyright 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
Shawn Willden98d9b922014-08-26 08:14:10 -060017#include <keymaster/google_keymaster_messages.h>
18#include <keymaster/google_keymaster_utils.h>
Shawn Willden128ffe02014-08-06 12:31:33 -060019
20namespace keymaster {
21
Shawn Willdenda8485e2014-08-17 08:00:01 -060022size_t KeymasterResponse::SerializedSize() const {
23 if (error != KM_ERROR_OK)
24 return sizeof(int32_t);
25 else
26 return sizeof(int32_t) + NonErrorSerializedSize();
Shawn Willden128ffe02014-08-06 12:31:33 -060027}
28
Shawn Willdenda8485e2014-08-17 08:00:01 -060029uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const {
30 buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error));
31 if (error == KM_ERROR_OK)
32 buf = NonErrorSerialize(buf, end);
Shawn Willden128ffe02014-08-06 12:31:33 -060033 return buf;
34}
35
Shawn Willdenda8485e2014-08-17 08:00:01 -060036bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
37 if (!copy_uint32_from_buf(buf_ptr, end, &error))
38 return false;
39 if (error != KM_ERROR_OK)
40 return true;
41 return NonErrorDeserialize(buf_ptr, end);
42}
43
44size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const {
45 return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length;
46}
47
48uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
49 return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length);
50}
51
52bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
53 delete[] algorithms;
Shawn Willdenf2282b32014-08-25 06:49:54 -060054 algorithms = NULL;
55 UniquePtr<keymaster_algorithm_t[]> deserialized_algorithms;
56 if (!copy_uint32_array_from_buf(buf_ptr, end, &deserialized_algorithms, &algorithms_length))
57 return false;
58 algorithms = deserialized_algorithms.release();
59 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060060}
61
62GenerateKeyResponse::~GenerateKeyResponse() {
63 delete[] key_blob.key_material;
64}
65
66size_t GenerateKeyResponse::NonErrorSerializedSize() const {
67 return sizeof(uint32_t) /* key size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -060068 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -060069}
70
71uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
72 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -060073 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -060074 return unenforced.Serialize(buf, end);
75}
76
77bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden128ffe02014-08-06 12:31:33 -060078 delete[] key_blob.key_material;
Shawn Willdenf2282b32014-08-25 06:49:54 -060079 key_blob.key_material = NULL;
80 UniquePtr<uint8_t[]> deserialized_key_material;
81 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
82 &deserialized_key_material) ||
83 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
84 return false;
85 key_blob.key_material = deserialized_key_material.release();
86 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -060087}
Shawn Willden128ffe02014-08-06 12:31:33 -060088
Shawn Willdenda8485e2014-08-17 08:00:01 -060089GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
90 delete[] key_blob.key_material;
91}
92
93void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) {
94 delete[] key_blob.key_material;
95 key_blob.key_material = dup_buffer(key_material, length);
96 key_blob.key_material_size = length;
97}
98
99size_t GetKeyCharacteristicsRequest::SerializedSize() const {
100 return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600101 additional_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600102}
103
104uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
105 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600106 return additional_params.Serialize(buf, end);
107}
108
109bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600110 delete[] key_blob.key_material;
111 key_blob.key_material = NULL;
112 UniquePtr<uint8_t[]> deserialized_key_material;
113 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
114 &deserialized_key_material) ||
115 !additional_params.Deserialize(buf_ptr, end))
116 return false;
117 key_blob.key_material = deserialized_key_material.release();
118 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600119}
120
121size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600122 return enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600123}
124
125uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600126 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600127 return unenforced.Serialize(buf, end);
128}
129
130bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
131 const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600132 return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600133}
134
135void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) {
136 delete[] key_blob.key_material;
137 key_blob.key_material = dup_buffer(key_material, length);
138 key_blob.key_material_size = length;
139}
140
141size_t BeginOperationRequest::SerializedSize() const {
142 return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ +
143 key_blob.key_material_size + additional_params.SerializedSize();
144}
145
146uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
147 buf = append_uint32_to_buf(buf, end, purpose);
148 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
149 return additional_params.Serialize(buf, end);
150}
151
152bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600153 delete[] key_blob.key_material;
154 key_blob.key_material = 0;
155 UniquePtr<uint8_t[]> deserialized_key_material;
156 if (!copy_uint32_from_buf(buf_ptr, end, &purpose) ||
157 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
158 &deserialized_key_material) ||
159 !additional_params.Deserialize(buf_ptr, end))
160 return false;
161 key_blob.key_material = deserialized_key_material.release();
162 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600163}
164
165size_t BeginOperationResponse::NonErrorSerializedSize() const {
Shawn Willden7b38f442015-01-22 13:07:34 -0700166 if (message_version == 0)
167 return sizeof(op_handle);
168 else
169 return sizeof(op_handle) + output_params.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600170}
171
172uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden7b38f442015-01-22 13:07:34 -0700173 buf = append_uint64_to_buf(buf, end, op_handle);
174 if (message_version > 0)
175 buf = output_params.Serialize(buf, end);
176 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600177}
178
179bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden7b38f442015-01-22 13:07:34 -0700180 bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle);
181 if (retval && message_version > 0)
182 retval = output_params.Deserialize(buf_ptr, end);
183 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600184}
185
186size_t UpdateOperationRequest::SerializedSize() const {
187 return sizeof(op_handle) + input.SerializedSize();
188}
189
190uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
191 buf = append_uint64_to_buf(buf, end, op_handle);
192 return input.Serialize(buf, end);
193}
194
195bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
196 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
197}
198
199size_t UpdateOperationResponse::NonErrorSerializedSize() const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700200 if (message_version == 0)
201 return output.SerializedSize();
202 else
203 return output.SerializedSize() + sizeof(uint32_t);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600204}
205
206uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700207 buf = output.Serialize(buf, end);
208 if (message_version > 0)
209 buf = append_uint32_to_buf(buf, end, input_consumed);
210 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600211}
212
213bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700214 bool retval = output.Deserialize(buf_ptr, end);
215 if (retval && message_version > 0)
216 retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed);
217 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600218}
219
220size_t FinishOperationRequest::SerializedSize() const {
221 return sizeof(op_handle) + signature.SerializedSize();
222}
223
224uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
225 buf = append_uint64_to_buf(buf, end, op_handle);
226 return signature.Serialize(buf, end);
227}
228
229bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
230 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
231}
232
233size_t FinishOperationResponse::NonErrorSerializedSize() const {
234 return output.SerializedSize();
235}
236
237uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
238 return output.Serialize(buf, end);
239}
240
241bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
242 return output.Deserialize(buf_ptr, end);
243}
244
245void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
246 delete[] key_data;
247 key_data = dup_buffer(key_material, length);
248 key_data_length = length;
249}
250
251size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600252 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
253 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600254}
255
256uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600257 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600258 buf = append_uint32_to_buf(buf, end, key_format);
259 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
260}
261
262bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600263 delete[] key_data;
264 key_data = NULL;
265 UniquePtr<uint8_t[]> deserialized_key_material;
266 if (!key_description.Deserialize(buf_ptr, end) ||
267 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
268 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
269 return false;
270 key_data = deserialized_key_material.release();
271 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600272}
273
274void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
275 delete[] key_blob.key_material;
276 key_blob.key_material = dup_buffer(key_material, length);
277 key_blob.key_material_size = length;
278}
279
280size_t ImportKeyResponse::NonErrorSerializedSize() const {
281 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600282 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600283}
284
285uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
286 buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600287 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600288 return unenforced.Serialize(buf, end);
289}
290
291bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600292 delete[] key_blob.key_material;
293 key_blob.key_material = NULL;
294 UniquePtr<uint8_t[]> deserialized_key_material;
295 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
296 &deserialized_key_material) ||
297 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
298 return false;
299 key_blob.key_material = deserialized_key_material.release();
300 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600301}
302
303void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
304 delete[] key_blob.key_material;
305 key_blob.key_material = dup_buffer(key_material, length);
306 key_blob.key_material_size = length;
307}
308
309size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600310 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
311 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600312}
313
314uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600315 buf = additional_params.Serialize(buf, end);
316 buf = append_uint32_to_buf(buf, end, key_format);
317 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
318}
319
320bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600321 delete[] key_blob.key_material;
322 key_blob.key_material = NULL;
323 UniquePtr<uint8_t[]> deserialized_key_material;
324 if (!additional_params.Deserialize(buf_ptr, end) ||
325 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
326 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
327 &deserialized_key_material))
328 return false;
329 key_blob.key_material = deserialized_key_material.release();
330 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600331}
332
333void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
334 delete[] key_data;
335 key_data = dup_buffer(key_material, length);
336 key_data_length = length;
337}
338
339size_t ExportKeyResponse::NonErrorSerializedSize() const {
340 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
341}
342
343uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
344 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
345}
346
347bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600348 delete[] key_data;
349 key_data = NULL;
350 UniquePtr<uint8_t[]> deserialized_key_material;
351 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
352 return false;
353 key_data = deserialized_key_material.release();
354 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600355}
356
Shawn Willden2665e862014-11-24 14:46:21 -0700357size_t GetVersionResponse::NonErrorSerializedSize() const {
358 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
359}
360
361uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
362 if (buf + NonErrorSerializedSize() <= end) {
363 *buf++ = major_ver;
364 *buf++ = minor_ver;
365 *buf++ = subminor_ver;
366 } else {
367 buf += NonErrorSerializedSize();
368 }
369 return buf;
370}
371
372bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
373 if (*buf_ptr + NonErrorSerializedSize() > end)
374 return false;
375 const uint8_t* tmp = *buf_ptr;
376 major_ver = *tmp++;
377 minor_ver = *tmp++;
378 subminor_ver = *tmp++;
379 *buf_ptr = tmp;
380 return true;
381}
382
Shawn Willden128ffe02014-08-06 12:31:33 -0600383} // namespace keymaster