Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 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 | |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 17 | #include <keymaster/google_keymaster_messages.h> |
| 18 | #include <keymaster/google_keymaster_utils.h> |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 19 | |
| 20 | namespace keymaster { |
| 21 | |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Helper functions for working with key blobs. |
| 24 | */ |
| 25 | |
| 26 | static void set_key_blob(keymaster_key_blob_t* key_blob, const void* key_material, size_t length) { |
| 27 | delete[] key_blob->key_material; |
| 28 | key_blob->key_material = dup_buffer(key_material, length); |
| 29 | key_blob->key_material_size = length; |
| 30 | } |
| 31 | |
| 32 | static size_t key_blob_size(const keymaster_key_blob_t& key_blob) { |
| 33 | return sizeof(uint32_t) /* key size */ + key_blob.key_material_size; |
| 34 | } |
| 35 | |
| 36 | static uint8_t* serialize_key_blob(const keymaster_key_blob_t& key_blob, uint8_t* buf, |
| 37 | const uint8_t* end) { |
| 38 | return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 39 | } |
| 40 | |
| 41 | static bool deserialize_key_blob(keymaster_key_blob_t* key_blob, const uint8_t** buf_ptr, |
| 42 | const uint8_t* end) { |
| 43 | delete[] key_blob->key_material; |
| 44 | key_blob->key_material = 0; |
| 45 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 46 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob->key_material_size, |
| 47 | &deserialized_key_material)) |
| 48 | return false; |
| 49 | key_blob->key_material = deserialized_key_material.release(); |
| 50 | return true; |
| 51 | } |
| 52 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 53 | size_t KeymasterResponse::SerializedSize() const { |
| 54 | if (error != KM_ERROR_OK) |
| 55 | return sizeof(int32_t); |
| 56 | else |
| 57 | return sizeof(int32_t) + NonErrorSerializedSize(); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 58 | } |
| 59 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 60 | uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 61 | buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error)); |
| 62 | if (error == KM_ERROR_OK) |
| 63 | buf = NonErrorSerialize(buf, end); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 64 | return buf; |
| 65 | } |
| 66 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 67 | bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 68 | if (!copy_uint32_from_buf(buf_ptr, end, &error)) |
| 69 | return false; |
| 70 | if (error != KM_ERROR_OK) |
| 71 | return true; |
| 72 | return NonErrorDeserialize(buf_ptr, end); |
| 73 | } |
| 74 | |
| 75 | size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const { |
| 76 | return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length; |
| 77 | } |
| 78 | |
| 79 | uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 80 | return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length); |
| 81 | } |
| 82 | |
| 83 | bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 84 | delete[] algorithms; |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 85 | algorithms = NULL; |
| 86 | UniquePtr<keymaster_algorithm_t[]> deserialized_algorithms; |
| 87 | if (!copy_uint32_array_from_buf(buf_ptr, end, &deserialized_algorithms, &algorithms_length)) |
| 88 | return false; |
| 89 | algorithms = deserialized_algorithms.release(); |
| 90 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | GenerateKeyResponse::~GenerateKeyResponse() { |
| 94 | delete[] key_blob.key_material; |
| 95 | } |
| 96 | |
| 97 | size_t GenerateKeyResponse::NonErrorSerializedSize() const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 98 | return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 102 | buf = serialize_key_blob(key_blob, buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 103 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 104 | return unenforced.Serialize(buf, end); |
| 105 | } |
| 106 | |
| 107 | bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 108 | return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) && |
| 109 | unenforced.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 110 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 111 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 112 | GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() { |
| 113 | delete[] key_blob.key_material; |
| 114 | } |
| 115 | |
| 116 | void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 117 | set_key_blob(&key_blob, key_material, length); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | size_t GetKeyCharacteristicsRequest::SerializedSize() const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 121 | return key_blob_size(key_blob) + additional_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 125 | buf = serialize_key_blob(key_blob, buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 126 | return additional_params.Serialize(buf, end); |
| 127 | } |
| 128 | |
| 129 | bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 130 | return deserialize_key_blob(&key_blob, buf_ptr, end) && |
| 131 | additional_params.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 135 | return enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 139 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 140 | return unenforced.Serialize(buf, end); |
| 141 | } |
| 142 | |
| 143 | bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, |
| 144 | const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 145 | return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 149 | set_key_blob(&key_blob, key_material, length); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | size_t BeginOperationRequest::SerializedSize() const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 153 | return sizeof(uint32_t) /* purpose */ + key_blob_size(key_blob) + |
| 154 | additional_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 158 | buf = append_uint32_to_buf(buf, end, purpose); |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 159 | buf = serialize_key_blob(key_blob, buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 160 | return additional_params.Serialize(buf, end); |
| 161 | } |
| 162 | |
| 163 | bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 164 | return copy_uint32_from_buf(buf_ptr, end, &purpose) && |
| 165 | deserialize_key_blob(&key_blob, buf_ptr, end) && |
| 166 | additional_params.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | size_t BeginOperationResponse::NonErrorSerializedSize() const { |
Shawn Willden | 7b38f44 | 2015-01-22 13:07:34 -0700 | [diff] [blame] | 170 | if (message_version == 0) |
| 171 | return sizeof(op_handle); |
| 172 | else |
| 173 | return sizeof(op_handle) + output_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 7b38f44 | 2015-01-22 13:07:34 -0700 | [diff] [blame] | 177 | buf = append_uint64_to_buf(buf, end, op_handle); |
| 178 | if (message_version > 0) |
| 179 | buf = output_params.Serialize(buf, end); |
| 180 | return buf; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 7b38f44 | 2015-01-22 13:07:34 -0700 | [diff] [blame] | 184 | bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle); |
| 185 | if (retval && message_version > 0) |
| 186 | retval = output_params.Deserialize(buf_ptr, end); |
| 187 | return retval; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | size_t UpdateOperationRequest::SerializedSize() const { |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 191 | if (message_version == 0) |
| 192 | return sizeof(op_handle) + input.SerializedSize(); |
| 193 | else |
| 194 | return sizeof(op_handle) + input.SerializedSize() + additional_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 198 | buf = append_uint64_to_buf(buf, end, op_handle); |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 199 | buf = input.Serialize(buf, end); |
| 200 | if (message_version > 0) |
| 201 | buf = additional_params.Serialize(buf, end); |
| 202 | return buf; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 206 | bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end); |
| 207 | if (retval && message_version > 0) |
| 208 | retval = additional_params.Deserialize(buf_ptr, end); |
| 209 | return retval; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | size_t UpdateOperationResponse::NonErrorSerializedSize() const { |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 213 | if (message_version == 0) |
| 214 | return output.SerializedSize(); |
| 215 | else |
| 216 | return output.SerializedSize() + sizeof(uint32_t); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 220 | buf = output.Serialize(buf, end); |
| 221 | if (message_version > 0) |
| 222 | buf = append_uint32_to_buf(buf, end, input_consumed); |
| 223 | return buf; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 227 | bool retval = output.Deserialize(buf_ptr, end); |
| 228 | if (retval && message_version > 0) |
| 229 | retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed); |
| 230 | return retval; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | size_t FinishOperationRequest::SerializedSize() const { |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 234 | if (message_version == 0) |
| 235 | return sizeof(op_handle) + signature.SerializedSize(); |
| 236 | else |
| 237 | return sizeof(op_handle) + signature.SerializedSize() + additional_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 241 | buf = append_uint64_to_buf(buf, end, op_handle); |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 242 | buf = signature.Serialize(buf, end); |
| 243 | if (message_version > 0) |
| 244 | buf = additional_params.Serialize(buf, end); |
| 245 | return buf; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | ba5e94d | 2015-02-06 17:09:23 -0700 | [diff] [blame] | 249 | bool retval = |
| 250 | copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end); |
| 251 | if (retval && message_version > 0) |
| 252 | retval = additional_params.Deserialize(buf_ptr, end); |
| 253 | return retval; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | size_t FinishOperationResponse::NonErrorSerializedSize() const { |
| 257 | return output.SerializedSize(); |
| 258 | } |
| 259 | |
| 260 | uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 261 | return output.Serialize(buf, end); |
| 262 | } |
| 263 | |
| 264 | bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 265 | return output.Deserialize(buf_ptr, end); |
| 266 | } |
| 267 | |
Shawn Willden | cd69582 | 2015-01-26 14:06:32 -0700 | [diff] [blame] | 268 | size_t AddEntropyRequest::SerializedSize() const { |
| 269 | return random_data.SerializedSize(); |
| 270 | } |
| 271 | |
| 272 | uint8_t* AddEntropyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 273 | return random_data.Serialize(buf, end); |
| 274 | } |
| 275 | |
| 276 | bool AddEntropyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 277 | return random_data.Deserialize(buf_ptr, end); |
| 278 | } |
| 279 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 280 | void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 281 | delete[] key_data; |
| 282 | key_data = dup_buffer(key_material, length); |
| 283 | key_data_length = length; |
| 284 | } |
| 285 | |
| 286 | size_t ImportKeyRequest::SerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 287 | return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ + |
| 288 | sizeof(uint32_t) /* key_data_length */ + key_data_length; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 292 | buf = key_description.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 293 | buf = append_uint32_to_buf(buf, end, key_format); |
| 294 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 295 | } |
| 296 | |
| 297 | bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 298 | delete[] key_data; |
| 299 | key_data = NULL; |
| 300 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 301 | if (!key_description.Deserialize(buf_ptr, end) || |
| 302 | !copy_uint32_from_buf(buf_ptr, end, &key_format) || |
| 303 | !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) |
| 304 | return false; |
| 305 | key_data = deserialized_key_material.release(); |
| 306 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 310 | set_key_blob(&key_blob, key_material, length); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | size_t ImportKeyResponse::NonErrorSerializedSize() const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 314 | return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 318 | buf = serialize_key_blob(key_blob, buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 319 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 320 | return unenforced.Serialize(buf, end); |
| 321 | } |
| 322 | |
| 323 | bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 324 | return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) && |
| 325 | unenforced.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 329 | set_key_blob(&key_blob, key_material, length); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | size_t ExportKeyRequest::SerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 333 | return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ + |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 334 | key_blob_size(key_blob); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 338 | buf = additional_params.Serialize(buf, end); |
| 339 | buf = append_uint32_to_buf(buf, end, key_format); |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 340 | return serialize_key_blob(key_blob, buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 38f1701 | 2015-01-13 16:37:08 -0700 | [diff] [blame] | 344 | return additional_params.Deserialize(buf_ptr, end) && |
| 345 | copy_uint32_from_buf(buf_ptr, end, &key_format) && |
| 346 | deserialize_key_blob(&key_blob, buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 350 | delete[] key_data; |
| 351 | key_data = dup_buffer(key_material, length); |
| 352 | key_data_length = length; |
| 353 | } |
| 354 | |
| 355 | size_t ExportKeyResponse::NonErrorSerializedSize() const { |
| 356 | return sizeof(uint32_t) /* key_data_length */ + key_data_length; |
| 357 | } |
| 358 | |
| 359 | uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 360 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 361 | } |
| 362 | |
| 363 | bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame] | 364 | delete[] key_data; |
| 365 | key_data = NULL; |
| 366 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 367 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) |
| 368 | return false; |
| 369 | key_data = deserialized_key_material.release(); |
| 370 | return true; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 371 | } |
| 372 | |
Shawn Willden | 2665e86 | 2014-11-24 14:46:21 -0700 | [diff] [blame] | 373 | size_t GetVersionResponse::NonErrorSerializedSize() const { |
| 374 | return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver); |
| 375 | } |
| 376 | |
| 377 | uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 378 | if (buf + NonErrorSerializedSize() <= end) { |
| 379 | *buf++ = major_ver; |
| 380 | *buf++ = minor_ver; |
| 381 | *buf++ = subminor_ver; |
| 382 | } else { |
| 383 | buf += NonErrorSerializedSize(); |
| 384 | } |
| 385 | return buf; |
| 386 | } |
| 387 | |
| 388 | bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 389 | if (*buf_ptr + NonErrorSerializedSize() > end) |
| 390 | return false; |
| 391 | const uint8_t* tmp = *buf_ptr; |
| 392 | major_ver = *tmp++; |
| 393 | minor_ver = *tmp++; |
| 394 | subminor_ver = *tmp++; |
| 395 | *buf_ptr = tmp; |
| 396 | return true; |
| 397 | } |
| 398 | |
Shawn Willden | 9c9dd9a | 2015-01-14 08:31:49 -0700 | [diff] [blame] | 399 | void RescopeRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 400 | set_key_blob(&key_blob, key_material, length); |
| 401 | } |
| 402 | |
| 403 | size_t RescopeRequest::SerializedSize() const { |
| 404 | return key_blob_size(key_blob) + additional_params.SerializedSize() + |
| 405 | new_authorizations.SerializedSize(); |
| 406 | } |
| 407 | |
| 408 | uint8_t* RescopeRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 409 | buf = serialize_key_blob(key_blob, buf, end); |
| 410 | buf = additional_params.Serialize(buf, end); |
| 411 | return new_authorizations.Serialize(buf, end); |
| 412 | } |
| 413 | |
| 414 | bool RescopeRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 415 | return deserialize_key_blob(&key_blob, buf_ptr, end) && |
| 416 | additional_params.Deserialize(buf_ptr, end) && |
| 417 | new_authorizations.Deserialize(buf_ptr, end); |
| 418 | } |
| 419 | |
| 420 | void RescopeResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 421 | set_key_blob(&key_blob, key_material, length); |
| 422 | } |
| 423 | |
| 424 | size_t RescopeResponse::NonErrorSerializedSize() const { |
| 425 | return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize(); |
| 426 | } |
| 427 | |
| 428 | uint8_t* RescopeResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 429 | buf = serialize_key_blob(key_blob, buf, end); |
| 430 | buf = enforced.Serialize(buf, end); |
| 431 | return unenforced.Serialize(buf, end); |
| 432 | } |
| 433 | |
| 434 | bool RescopeResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 435 | return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) && |
| 436 | unenforced.Deserialize(buf_ptr, end); |
| 437 | } |
| 438 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 439 | } // namespace keymaster |