1 // Copyright 2020 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_ 6 #define COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "components/sync/nigori/nigori.h" 13 14 namespace sync_pb { 15 16 class BookmarkSpecifics; 17 class NigoriSpecifics; 18 class EntitySpecifics; 19 20 } // namespace sync_pb 21 22 namespace syncer { 23 24 class Cryptographer; 25 26 struct KeyParamsForTesting { 27 KeyDerivationParams derivation_params; 28 std::string password; 29 }; 30 31 // Creates KeyParamsForTesting, where |derivation_params| is Pbkdf2 32 // KeyDerivationParams and |password| is base64 encoded |raw_key|. 33 KeyParamsForTesting Pbkdf2KeyParamsForTesting( 34 const std::vector<uint8_t>& raw_key); 35 36 // Builds NigoriSpecifics with following fields: 37 // 1. encryption_keybag contains all keys derived from |keybag_keys_params| 38 // and encrypted with a key derived from |keybag_decryptor_params|. 39 // 2. keystore_decryptor_token contains the key derived from 40 // |keybag_decryptor_params| and encrypted with a key derived from 41 // |keystore_key_params|. 42 // 3. passphrase_type is KEYSTORE_PASSHPRASE. 43 // 4. Other fields are default. 44 // |keybag_keys_params| must be non-empty. 45 sync_pb::NigoriSpecifics BuildKeystoreNigoriSpecifics( 46 const std::vector<KeyParamsForTesting>& keybag_keys_params, 47 const KeyParamsForTesting& keystore_decryptor_params, 48 const KeyParamsForTesting& keystore_key_params); 49 50 // Builds NigoriSpecifics with following fields: 51 // 1. encryption_keybag contains keys derived from |trusted_vault_keys| and 52 // encrypted with key derived from last of them. 53 // 2. passphrase_type is TRUSTED_VAULT_PASSPHRASE. 54 // 3. keybag_is_frozen set to true. 55 sync_pb::NigoriSpecifics BuildTrustedVaultNigoriSpecifics( 56 const std::vector<std::vector<uint8_t>>& trusted_vault_keys); 57 58 // Creates a NigoriSpecifics that describes encryption using a custom 59 // passphrase with the given |passphrase_key_params|. If |old_key_params| is 60 // presented, |encryption_keybag| will also contain keys derived from it. 61 sync_pb::NigoriSpecifics CreateCustomPassphraseNigori( 62 const KeyParamsForTesting& passphrase_key_params, 63 const base::Optional<KeyParamsForTesting>& old_key_params = base::nullopt); 64 65 // Given a |nigori| with CUSTOM_PASSPHRASE passphrase type, initializes the 66 // given |cryptographer| with the key described in it. Since the key inside the 67 // Nigori is encrypted (by design), the provided |passphrase| will be used to 68 // decrypt it. This function will fail the test (using ASSERT) if the Nigori is 69 // not a custom passphrase one, or if the key cannot be decrypted. 70 std::unique_ptr<Cryptographer> InitCustomPassphraseCryptographerFromNigori( 71 const sync_pb::NigoriSpecifics& nigori, 72 const std::string& passphrase); 73 74 // Returns an EntitySpecifics containing encrypted data corresponding to the 75 // provided BookmarkSpecifics and encrypted using the given |key_params|. 76 sync_pb::EntitySpecifics GetEncryptedBookmarkEntitySpecifics( 77 const sync_pb::BookmarkSpecifics& specifics, 78 const KeyParamsForTesting& key_params); 79 80 } // namespace syncer 81 82 #endif // COMPONENTS_SYNC_NIGORI_NIGORI_TEST_UTILS_H_ 83