blob: 79e7e1b2a9eea461c232e6432d79b7f8557d184e [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 Willden5da34d22015-01-20 11:44:47 -0700191 return output.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600192}
193
194uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden5da34d22015-01-20 11:44:47 -0700195 return output.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600196}
197
198bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willden5da34d22015-01-20 11:44:47 -0700199 return output.Deserialize(buf_ptr, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600200}
201
202size_t FinishOperationRequest::SerializedSize() const {
203 return sizeof(op_handle) + signature.SerializedSize();
204}
205
206uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
207 buf = append_uint64_to_buf(buf, end, op_handle);
208 return signature.Serialize(buf, end);
209}
210
211bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
212 return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end);
213}
214
215size_t FinishOperationResponse::NonErrorSerializedSize() const {
216 return output.SerializedSize();
217}
218
219uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
220 return output.Serialize(buf, end);
221}
222
223bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
224 return output.Deserialize(buf_ptr, end);
225}
226
227void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
228 delete[] key_data;
229 key_data = dup_buffer(key_material, length);
230 key_data_length = length;
231}
232
233size_t ImportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600234 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
235 sizeof(uint32_t) /* key_data_length */ + key_data_length;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600236}
237
238uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willden437fbd12014-08-20 11:59:49 -0600239 buf = key_description.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600240 buf = append_uint32_to_buf(buf, end, key_format);
241 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
242}
243
244bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600245 delete[] key_data;
246 key_data = NULL;
247 UniquePtr<uint8_t[]> deserialized_key_material;
248 if (!key_description.Deserialize(buf_ptr, end) ||
249 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
250 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
251 return false;
252 key_data = deserialized_key_material.release();
253 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600254}
255
256void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
257 delete[] key_blob.key_material;
258 key_blob.key_material = dup_buffer(key_material, length);
259 key_blob.key_material_size = length;
260}
261
262size_t ImportKeyResponse::NonErrorSerializedSize() const {
263 return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
Shawn Willdenf2282b32014-08-25 06:49:54 -0600264 enforced.SerializedSize() + unenforced.SerializedSize();
Shawn Willdenda8485e2014-08-17 08:00:01 -0600265}
266
267uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
268 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 -0600269 buf = enforced.Serialize(buf, end);
Shawn Willdenda8485e2014-08-17 08:00:01 -0600270 return unenforced.Serialize(buf, end);
271}
272
273bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600274 delete[] key_blob.key_material;
275 key_blob.key_material = NULL;
276 UniquePtr<uint8_t[]> deserialized_key_material;
277 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
278 &deserialized_key_material) ||
279 !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end))
280 return false;
281 key_blob.key_material = deserialized_key_material.release();
282 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600283}
284
285void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) {
286 delete[] key_blob.key_material;
287 key_blob.key_material = dup_buffer(key_material, length);
288 key_blob.key_material_size = length;
289}
290
291size_t ExportKeyRequest::SerializedSize() const {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600292 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
293 sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600294}
295
296uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
Shawn Willdenda8485e2014-08-17 08:00:01 -0600297 buf = additional_params.Serialize(buf, end);
298 buf = append_uint32_to_buf(buf, end, key_format);
299 return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size);
300}
301
302bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600303 delete[] key_blob.key_material;
304 key_blob.key_material = NULL;
305 UniquePtr<uint8_t[]> deserialized_key_material;
306 if (!additional_params.Deserialize(buf_ptr, end) ||
307 !copy_uint32_from_buf(buf_ptr, end, &key_format) ||
308 !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size,
309 &deserialized_key_material))
310 return false;
311 key_blob.key_material = deserialized_key_material.release();
312 return true;
Shawn Willdenda8485e2014-08-17 08:00:01 -0600313}
314
315void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) {
316 delete[] key_data;
317 key_data = dup_buffer(key_material, length);
318 key_data_length = length;
319}
320
321size_t ExportKeyResponse::NonErrorSerializedSize() const {
322 return sizeof(uint32_t) /* key_data_length */ + key_data_length;
323}
324
325uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
326 return append_size_and_data_to_buf(buf, end, key_data, key_data_length);
327}
328
329bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
Shawn Willdenf2282b32014-08-25 06:49:54 -0600330 delete[] key_data;
331 key_data = NULL;
332 UniquePtr<uint8_t[]> deserialized_key_material;
333 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material))
334 return false;
335 key_data = deserialized_key_material.release();
336 return true;
Shawn Willden128ffe02014-08-06 12:31:33 -0600337}
338
Shawn Willden2665e862014-11-24 14:46:21 -0700339size_t GetVersionResponse::NonErrorSerializedSize() const {
340 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver);
341}
342
343uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
344 if (buf + NonErrorSerializedSize() <= end) {
345 *buf++ = major_ver;
346 *buf++ = minor_ver;
347 *buf++ = subminor_ver;
348 } else {
349 buf += NonErrorSerializedSize();
350 }
351 return buf;
352}
353
354bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
355 if (*buf_ptr + NonErrorSerializedSize() > end)
356 return false;
357 const uint8_t* tmp = *buf_ptr;
358 major_ver = *tmp++;
359 minor_ver = *tmp++;
360 subminor_ver = *tmp++;
361 *buf_ptr = tmp;
362 return true;
363}
364
Shawn Willden128ffe02014-08-06 12:31:33 -0600365} // namespace keymaster