1 /*
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 
17 #include <keymaster/google_keymaster_messages.h>
18 #include <keymaster/google_keymaster_utils.h>
19 
20 namespace keymaster {
21 
SerializedSize() const22 size_t KeymasterResponse::SerializedSize() const {
23     if (error != KM_ERROR_OK)
24         return sizeof(int32_t);
25     else
26         return sizeof(int32_t) + NonErrorSerializedSize();
27 }
28 
Serialize(uint8_t * buf,const uint8_t * end) const29 uint8_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);
33     return buf;
34 }
35 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)36 bool 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 
NonErrorSerializedSize() const44 size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const {
45     return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length;
46 }
47 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const48 uint8_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 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)52 bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
53     delete[] algorithms;
54     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;
60 }
61 
~GenerateKeyResponse()62 GenerateKeyResponse::~GenerateKeyResponse() {
63     delete[] key_blob.key_material;
64 }
65 
NonErrorSerializedSize() const66 size_t GenerateKeyResponse::NonErrorSerializedSize() const {
67     return sizeof(uint32_t) /* key size */ + key_blob.key_material_size +
68            enforced.SerializedSize() + unenforced.SerializedSize();
69 }
70 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const71 uint8_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);
73     buf = enforced.Serialize(buf, end);
74     return unenforced.Serialize(buf, end);
75 }
76 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)77 bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
78     delete[] key_blob.key_material;
79     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;
87 }
88 
~GetKeyCharacteristicsRequest()89 GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() {
90     delete[] key_blob.key_material;
91 }
92 
SetKeyMaterial(const void * key_material,size_t length)93 void 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 
SerializedSize() const99 size_t GetKeyCharacteristicsRequest::SerializedSize() const {
100     return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size +
101            additional_params.SerializedSize();
102 }
103 
Serialize(uint8_t * buf,const uint8_t * end) const104 uint8_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);
106     return additional_params.Serialize(buf, end);
107 }
108 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)109 bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
110     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;
119 }
120 
NonErrorSerializedSize() const121 size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const {
122     return enforced.SerializedSize() + unenforced.SerializedSize();
123 }
124 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const125 uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
126     buf = enforced.Serialize(buf, end);
127     return unenforced.Serialize(buf, end);
128 }
129 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)130 bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr,
131                                                         const uint8_t* end) {
132     return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end);
133 }
134 
SetKeyMaterial(const void * key_material,size_t length)135 void 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 
SerializedSize() const141 size_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 
Serialize(uint8_t * buf,const uint8_t * end) const146 uint8_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 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)152 bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
153     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;
163 }
164 
NonErrorSerializedSize() const165 size_t BeginOperationResponse::NonErrorSerializedSize() const {
166     return sizeof(op_handle);
167 }
168 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const169 uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
170     return append_uint64_to_buf(buf, end, op_handle);
171 }
172 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)173 bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
174     return copy_uint64_from_buf(buf_ptr, end, &op_handle);
175 }
176 
SerializedSize() const177 size_t UpdateOperationRequest::SerializedSize() const {
178     return sizeof(op_handle) + input.SerializedSize();
179 }
180 
Serialize(uint8_t * buf,const uint8_t * end) const181 uint8_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 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)186 bool 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 
NonErrorSerializedSize() const190 size_t UpdateOperationResponse::NonErrorSerializedSize() const {
191     return output.SerializedSize();
192 }
193 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const194 uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
195     return output.Serialize(buf, end);
196 }
197 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)198 bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
199     return output.Deserialize(buf_ptr, end);
200 }
201 
SerializedSize() const202 size_t FinishOperationRequest::SerializedSize() const {
203     return sizeof(op_handle) + signature.SerializedSize();
204 }
205 
Serialize(uint8_t * buf,const uint8_t * end) const206 uint8_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 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)211 bool 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 
NonErrorSerializedSize() const215 size_t FinishOperationResponse::NonErrorSerializedSize() const {
216     return output.SerializedSize();
217 }
218 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const219 uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const {
220     return output.Serialize(buf, end);
221 }
222 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)223 bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
224     return output.Deserialize(buf_ptr, end);
225 }
226 
SetKeyMaterial(const void * key_material,size_t length)227 void 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 
SerializedSize() const233 size_t ImportKeyRequest::SerializedSize() const {
234     return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ +
235            sizeof(uint32_t) /* key_data_length */ + key_data_length;
236 }
237 
Serialize(uint8_t * buf,const uint8_t * end) const238 uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
239     buf = key_description.Serialize(buf, end);
240     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 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)244 bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
245     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;
254 }
255 
SetKeyMaterial(const void * key_material,size_t length)256 void 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 
NonErrorSerializedSize() const262 size_t ImportKeyResponse::NonErrorSerializedSize() const {
263     return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size +
264            enforced.SerializedSize() + unenforced.SerializedSize();
265 }
266 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const267 uint8_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);
269     buf = enforced.Serialize(buf, end);
270     return unenforced.Serialize(buf, end);
271 }
272 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)273 bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
274     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;
283 }
284 
SetKeyMaterial(const void * key_material,size_t length)285 void 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 
SerializedSize() const291 size_t ExportKeyRequest::SerializedSize() const {
292     return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ +
293            sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size;
294 }
295 
Serialize(uint8_t * buf,const uint8_t * end) const296 uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const {
297     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 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)302 bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
303     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;
313 }
314 
SetKeyMaterial(const void * key_material,size_t length)315 void 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 
NonErrorSerializedSize() const321 size_t ExportKeyResponse::NonErrorSerializedSize() const {
322     return sizeof(uint32_t) /* key_data_length */ + key_data_length;
323 }
324 
NonErrorSerialize(uint8_t * buf,const uint8_t * end) const325 uint8_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 
NonErrorDeserialize(const uint8_t ** buf_ptr,const uint8_t * end)329 bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
330     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;
337 }
338 
339 }  // namespace keymaster
340