1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 #ifndef FAKE_AVB_OPS_H_ 26 #define FAKE_AVB_OPS_H_ 27 28 #include <base/files/file_util.h> 29 #include <map> 30 #include <set> 31 #include <string> 32 33 #include <libavb_ab/libavb_ab.h> 34 #include <libavb_atx/libavb_atx.h> 35 36 namespace avb { 37 38 // A delegate interface for ops callbacks. This allows tests to override default 39 // fake implementations. For convenience, test fixtures can inherit 40 // FakeAvbOpsDelegateWithDefaults and only override as needed. 41 class FakeAvbOpsDelegate { 42 public: ~FakeAvbOpsDelegate()43 virtual ~FakeAvbOpsDelegate() {} 44 virtual AvbIOResult read_from_partition(const char* partition, 45 int64_t offset, 46 size_t num_bytes, 47 void* buffer, 48 size_t* out_num_read) = 0; 49 50 virtual AvbIOResult get_preloaded_partition( 51 const char* partition, 52 size_t num_bytes, 53 uint8_t** out_pointer, 54 size_t* out_num_bytes_preloaded) = 0; 55 56 virtual AvbIOResult write_to_partition(const char* partition, 57 int64_t offset, 58 size_t num_bytes, 59 const void* buffer) = 0; 60 61 virtual AvbIOResult validate_vbmeta_public_key( 62 AvbOps* ops, 63 const uint8_t* public_key_data, 64 size_t public_key_length, 65 const uint8_t* public_key_metadata, 66 size_t public_key_metadata_length, 67 bool* out_key_is_trusted) = 0; 68 69 virtual AvbIOResult read_rollback_index(AvbOps* ops, 70 size_t rollback_index_slot, 71 uint64_t* out_rollback_index) = 0; 72 73 virtual AvbIOResult write_rollback_index(AvbOps* ops, 74 size_t rollback_index_slot, 75 uint64_t rollback_index) = 0; 76 77 virtual AvbIOResult read_is_device_unlocked(AvbOps* ops, 78 bool* out_is_device_unlocked) = 0; 79 80 virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops, 81 const char* partition, 82 char* guid_buf, 83 size_t guid_buf_size) = 0; 84 85 virtual AvbIOResult get_size_of_partition(AvbOps* ops, 86 const char* partition, 87 uint64_t* out_size) = 0; 88 89 virtual AvbIOResult read_persistent_value(const char* name, 90 size_t buffer_size, 91 uint8_t* out_buffer, 92 size_t* out_num_bytes_read) = 0; 93 94 virtual AvbIOResult write_persistent_value(const char* name, 95 size_t value_size, 96 const uint8_t* value) = 0; 97 98 virtual AvbIOResult validate_public_key_for_partition( 99 AvbOps* ops, 100 const char* partition, 101 const uint8_t* public_key_data, 102 size_t public_key_length, 103 const uint8_t* public_key_metadata, 104 size_t public_key_metadata_length, 105 bool* out_is_trusted, 106 uint32_t* out_rollback_index_location) = 0; 107 108 virtual AvbIOResult read_permanent_attributes( 109 AvbAtxPermanentAttributes* attributes) = 0; 110 111 virtual AvbIOResult read_permanent_attributes_hash( 112 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0; 113 114 virtual void set_key_version(size_t rollback_index_location, 115 uint64_t key_version) = 0; 116 117 virtual AvbIOResult get_random(size_t num_bytes, uint8_t* output) = 0; 118 }; 119 120 // Provides fake implementations of AVB ops. All instances of this class must be 121 // created on the same thread. 122 class FakeAvbOps : public FakeAvbOpsDelegate { 123 public: 124 FakeAvbOps(); 125 virtual ~FakeAvbOps(); 126 GetInstanceFromAvbOps(AvbOps * ops)127 static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) { 128 return reinterpret_cast<FakeAvbOps*>(ops->user_data); 129 } GetInstanceFromAvbABOps(AvbABOps * ab_ops)130 static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) { 131 return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data); 132 } 133 avb_ops()134 AvbOps* avb_ops() { 135 return &avb_ops_; 136 } 137 avb_ab_ops()138 AvbABOps* avb_ab_ops() { 139 return &avb_ab_ops_; 140 } 141 avb_atx_ops()142 AvbAtxOps* avb_atx_ops() { 143 return &avb_atx_ops_; 144 } 145 delegate()146 FakeAvbOpsDelegate* delegate() { 147 return delegate_; 148 } 149 150 // Does not take ownership of |delegate|. set_delegate(FakeAvbOpsDelegate * delegate)151 void set_delegate(FakeAvbOpsDelegate* delegate) { 152 delegate_ = delegate; 153 } 154 set_partition_dir(const base::FilePath & partition_dir)155 void set_partition_dir(const base::FilePath& partition_dir) { 156 partition_dir_ = partition_dir; 157 } 158 set_expected_public_key(const std::string & expected_public_key)159 void set_expected_public_key(const std::string& expected_public_key) { 160 expected_public_key_ = expected_public_key; 161 } 162 set_expected_public_key_for_partition(const std::string & partition_name,const std::string & expected_public_key,uint32_t rollback_index_location)163 void set_expected_public_key_for_partition( 164 const std::string& partition_name, 165 const std::string& expected_public_key, 166 uint32_t rollback_index_location) { 167 expected_public_key_for_partition_map_[partition_name] = 168 expected_public_key; 169 rollback_index_location_for_partition_map_[partition_name] = 170 rollback_index_location; 171 } 172 set_expected_public_key_metadata(const std::string & expected_public_key_metadata)173 void set_expected_public_key_metadata( 174 const std::string& expected_public_key_metadata) { 175 expected_public_key_metadata_ = expected_public_key_metadata; 176 } 177 set_stored_rollback_indexes(const std::map<size_t,uint64_t> & stored_rollback_indexes)178 void set_stored_rollback_indexes( 179 const std::map<size_t, uint64_t>& stored_rollback_indexes) { 180 stored_rollback_indexes_ = stored_rollback_indexes; 181 } 182 get_stored_rollback_indexes()183 std::map<size_t, uint64_t> get_stored_rollback_indexes() { 184 return stored_rollback_indexes_; 185 } 186 get_verified_rollback_indexes()187 std::map<size_t, uint64_t> get_verified_rollback_indexes() { 188 return verified_rollback_indexes_; 189 } 190 set_stored_is_device_unlocked(bool stored_is_device_unlocked)191 void set_stored_is_device_unlocked(bool stored_is_device_unlocked) { 192 stored_is_device_unlocked_ = stored_is_device_unlocked; 193 } 194 set_permanent_attributes(const AvbAtxPermanentAttributes & attributes)195 void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) { 196 permanent_attributes_ = attributes; 197 } 198 set_permanent_attributes_hash(const std::string & hash)199 void set_permanent_attributes_hash(const std::string& hash) { 200 permanent_attributes_hash_ = hash; 201 } 202 203 // All AvbOps for partitions in the given set will fail with 204 // AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION. set_hidden_partitions(const std::set<std::string> & partitions)205 void set_hidden_partitions(const std::set<std::string>& partitions) { 206 hidden_partitions_ = partitions; 207 } 208 209 void enable_get_preloaded_partition(); 210 211 bool preload_partition(const std::string& partition, 212 const base::FilePath& path); 213 214 // Gets the partition names that were passed to the 215 // read_from_partition() operation. 216 std::set<std::string> get_partition_names_read_from(); 217 218 // FakeAvbOpsDelegate methods. 219 AvbIOResult read_from_partition(const char* partition, 220 int64_t offset, 221 size_t num_bytes, 222 void* buffer, 223 size_t* out_num_read) override; 224 225 AvbIOResult get_preloaded_partition(const char* partition, 226 size_t num_bytes, 227 uint8_t** out_pointer, 228 size_t* out_num_bytes_preloaded) override; 229 230 AvbIOResult write_to_partition(const char* partition, 231 int64_t offset, 232 size_t num_bytes, 233 const void* buffer) override; 234 235 AvbIOResult validate_vbmeta_public_key(AvbOps* ops, 236 const uint8_t* public_key_data, 237 size_t public_key_length, 238 const uint8_t* public_key_metadata, 239 size_t public_key_metadata_length, 240 bool* out_key_is_trusted) override; 241 242 AvbIOResult read_rollback_index(AvbOps* ops, 243 size_t rollback_index_location, 244 uint64_t* out_rollback_index) override; 245 246 AvbIOResult write_rollback_index(AvbOps* ops, 247 size_t rollback_index_location, 248 uint64_t rollback_index) override; 249 250 AvbIOResult read_is_device_unlocked(AvbOps* ops, 251 bool* out_is_device_unlocked) override; 252 253 AvbIOResult get_unique_guid_for_partition(AvbOps* ops, 254 const char* partition, 255 char* guid_buf, 256 size_t guid_buf_size) override; 257 258 AvbIOResult get_size_of_partition(AvbOps* ops, 259 const char* partition, 260 uint64_t* out_size) override; 261 262 AvbIOResult read_persistent_value(const char* name, 263 size_t buffer_size, 264 uint8_t* out_buffer, 265 size_t* out_num_bytes_read) override; 266 267 AvbIOResult write_persistent_value(const char* name, 268 size_t value_size, 269 const uint8_t* value) override; 270 271 AvbIOResult validate_public_key_for_partition( 272 AvbOps* ops, 273 const char* partition, 274 const uint8_t* public_key_data, 275 size_t public_key_length, 276 const uint8_t* public_key_metadata, 277 size_t public_key_metadata_length, 278 bool* out_is_trusted, 279 uint32_t* out_rollback_index_location) override; 280 281 AvbIOResult read_permanent_attributes( 282 AvbAtxPermanentAttributes* attributes) override; 283 284 AvbIOResult read_permanent_attributes_hash( 285 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override; 286 287 void set_key_version(size_t rollback_index_location, 288 uint64_t key_version) override; 289 290 AvbIOResult get_random(size_t num_bytes, uint8_t* output) override; 291 292 private: 293 AvbOps avb_ops_; 294 AvbABOps avb_ab_ops_; 295 AvbAtxOps avb_atx_ops_; 296 297 FakeAvbOpsDelegate* delegate_; 298 299 base::FilePath partition_dir_; 300 301 std::string expected_public_key_; 302 std::string expected_public_key_metadata_; 303 304 std::map<std::string, std::string> expected_public_key_for_partition_map_; 305 306 std::map<std::string, uint32_t> rollback_index_location_for_partition_map_; 307 308 std::map<size_t, uint64_t> stored_rollback_indexes_; 309 std::map<size_t, uint64_t> verified_rollback_indexes_; 310 311 bool stored_is_device_unlocked_; 312 313 AvbAtxPermanentAttributes permanent_attributes_; 314 std::string permanent_attributes_hash_; 315 316 std::set<std::string> partition_names_read_from_; 317 std::map<std::string, uint8_t*> preloaded_partitions_; 318 std::set<std::string> hidden_partitions_; 319 320 std::map<std::string, std::string> stored_values_; 321 }; 322 323 // A delegate implementation that calls FakeAvbOps by default. 324 class FakeAvbOpsDelegateWithDefaults : public FakeAvbOpsDelegate { 325 public: read_from_partition(const char * partition,int64_t offset,size_t num_bytes,void * buffer,size_t * out_num_read)326 AvbIOResult read_from_partition(const char* partition, 327 int64_t offset, 328 size_t num_bytes, 329 void* buffer, 330 size_t* out_num_read) override { 331 return ops_.read_from_partition( 332 partition, offset, num_bytes, buffer, out_num_read); 333 } 334 get_preloaded_partition(const char * partition,size_t num_bytes,uint8_t ** out_pointer,size_t * out_num_bytes_preloaded)335 AvbIOResult get_preloaded_partition( 336 const char* partition, 337 size_t num_bytes, 338 uint8_t** out_pointer, 339 size_t* out_num_bytes_preloaded) override { 340 return ops_.get_preloaded_partition( 341 partition, num_bytes, out_pointer, out_num_bytes_preloaded); 342 } 343 write_to_partition(const char * partition,int64_t offset,size_t num_bytes,const void * buffer)344 AvbIOResult write_to_partition(const char* partition, 345 int64_t offset, 346 size_t num_bytes, 347 const void* buffer) override { 348 return ops_.write_to_partition(partition, offset, num_bytes, buffer); 349 } 350 validate_vbmeta_public_key(AvbOps * ops,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_key_is_trusted)351 AvbIOResult validate_vbmeta_public_key(AvbOps* ops, 352 const uint8_t* public_key_data, 353 size_t public_key_length, 354 const uint8_t* public_key_metadata, 355 size_t public_key_metadata_length, 356 bool* out_key_is_trusted) override { 357 return ops_.validate_vbmeta_public_key(ops, 358 public_key_data, 359 public_key_length, 360 public_key_metadata, 361 public_key_metadata_length, 362 out_key_is_trusted); 363 } 364 validate_public_key_for_partition(AvbOps * ops,const char * partition,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_key_is_trusted,uint32_t * out_rollback_index_location)365 AvbIOResult validate_public_key_for_partition( 366 AvbOps* ops, 367 const char* partition, 368 const uint8_t* public_key_data, 369 size_t public_key_length, 370 const uint8_t* public_key_metadata, 371 size_t public_key_metadata_length, 372 bool* out_key_is_trusted, 373 uint32_t* out_rollback_index_location) override { 374 return ops_.validate_public_key_for_partition(ops, 375 partition, 376 public_key_data, 377 public_key_length, 378 public_key_metadata, 379 public_key_metadata_length, 380 out_key_is_trusted, 381 out_rollback_index_location); 382 } 383 read_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t * out_rollback_index)384 AvbIOResult read_rollback_index(AvbOps* ops, 385 size_t rollback_index_slot, 386 uint64_t* out_rollback_index) override { 387 return ops_.read_rollback_index( 388 ops, rollback_index_slot, out_rollback_index); 389 } 390 write_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t rollback_index)391 AvbIOResult write_rollback_index(AvbOps* ops, 392 size_t rollback_index_slot, 393 uint64_t rollback_index) override { 394 return ops_.write_rollback_index(ops, rollback_index_slot, rollback_index); 395 } 396 read_is_device_unlocked(AvbOps * ops,bool * out_is_device_unlocked)397 AvbIOResult read_is_device_unlocked(AvbOps* ops, 398 bool* out_is_device_unlocked) override { 399 return ops_.read_is_device_unlocked(ops, out_is_device_unlocked); 400 } 401 get_unique_guid_for_partition(AvbOps * ops,const char * partition,char * guid_buf,size_t guid_buf_size)402 AvbIOResult get_unique_guid_for_partition(AvbOps* ops, 403 const char* partition, 404 char* guid_buf, 405 size_t guid_buf_size) override { 406 return ops_.get_unique_guid_for_partition( 407 ops, partition, guid_buf, guid_buf_size); 408 } 409 get_size_of_partition(AvbOps * ops,const char * partition,uint64_t * out_size)410 AvbIOResult get_size_of_partition(AvbOps* ops, 411 const char* partition, 412 uint64_t* out_size) override { 413 return ops_.get_size_of_partition(ops, partition, out_size); 414 } 415 read_persistent_value(const char * name,size_t buffer_size,uint8_t * out_buffer,size_t * out_num_bytes_read)416 AvbIOResult read_persistent_value(const char* name, 417 size_t buffer_size, 418 uint8_t* out_buffer, 419 size_t* out_num_bytes_read) override { 420 return ops_.read_persistent_value( 421 name, buffer_size, out_buffer, out_num_bytes_read); 422 } 423 write_persistent_value(const char * name,size_t value_size,const uint8_t * value)424 AvbIOResult write_persistent_value(const char* name, 425 size_t value_size, 426 const uint8_t* value) override { 427 return ops_.write_persistent_value(name, value_size, value); 428 } 429 read_permanent_attributes(AvbAtxPermanentAttributes * attributes)430 AvbIOResult read_permanent_attributes( 431 AvbAtxPermanentAttributes* attributes) override { 432 return ops_.read_permanent_attributes(attributes); 433 } 434 read_permanent_attributes_hash(uint8_t hash[AVB_SHA256_DIGEST_SIZE])435 AvbIOResult read_permanent_attributes_hash( 436 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override { 437 return ops_.read_permanent_attributes_hash(hash); 438 } 439 set_key_version(size_t rollback_index_location,uint64_t key_version)440 void set_key_version(size_t rollback_index_location, 441 uint64_t key_version) override { 442 ops_.set_key_version(rollback_index_location, key_version); 443 } 444 get_random(size_t num_bytes,uint8_t * output)445 AvbIOResult get_random(size_t num_bytes, uint8_t* output) override { 446 return ops_.get_random(num_bytes, output); 447 } 448 449 protected: 450 FakeAvbOps ops_; 451 }; 452 453 } // namespace avb 454 455 #endif /* FAKE_AVB_OPS_H_ */ 456