blob: 3297a1974652fd9c2a2189605fc23b3ab25e3baa [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 Willden125febc2014-12-08 08:15:14 -0700191 return output.SerializedSize() + sizeof(uint32_t);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600192}
193
194uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden125febc2014-12-08 08:15:14 -0700195 buf = output.Serialize(buf, end);
196 return append_uint32_to_buf(buf, end, input_consumed);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600197}
198
199bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden125febc2014-12-08 08:15:14 -0700200 return output.Deserialize(buf_ptr, end) && copy_uint32_from_buf(buf_ptr, end, &input_consumed);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600201}
202
203size_t FinishOperationRequest::SerializedSize() const {
204 return sizeof(op_handle) + signature.SerializedSize();
205}
206
207uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
208 buf = append_uint64_to_buf(buf, end, op_handle);
209 return signature.Serialize(buf, end);
210}
211
212bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
213 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
214}
215
216size_t FinishOperationResponse::NonErrorSerializedSize() const {
217 return output.SerializedSize();
218}
219
220uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
221 return output.Serialize(buf, end);
222}
223
224bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
225 return output.Deserialize(buf_ptr, end);
226}
227
228void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
229 delete[] key_data;
230 key_data = dup_buffer(key_material, length);
231 key_data_length = length;
232}
233
234size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600235 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
236 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600237}
238
239uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600240 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600241 buf = append_uint32_to_buf(buf, end, key_format);
242 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
243}
244
245bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600246 delete[] key_data;
247 key_data = NULL;
248 UniquePtr<uint8_t[]> deserialized_key_material;
249 if (!key_description.Deserialize(buf_ptr, end) ||
250 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
251 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
252 return false;
253 key_data = deserialized_key_material.release();
254 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600255}
256
257void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
258 delete[] key_blob.key_material;
259 key_blob.key_material = dup_buffer(key_material, length);
260 key_blob.key_material_size = length;
261}
262
263size_t ImportKeyResponse::NonErrorSerializedSize() const {
264 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600265 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600266}
267
268uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
269 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 -0600270 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600271 return unenforced.Serialize(buf, end);
272}
273
274bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600275 delete[] key_blob.key_material;
276 key_blob.key_material = NULL;
277 UniquePtr<uint8_t[]> deserialized_key_material;
278 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
279 &deserialized_key_material) ||
280 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
281 return false;
282 key_blob.key_material = deserialized_key_material.release();
283 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600284}
285
286void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
287 delete[] key_blob.key_material;
288 key_blob.key_material = dup_buffer(key_material, length);
289 key_blob.key_material_size = length;
290}
291
292size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600293 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
294 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600295}
296
297uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600298 buf = additional_params.Serialize(buf, end);
299 buf = append_uint32_to_buf(buf, end, key_format);
300 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
301}
302
303bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600304 delete[] key_blob.key_material;
305 key_blob.key_material = NULL;
306 UniquePtr<uint8_t[]> deserialized_key_material;
307 if (!additional_params.Deserialize(buf_ptr, end) ||
308 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
309 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
310 &deserialized_key_material))
311 return false;
312 key_blob.key_material = deserialized_key_material.release();
313 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600314}
315
316void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
317 delete[] key_data;
318 key_data = dup_buffer(key_material, length);
319 key_data_length = length;
320}
321
322size_t ExportKeyResponse::NonErrorSerializedSize() const {
323 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
324}
325
326uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
327 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
328}
329
330bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600331 delete[] key_data;
332 key_data = NULL;
333 UniquePtr<uint8_t[]> deserialized_key_material;
334 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
335 return false;
336 key_data = deserialized_key_material.release();
337 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600338}
339
Shawn Willdend7fe0432014-11-24 14:46:21 -0700340size_t GetVersionResponse::NonErrorSerializedSize() const {
341 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
342}
343
344uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
345 if (buf + NonErrorSerializedSize() <= end) {
346 *buf++ = major_ver;
347 *buf++ = minor_ver;
348 *buf++ = subminor_ver;
349 } else {
350 buf += NonErrorSerializedSize();
351 }
352 return buf;
353}
354
355bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
356 if (*buf_ptr + NonErrorSerializedSize() > end)
357 return false;
358 const uint8_t* tmp = *buf_ptr;
359 major_ver = *tmp++;
360 minor_ver = *tmp++;
361 subminor_ver = *tmp++;
362 *buf_ptr = tmp;
363 return true;
364}
365
Shawn Willden128ffe02014-08-06 12:31:33 -0600366} // namespace keymaster