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 bool preload_preallocated_partition(const std::string& partition, 215 uint8_t* buffer, 216 size_t size); 217 218 // Gets the partition names that were passed to the 219 // read_from_partition() operation. 220 std::set<std::string> get_partition_names_read_from(); 221 222 // FakeAvbOpsDelegate methods. 223 AvbIOResult read_from_partition(const char* partition, 224 int64_t offset, 225 size_t num_bytes, 226 void* buffer, 227 size_t* out_num_read) override; 228 229 AvbIOResult get_preloaded_partition(const char* partition, 230 size_t num_bytes, 231 uint8_t** out_pointer, 232 size_t* out_num_bytes_preloaded) override; 233 234 AvbIOResult write_to_partition(const char* partition, 235 int64_t offset, 236 size_t num_bytes, 237 const void* buffer) override; 238 239 AvbIOResult validate_vbmeta_public_key(AvbOps* ops, 240 const uint8_t* public_key_data, 241 size_t public_key_length, 242 const uint8_t* public_key_metadata, 243 size_t public_key_metadata_length, 244 bool* out_key_is_trusted) override; 245 246 AvbIOResult read_rollback_index(AvbOps* ops, 247 size_t rollback_index_location, 248 uint64_t* out_rollback_index) override; 249 250 AvbIOResult write_rollback_index(AvbOps* ops, 251 size_t rollback_index_location, 252 uint64_t rollback_index) override; 253 254 AvbIOResult read_is_device_unlocked(AvbOps* ops, 255 bool* out_is_device_unlocked) override; 256 257 AvbIOResult get_unique_guid_for_partition(AvbOps* ops, 258 const char* partition, 259 char* guid_buf, 260 size_t guid_buf_size) override; 261 262 AvbIOResult get_size_of_partition(AvbOps* ops, 263 const char* partition, 264 uint64_t* out_size) override; 265 266 AvbIOResult read_persistent_value(const char* name, 267 size_t buffer_size, 268 uint8_t* out_buffer, 269 size_t* out_num_bytes_read) override; 270 271 AvbIOResult write_persistent_value(const char* name, 272 size_t value_size, 273 const uint8_t* value) override; 274 275 AvbIOResult validate_public_key_for_partition( 276 AvbOps* ops, 277 const char* partition, 278 const uint8_t* public_key_data, 279 size_t public_key_length, 280 const uint8_t* public_key_metadata, 281 size_t public_key_metadata_length, 282 bool* out_is_trusted, 283 uint32_t* out_rollback_index_location) override; 284 285 AvbIOResult read_permanent_attributes( 286 AvbAtxPermanentAttributes* attributes) override; 287 288 AvbIOResult read_permanent_attributes_hash( 289 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override; 290 291 void set_key_version(size_t rollback_index_location, 292 uint64_t key_version) override; 293 294 AvbIOResult get_random(size_t num_bytes, uint8_t* output) override; 295 296 private: 297 AvbOps avb_ops_; 298 AvbABOps avb_ab_ops_; 299 AvbAtxOps avb_atx_ops_; 300 301 FakeAvbOpsDelegate* delegate_; 302 303 base::FilePath partition_dir_; 304 305 std::string expected_public_key_; 306 std::string expected_public_key_metadata_; 307 308 std::map<std::string, std::string> expected_public_key_for_partition_map_; 309 310 std::map<std::string, uint32_t> rollback_index_location_for_partition_map_; 311 312 std::map<size_t, uint64_t> stored_rollback_indexes_; 313 std::map<size_t, uint64_t> verified_rollback_indexes_; 314 315 bool stored_is_device_unlocked_; 316 317 AvbAtxPermanentAttributes permanent_attributes_; 318 std::string permanent_attributes_hash_; 319 320 std::set<std::string> partition_names_read_from_; 321 std::map<std::string, uint8_t*> preloaded_partitions_; 322 std::map<std::string, std::pair<uint8_t*, size_t>> 323 preallocated_preloaded_partitions_; 324 std::set<std::string> hidden_partitions_; 325 326 std::map<std::string, std::string> stored_values_; 327 }; 328 329 // A delegate implementation that calls FakeAvbOps by default. 330 class FakeAvbOpsDelegateWithDefaults : public FakeAvbOpsDelegate { 331 public: read_from_partition(const char * partition,int64_t offset,size_t num_bytes,void * buffer,size_t * out_num_read)332 AvbIOResult read_from_partition(const char* partition, 333 int64_t offset, 334 size_t num_bytes, 335 void* buffer, 336 size_t* out_num_read) override { 337 return ops_.read_from_partition( 338 partition, offset, num_bytes, buffer, out_num_read); 339 } 340 get_preloaded_partition(const char * partition,size_t num_bytes,uint8_t ** out_pointer,size_t * out_num_bytes_preloaded)341 AvbIOResult get_preloaded_partition( 342 const char* partition, 343 size_t num_bytes, 344 uint8_t** out_pointer, 345 size_t* out_num_bytes_preloaded) override { 346 return ops_.get_preloaded_partition( 347 partition, num_bytes, out_pointer, out_num_bytes_preloaded); 348 } 349 write_to_partition(const char * partition,int64_t offset,size_t num_bytes,const void * buffer)350 AvbIOResult write_to_partition(const char* partition, 351 int64_t offset, 352 size_t num_bytes, 353 const void* buffer) override { 354 return ops_.write_to_partition(partition, offset, num_bytes, buffer); 355 } 356 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)357 AvbIOResult validate_vbmeta_public_key(AvbOps* ops, 358 const uint8_t* public_key_data, 359 size_t public_key_length, 360 const uint8_t* public_key_metadata, 361 size_t public_key_metadata_length, 362 bool* out_key_is_trusted) override { 363 return ops_.validate_vbmeta_public_key(ops, 364 public_key_data, 365 public_key_length, 366 public_key_metadata, 367 public_key_metadata_length, 368 out_key_is_trusted); 369 } 370 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)371 AvbIOResult validate_public_key_for_partition( 372 AvbOps* ops, 373 const char* partition, 374 const uint8_t* public_key_data, 375 size_t public_key_length, 376 const uint8_t* public_key_metadata, 377 size_t public_key_metadata_length, 378 bool* out_key_is_trusted, 379 uint32_t* out_rollback_index_location) override { 380 return ops_.validate_public_key_for_partition(ops, 381 partition, 382 public_key_data, 383 public_key_length, 384 public_key_metadata, 385 public_key_metadata_length, 386 out_key_is_trusted, 387 out_rollback_index_location); 388 } 389 read_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t * out_rollback_index)390 AvbIOResult read_rollback_index(AvbOps* ops, 391 size_t rollback_index_slot, 392 uint64_t* out_rollback_index) override { 393 return ops_.read_rollback_index( 394 ops, rollback_index_slot, out_rollback_index); 395 } 396 write_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t rollback_index)397 AvbIOResult write_rollback_index(AvbOps* ops, 398 size_t rollback_index_slot, 399 uint64_t rollback_index) override { 400 return ops_.write_rollback_index(ops, rollback_index_slot, rollback_index); 401 } 402 read_is_device_unlocked(AvbOps * ops,bool * out_is_device_unlocked)403 AvbIOResult read_is_device_unlocked(AvbOps* ops, 404 bool* out_is_device_unlocked) override { 405 return ops_.read_is_device_unlocked(ops, out_is_device_unlocked); 406 } 407 get_unique_guid_for_partition(AvbOps * ops,const char * partition,char * guid_buf,size_t guid_buf_size)408 AvbIOResult get_unique_guid_for_partition(AvbOps* ops, 409 const char* partition, 410 char* guid_buf, 411 size_t guid_buf_size) override { 412 return ops_.get_unique_guid_for_partition( 413 ops, partition, guid_buf, guid_buf_size); 414 } 415 get_size_of_partition(AvbOps * ops,const char * partition,uint64_t * out_size)416 AvbIOResult get_size_of_partition(AvbOps* ops, 417 const char* partition, 418 uint64_t* out_size) override { 419 return ops_.get_size_of_partition(ops, partition, out_size); 420 } 421 read_persistent_value(const char * name,size_t buffer_size,uint8_t * out_buffer,size_t * out_num_bytes_read)422 AvbIOResult read_persistent_value(const char* name, 423 size_t buffer_size, 424 uint8_t* out_buffer, 425 size_t* out_num_bytes_read) override { 426 return ops_.read_persistent_value( 427 name, buffer_size, out_buffer, out_num_bytes_read); 428 } 429 write_persistent_value(const char * name,size_t value_size,const uint8_t * value)430 AvbIOResult write_persistent_value(const char* name, 431 size_t value_size, 432 const uint8_t* value) override { 433 return ops_.write_persistent_value(name, value_size, value); 434 } 435 read_permanent_attributes(AvbAtxPermanentAttributes * attributes)436 AvbIOResult read_permanent_attributes( 437 AvbAtxPermanentAttributes* attributes) override { 438 return ops_.read_permanent_attributes(attributes); 439 } 440 read_permanent_attributes_hash(uint8_t hash[AVB_SHA256_DIGEST_SIZE])441 AvbIOResult read_permanent_attributes_hash( 442 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override { 443 return ops_.read_permanent_attributes_hash(hash); 444 } 445 set_key_version(size_t rollback_index_location,uint64_t key_version)446 void set_key_version(size_t rollback_index_location, 447 uint64_t key_version) override { 448 ops_.set_key_version(rollback_index_location, key_version); 449 } 450 get_random(size_t num_bytes,uint8_t * output)451 AvbIOResult get_random(size_t num_bytes, uint8_t* output) override { 452 return ops_.get_random(num_bytes, output); 453 } 454 455 protected: 456 FakeAvbOps ops_; 457 }; 458 459 } // namespace avb 460 461 #endif /* FAKE_AVB_OPS_H_ */ 462