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 | |
| 17 | #include "google_keymaster_messages.h" |
| 18 | |
| 19 | namespace keymaster { |
| 20 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 21 | size_t KeymasterResponse::SerializedSize() const { |
| 22 | if (error != KM_ERROR_OK) |
| 23 | return sizeof(int32_t); |
| 24 | else |
| 25 | return sizeof(int32_t) + NonErrorSerializedSize(); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 26 | } |
| 27 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 28 | uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 29 | buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error)); |
| 30 | if (error == KM_ERROR_OK) |
| 31 | buf = NonErrorSerialize(buf, end); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 32 | return buf; |
| 33 | } |
| 34 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 35 | bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 36 | if (!copy_uint32_from_buf(buf_ptr, end, &error)) |
| 37 | return false; |
| 38 | if (error != KM_ERROR_OK) |
| 39 | return true; |
| 40 | return NonErrorDeserialize(buf_ptr, end); |
| 41 | } |
| 42 | |
| 43 | size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const { |
| 44 | return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length; |
| 45 | } |
| 46 | |
| 47 | uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 48 | return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length); |
| 49 | } |
| 50 | |
| 51 | bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 52 | delete[] algorithms; |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 53 | algorithms = NULL; |
| 54 | UniquePtr<keymaster_algorithm_t[]> deserialized_algorithms; |
| 55 | if (!copy_uint32_array_from_buf(buf_ptr, end, &deserialized_algorithms, &algorithms_length)) |
| 56 | return false; |
| 57 | algorithms = deserialized_algorithms.release(); |
| 58 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | GenerateKeyResponse::~GenerateKeyResponse() { |
| 62 | delete[] key_blob.key_material; |
| 63 | } |
| 64 | |
| 65 | size_t GenerateKeyResponse::NonErrorSerializedSize() const { |
| 66 | return sizeof(uint32_t) /* key size */ + key_blob.key_material_size + |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 67 | enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 71 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 72 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 73 | return unenforced.Serialize(buf, end); |
| 74 | } |
| 75 | |
| 76 | bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 77 | delete[] key_blob.key_material; |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 78 | key_blob.key_material = NULL; |
| 79 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 80 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 81 | &deserialized_key_material) || |
| 82 | !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end)) |
| 83 | return false; |
| 84 | key_blob.key_material = deserialized_key_material.release(); |
| 85 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 86 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 87 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 88 | GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() { |
| 89 | delete[] key_blob.key_material; |
| 90 | } |
| 91 | |
| 92 | void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 93 | delete[] key_blob.key_material; |
| 94 | key_blob.key_material = dup_buffer(key_material, length); |
| 95 | key_blob.key_material_size = length; |
| 96 | } |
| 97 | |
| 98 | size_t GetKeyCharacteristicsRequest::SerializedSize() const { |
| 99 | return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size + |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 100 | additional_params.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 104 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 105 | return additional_params.Serialize(buf, end); |
| 106 | } |
| 107 | |
| 108 | bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 109 | delete[] key_blob.key_material; |
| 110 | key_blob.key_material = NULL; |
| 111 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 112 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 113 | &deserialized_key_material) || |
| 114 | !additional_params.Deserialize(buf_ptr, end)) |
| 115 | return false; |
| 116 | key_blob.key_material = deserialized_key_material.release(); |
| 117 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 121 | return enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 125 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 126 | return unenforced.Serialize(buf, end); |
| 127 | } |
| 128 | |
| 129 | bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, |
| 130 | const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 131 | return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 135 | delete[] key_blob.key_material; |
| 136 | key_blob.key_material = dup_buffer(key_material, length); |
| 137 | key_blob.key_material_size = length; |
| 138 | } |
| 139 | |
| 140 | size_t BeginOperationRequest::SerializedSize() const { |
| 141 | return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ + |
| 142 | key_blob.key_material_size + additional_params.SerializedSize(); |
| 143 | } |
| 144 | |
| 145 | uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 146 | buf = append_uint32_to_buf(buf, end, purpose); |
| 147 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 148 | return additional_params.Serialize(buf, end); |
| 149 | } |
| 150 | |
| 151 | bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 152 | delete[] key_blob.key_material; |
| 153 | key_blob.key_material = 0; |
| 154 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 155 | if (!copy_uint32_from_buf(buf_ptr, end, &purpose) || |
| 156 | !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 157 | &deserialized_key_material) || |
| 158 | !additional_params.Deserialize(buf_ptr, end)) |
| 159 | return false; |
| 160 | key_blob.key_material = deserialized_key_material.release(); |
| 161 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | size_t BeginOperationResponse::NonErrorSerializedSize() const { |
| 165 | return sizeof(op_handle); |
| 166 | } |
| 167 | |
| 168 | uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 169 | return append_uint64_to_buf(buf, end, op_handle); |
| 170 | } |
| 171 | |
| 172 | bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 173 | return copy_uint64_from_buf(buf_ptr, end, &op_handle); |
| 174 | } |
| 175 | |
| 176 | size_t UpdateOperationRequest::SerializedSize() const { |
| 177 | return sizeof(op_handle) + input.SerializedSize(); |
| 178 | } |
| 179 | |
| 180 | uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 181 | buf = append_uint64_to_buf(buf, end, op_handle); |
| 182 | return input.Serialize(buf, end); |
| 183 | } |
| 184 | |
| 185 | bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 186 | return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end); |
| 187 | } |
| 188 | |
| 189 | size_t UpdateOperationResponse::NonErrorSerializedSize() const { |
| 190 | return output.SerializedSize(); |
| 191 | } |
| 192 | |
| 193 | uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 194 | return output.Serialize(buf, end); |
| 195 | } |
| 196 | |
| 197 | bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 198 | return output.Deserialize(buf_ptr, end); |
| 199 | } |
| 200 | |
| 201 | size_t FinishOperationRequest::SerializedSize() const { |
| 202 | return sizeof(op_handle) + signature.SerializedSize(); |
| 203 | } |
| 204 | |
| 205 | uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 206 | buf = append_uint64_to_buf(buf, end, op_handle); |
| 207 | return signature.Serialize(buf, end); |
| 208 | } |
| 209 | |
| 210 | bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 211 | return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end); |
| 212 | } |
| 213 | |
| 214 | size_t FinishOperationResponse::NonErrorSerializedSize() const { |
| 215 | return output.SerializedSize(); |
| 216 | } |
| 217 | |
| 218 | uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 219 | return output.Serialize(buf, end); |
| 220 | } |
| 221 | |
| 222 | bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 223 | return output.Deserialize(buf_ptr, end); |
| 224 | } |
| 225 | |
| 226 | void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 227 | delete[] key_data; |
| 228 | key_data = dup_buffer(key_material, length); |
| 229 | key_data_length = length; |
| 230 | } |
| 231 | |
| 232 | size_t ImportKeyRequest::SerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 233 | return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ + |
| 234 | sizeof(uint32_t) /* key_data_length */ + key_data_length; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | 437fbd1 | 2014-08-20 11:59:49 -0600 | [diff] [blame] | 238 | buf = key_description.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 239 | buf = append_uint32_to_buf(buf, end, key_format); |
| 240 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 241 | } |
| 242 | |
| 243 | bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 244 | delete[] key_data; |
| 245 | key_data = NULL; |
| 246 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 247 | if (!key_description.Deserialize(buf_ptr, end) || |
| 248 | !copy_uint32_from_buf(buf_ptr, end, &key_format) || |
| 249 | !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) |
| 250 | return false; |
| 251 | key_data = deserialized_key_material.release(); |
| 252 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 256 | delete[] key_blob.key_material; |
| 257 | key_blob.key_material = dup_buffer(key_material, length); |
| 258 | key_blob.key_material_size = length; |
| 259 | } |
| 260 | |
| 261 | size_t ImportKeyResponse::NonErrorSerializedSize() const { |
| 262 | return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size + |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 263 | enforced.SerializedSize() + unenforced.SerializedSize(); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 267 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 268 | buf = enforced.Serialize(buf, end); |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 269 | return unenforced.Serialize(buf, end); |
| 270 | } |
| 271 | |
| 272 | bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 273 | delete[] key_blob.key_material; |
| 274 | key_blob.key_material = NULL; |
| 275 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 276 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 277 | &deserialized_key_material) || |
| 278 | !enforced.Deserialize(buf_ptr, end) || !unenforced.Deserialize(buf_ptr, end)) |
| 279 | return false; |
| 280 | key_blob.key_material = deserialized_key_material.release(); |
| 281 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 285 | delete[] key_blob.key_material; |
| 286 | key_blob.key_material = dup_buffer(key_material, length); |
| 287 | key_blob.key_material_size = length; |
| 288 | } |
| 289 | |
| 290 | size_t ExportKeyRequest::SerializedSize() const { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 291 | return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ + |
| 292 | sizeof(uint32_t) /* key_material_size */ + key_blob.key_material_size; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 296 | buf = additional_params.Serialize(buf, end); |
| 297 | buf = append_uint32_to_buf(buf, end, key_format); |
| 298 | return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 299 | } |
| 300 | |
| 301 | bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 302 | delete[] key_blob.key_material; |
| 303 | key_blob.key_material = NULL; |
| 304 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 305 | if (!additional_params.Deserialize(buf_ptr, end) || |
| 306 | !copy_uint32_from_buf(buf_ptr, end, &key_format) || |
| 307 | !copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 308 | &deserialized_key_material)) |
| 309 | return false; |
| 310 | key_blob.key_material = deserialized_key_material.release(); |
| 311 | return true; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 315 | delete[] key_data; |
| 316 | key_data = dup_buffer(key_material, length); |
| 317 | key_data_length = length; |
| 318 | } |
| 319 | |
| 320 | size_t ExportKeyResponse::NonErrorSerializedSize() const { |
| 321 | return sizeof(uint32_t) /* key_data_length */ + key_data_length; |
| 322 | } |
| 323 | |
| 324 | uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 325 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 326 | } |
| 327 | |
| 328 | bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | f2282b3 | 2014-08-25 06:49:54 -0600 | [diff] [blame^] | 329 | delete[] key_data; |
| 330 | key_data = NULL; |
| 331 | UniquePtr<uint8_t[]> deserialized_key_material; |
| 332 | if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) |
| 333 | return false; |
| 334 | key_data = deserialized_key_material.release(); |
| 335 | return true; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | } // namespace keymaster |