blob: 13fb3f2686fa7a45c64d3fc0849043b5e8fbbc49 [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 {
166 return sizeof(op_handle);
167}
168
169uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
170 return append_uint64_to_buf(buf, end, op_handle);
171}
172
173bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
174 return copy_uint64_from_buf(buf_ptr, end, &op_handle);
175}
176
177size_t UpdateOperationRequest::SerializedSize() const {
178 return sizeof(op_handle) + input.SerializedSize();
179}
180
181uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
182 buf = append_uint64_to_buf(buf, end, op_handle);
183 return input.Serialize(buf, end);
184}
185
186bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
187 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end);
188}
189
190size_t UpdateOperationResponse::NonErrorSerializedSize() const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700191 if (message_version == 0)
192 return output.SerializedSize();
193 else
194 return output.SerializedSize() + sizeof(uint32_t);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600195}
196
197uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenb7361132014-12-08 08:15:14 -0700198 buf = output.Serialize(buf, end);
199 if (message_version > 0)
200 buf = append_uint32_to_buf(buf, end, input_consumed);
201 return buf;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600202}
203
204bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700205 bool retval = output.Deserialize(buf_ptr, end);
206 if (retval && message_version > 0)
207 retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed);
208 return retval;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600209}
210
211size_t FinishOperationRequest::SerializedSize() const {
212 return sizeof(op_handle) + signature.SerializedSize();
213}
214
215uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
216 buf = append_uint64_to_buf(buf, end, op_handle);
217 return signature.Serialize(buf, end);
218}
219
220bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
221 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
222}
223
224size_t FinishOperationResponse::NonErrorSerializedSize() const {
225 return output.SerializedSize();
226}
227
228uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
229 return output.Serialize(buf, end);
230}
231
232bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
233 return output.Deserialize(buf_ptr, end);
234}
235
236void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
237 delete[] key_data;
238 key_data = dup_buffer(key_material, length);
239 key_data_length = length;
240}
241
242size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600243 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
244 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600245}
246
247uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600248 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600249 buf = append_uint32_to_buf(buf, end, key_format);
250 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
251}
252
253bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600254 delete[] key_data;
255 key_data = NULL;
256 UniquePtr<uint8_t[]> deserialized_key_material;
257 if (!key_description.Deserialize(buf_ptr, end) ||
258 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
259 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
260 return false;
261 key_data = deserialized_key_material.release();
262 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600263}
264
265void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
266 delete[] key_blob.key_material;
267 key_blob.key_material = dup_buffer(key_material, length);
268 key_blob.key_material_size = length;
269}
270
271size_t ImportKeyResponse::NonErrorSerializedSize() const {
272 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600273 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600274}
275
276uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
277 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 -0600278 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600279 return unenforced.Serialize(buf, end);
280}
281
282bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600283 delete[] key_blob.key_material;
284 key_blob.key_material = NULL;
285 UniquePtr<uint8_t[]> deserialized_key_material;
286 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
287 &deserialized_key_material) ||
288 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
289 return false;
290 key_blob.key_material = deserialized_key_material.release();
291 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600292}
293
294void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
295 delete[] key_blob.key_material;
296 key_blob.key_material = dup_buffer(key_material, length);
297 key_blob.key_material_size = length;
298}
299
300size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600301 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
302 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600303}
304
305uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600306 buf = additional_params.Serialize(buf, end);
307 buf = append_uint32_to_buf(buf, end, key_format);
308 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
309}
310
311bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600312 delete[] key_blob.key_material;
313 key_blob.key_material = NULL;
314 UniquePtr<uint8_t[]> deserialized_key_material;
315 if (!additional_params.Deserialize(buf_ptr, end) ||
316 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
317 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
318 &deserialized_key_material))
319 return false;
320 key_blob.key_material = deserialized_key_material.release();
321 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600322}
323
324void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
325 delete[] key_data;
326 key_data = dup_buffer(key_material, length);
327 key_data_length = length;
328}
329
330size_t ExportKeyResponse::NonErrorSerializedSize() const {
331 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
332}
333
334uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
335 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
336}
337
338bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600339 delete[] key_data;
340 key_data = NULL;
341 UniquePtr<uint8_t[]> deserialized_key_material;
342 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
343 return false;
344 key_data = deserialized_key_material.release();
345 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600346}
347
Shawn Willden2665e862014-11-24 14:46:21 -0700348size_t GetVersionResponse::NonErrorSerializedSize() const {
349 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
350}
351
352uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
353 if (buf + NonErrorSerializedSize() <= end) {
354 *buf++ = major_ver;
355 *buf++ = minor_ver;
356 *buf++ = subminor_ver;
357 } else {
358 buf += NonErrorSerializedSize();
359 }
360 return buf;
361}
362
363bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
364 if (*buf_ptr + NonErrorSerializedSize() > end)
365 return false;
366 const uint8_t* tmp = *buf_ptr;
367 major_ver = *tmp++;
368 minor_ver = *tmp++;
369 subminor_ver = *tmp++;
370 *buf_ptr = tmp;
371 return true;
372}
373
Shawn Willden128ffe02014-08-06 12:31:33 -0600374} // namespace keymaster