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/android_keymaster_messages.h> 18 #include <keymaster/android_keymaster_utils.h> 19 20 namespace keymaster { 21 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 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(); 58 } 59 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); 64 return buf; 65 } 66 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 GenerateKeyResponse::~GenerateKeyResponse() { 76 delete[] key_blob.key_material; 77 } 78 79 size_t GenerateKeyResponse::NonErrorSerializedSize() const { 80 return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize(); 81 } 82 83 uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 84 buf = serialize_key_blob(key_blob, buf, end); 85 buf = enforced.Serialize(buf, end); 86 return unenforced.Serialize(buf, end); 87 } 88 89 bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 90 return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) && 91 unenforced.Deserialize(buf_ptr, end); 92 } 93 94 GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() { 95 delete[] key_blob.key_material; 96 } 97 98 void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) { 99 set_key_blob(&key_blob, key_material, length); 100 } 101 102 size_t GetKeyCharacteristicsRequest::SerializedSize() const { 103 return key_blob_size(key_blob) + additional_params.SerializedSize(); 104 } 105 106 uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 107 buf = serialize_key_blob(key_blob, buf, end); 108 return additional_params.Serialize(buf, end); 109 } 110 111 bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 112 return deserialize_key_blob(&key_blob, buf_ptr, end) && 113 additional_params.Deserialize(buf_ptr, end); 114 } 115 116 size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const { 117 return enforced.SerializedSize() + unenforced.SerializedSize(); 118 } 119 120 uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 121 buf = enforced.Serialize(buf, end); 122 return unenforced.Serialize(buf, end); 123 } 124 125 bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, 126 const uint8_t* end) { 127 return enforced.Deserialize(buf_ptr, end) && unenforced.Deserialize(buf_ptr, end); 128 } 129 130 void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) { 131 set_key_blob(&key_blob, key_material, length); 132 } 133 134 size_t BeginOperationRequest::SerializedSize() const { 135 return sizeof(uint32_t) /* purpose */ + key_blob_size(key_blob) + 136 additional_params.SerializedSize(); 137 } 138 139 uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 140 buf = append_uint32_to_buf(buf, end, purpose); 141 buf = serialize_key_blob(key_blob, buf, end); 142 return additional_params.Serialize(buf, end); 143 } 144 145 bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 146 return copy_uint32_from_buf(buf_ptr, end, &purpose) && 147 deserialize_key_blob(&key_blob, buf_ptr, end) && 148 additional_params.Deserialize(buf_ptr, end); 149 } 150 151 size_t BeginOperationResponse::NonErrorSerializedSize() const { 152 if (message_version == 0) 153 return sizeof(op_handle); 154 else 155 return sizeof(op_handle) + output_params.SerializedSize(); 156 } 157 158 uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 159 buf = append_uint64_to_buf(buf, end, op_handle); 160 if (message_version > 0) 161 buf = output_params.Serialize(buf, end); 162 return buf; 163 } 164 165 bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 166 bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle); 167 if (retval && message_version > 0) 168 retval = output_params.Deserialize(buf_ptr, end); 169 return retval; 170 } 171 172 size_t UpdateOperationRequest::SerializedSize() const { 173 if (message_version == 0) 174 return sizeof(op_handle) + input.SerializedSize(); 175 else 176 return sizeof(op_handle) + input.SerializedSize() + additional_params.SerializedSize(); 177 } 178 179 uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 180 buf = append_uint64_to_buf(buf, end, op_handle); 181 buf = input.Serialize(buf, end); 182 if (message_version > 0) 183 buf = additional_params.Serialize(buf, end); 184 return buf; 185 } 186 187 bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 188 bool retval = copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end); 189 if (retval && message_version > 0) 190 retval = additional_params.Deserialize(buf_ptr, end); 191 return retval; 192 } 193 194 size_t UpdateOperationResponse::NonErrorSerializedSize() const { 195 switch (message_version) { 196 case 0: 197 return output.SerializedSize(); 198 case 1: 199 return output.SerializedSize() + sizeof(uint32_t); 200 case 2: 201 return output.SerializedSize() + sizeof(uint32_t) + output_params.SerializedSize(); 202 default: 203 assert(false); 204 return 0; 205 } 206 } 207 208 uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 209 buf = output.Serialize(buf, end); 210 if (message_version > 0) 211 buf = append_uint32_to_buf(buf, end, input_consumed); 212 if (message_version > 1) 213 buf = output_params.Serialize(buf, end); 214 return buf; 215 } 216 217 bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 218 bool retval = output.Deserialize(buf_ptr, end); 219 if (retval && message_version > 0) 220 retval = copy_uint32_from_buf(buf_ptr, end, &input_consumed); 221 if (retval && message_version > 1) 222 retval = output_params.Deserialize(buf_ptr, end); 223 return retval; 224 } 225 226 size_t FinishOperationRequest::SerializedSize() const { 227 if (message_version == 0) 228 return sizeof(op_handle) + signature.SerializedSize(); 229 else 230 return sizeof(op_handle) + signature.SerializedSize() + additional_params.SerializedSize(); 231 } 232 233 uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 234 buf = append_uint64_to_buf(buf, end, op_handle); 235 buf = signature.Serialize(buf, end); 236 if (message_version > 0) 237 buf = additional_params.Serialize(buf, end); 238 return buf; 239 } 240 241 bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 242 bool retval = 243 copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end); 244 if (retval && message_version > 0) 245 retval = additional_params.Deserialize(buf_ptr, end); 246 return retval; 247 } 248 249 size_t FinishOperationResponse::NonErrorSerializedSize() const { 250 if (message_version < 2) 251 return output.SerializedSize(); 252 else 253 return output.SerializedSize() + output_params.SerializedSize(); 254 } 255 256 uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 257 buf = output.Serialize(buf, end); 258 if (message_version > 1) 259 buf = output_params.Serialize(buf, end); 260 return buf; 261 } 262 263 bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 264 bool retval = output.Deserialize(buf_ptr, end); 265 if (retval && message_version > 1) 266 retval = output_params.Deserialize(buf_ptr, end); 267 return retval; 268 } 269 270 size_t AddEntropyRequest::SerializedSize() const { 271 return random_data.SerializedSize(); 272 } 273 274 uint8_t* AddEntropyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 275 return random_data.Serialize(buf, end); 276 } 277 278 bool AddEntropyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 279 return random_data.Deserialize(buf_ptr, end); 280 } 281 282 void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { 283 delete[] key_data; 284 key_data = dup_buffer(key_material, length); 285 key_data_length = length; 286 } 287 288 size_t ImportKeyRequest::SerializedSize() const { 289 return key_description.SerializedSize() + sizeof(uint32_t) /* key_format */ + 290 sizeof(uint32_t) /* key_data_length */ + key_data_length; 291 } 292 293 uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 294 buf = key_description.Serialize(buf, end); 295 buf = append_uint32_to_buf(buf, end, key_format); 296 return append_size_and_data_to_buf(buf, end, key_data, key_data_length); 297 } 298 299 bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 300 delete[] key_data; 301 key_data = NULL; 302 UniquePtr<uint8_t[]> deserialized_key_material; 303 if (!key_description.Deserialize(buf_ptr, end) || 304 !copy_uint32_from_buf(buf_ptr, end, &key_format) || 305 !copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) 306 return false; 307 key_data = deserialized_key_material.release(); 308 return true; 309 } 310 311 void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { 312 set_key_blob(&key_blob, key_material, length); 313 } 314 315 size_t ImportKeyResponse::NonErrorSerializedSize() const { 316 return key_blob_size(key_blob) + enforced.SerializedSize() + unenforced.SerializedSize(); 317 } 318 319 uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 320 buf = serialize_key_blob(key_blob, buf, end); 321 buf = enforced.Serialize(buf, end); 322 return unenforced.Serialize(buf, end); 323 } 324 325 bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 326 return deserialize_key_blob(&key_blob, buf_ptr, end) && enforced.Deserialize(buf_ptr, end) && 327 unenforced.Deserialize(buf_ptr, end); 328 } 329 330 void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { 331 set_key_blob(&key_blob, key_material, length); 332 } 333 334 size_t ExportKeyRequest::SerializedSize() const { 335 return additional_params.SerializedSize() + sizeof(uint32_t) /* key_format */ + 336 key_blob_size(key_blob); 337 } 338 339 uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 340 buf = additional_params.Serialize(buf, end); 341 buf = append_uint32_to_buf(buf, end, key_format); 342 return serialize_key_blob(key_blob, buf, end); 343 } 344 345 bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 346 return additional_params.Deserialize(buf_ptr, end) && 347 copy_uint32_from_buf(buf_ptr, end, &key_format) && 348 deserialize_key_blob(&key_blob, buf_ptr, end); 349 } 350 351 void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { 352 delete[] key_data; 353 key_data = dup_buffer(key_material, length); 354 key_data_length = length; 355 } 356 357 size_t ExportKeyResponse::NonErrorSerializedSize() const { 358 return sizeof(uint32_t) /* key_data_length */ + key_data_length; 359 } 360 361 uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 362 return append_size_and_data_to_buf(buf, end, key_data, key_data_length); 363 } 364 365 bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 366 delete[] key_data; 367 key_data = NULL; 368 UniquePtr<uint8_t[]> deserialized_key_material; 369 if (!copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &deserialized_key_material)) 370 return false; 371 key_data = deserialized_key_material.release(); 372 return true; 373 } 374 375 void DeleteKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { 376 set_key_blob(&key_blob, key_material, length); 377 } 378 379 size_t DeleteKeyRequest::SerializedSize() const { 380 return key_blob_size(key_blob); 381 } 382 383 uint8_t* DeleteKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { 384 return serialize_key_blob(key_blob, buf, end); 385 } 386 387 bool DeleteKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { 388 return deserialize_key_blob(&key_blob, buf_ptr, end); 389 } 390 391 size_t GetVersionResponse::NonErrorSerializedSize() const { 392 return sizeof(major_ver) + sizeof(minor_ver) + sizeof(subminor_ver); 393 } 394 395 uint8_t* GetVersionResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { 396 if (buf + NonErrorSerializedSize() <= end) { 397 *buf++ = major_ver; 398 *buf++ = minor_ver; 399 *buf++ = subminor_ver; 400 } else { 401 buf += NonErrorSerializedSize(); 402 } 403 return buf; 404 } 405 406 bool GetVersionResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { 407 if (*buf_ptr + NonErrorSerializedSize() > end) 408 return false; 409 const uint8_t* tmp = *buf_ptr; 410 major_ver = *tmp++; 411 minor_ver = *tmp++; 412 subminor_ver = *tmp++; 413 *buf_ptr = tmp; 414 return true; 415 } 416 417 } // namespace keymaster 418