keystore_keys_cryptographer.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2019 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. #ifndef COMPONENTS_SYNC_NIGORI_KEYSTORE_KEYS_CRYPTOGRAPHER_H_
  5. #define COMPONENTS_SYNC_NIGORI_KEYSTORE_KEYS_CRYPTOGRAPHER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. namespace sync_pb {
  10. class EncryptedData;
  11. class NigoriKey;
  12. } // namespace sync_pb
  13. namespace syncer {
  14. class CryptographerImpl;
  15. // Wrapper of CryptographerImpl, which contains only keystore keys and uses the
  16. // last one as the default encryption key.
  17. class KeystoreKeysCryptographer {
  18. public:
  19. // Factory methods.
  20. static std::unique_ptr<KeystoreKeysCryptographer> CreateEmpty();
  21. // Returns null if crypto error occurs.
  22. static std::unique_ptr<KeystoreKeysCryptographer> FromKeystoreKeys(
  23. const std::vector<std::string>& keystore_keys);
  24. KeystoreKeysCryptographer(const KeystoreKeysCryptographer&) = delete;
  25. KeystoreKeysCryptographer& operator=(const KeystoreKeysCryptographer&) =
  26. delete;
  27. ~KeystoreKeysCryptographer();
  28. const std::vector<std::string>& keystore_keys() const {
  29. return keystore_keys_;
  30. }
  31. // Returns name of Nigori key derived from last keystore key if !IsEmpty()
  32. // and empty string otherwise.
  33. std::string GetLastKeystoreKeyName() const;
  34. bool IsEmpty() const;
  35. std::unique_ptr<KeystoreKeysCryptographer> Clone() const;
  36. // Returns CryptographerImpl, which contains all keystore keys and uses the
  37. // last one as the default encryption key.
  38. std::unique_ptr<CryptographerImpl> ToCryptographerImpl() const;
  39. // Encrypts |keystore_decryptor_key| into |keystore_decryptor_token|.
  40. // |keystore_decryptor_token| must be not null. Returns false if there is no
  41. // keystore keys or crypto error occurs.
  42. bool EncryptKeystoreDecryptorToken(
  43. const sync_pb::NigoriKey& keystore_decryptor_key,
  44. sync_pb::EncryptedData* keystore_decryptor_token) const;
  45. // Decrypts |keystore_decryptor_token| into |keystore_decryptor_key|.
  46. // |keystore_decryptor_key| must be not null. Returns false if can't decrypt
  47. // or crypto error occurs.
  48. bool DecryptKeystoreDecryptorToken(
  49. const sync_pb::EncryptedData& keystore_decryptor_token,
  50. sync_pb::NigoriKey* keystore_decryptor_key) const;
  51. private:
  52. KeystoreKeysCryptographer(std::unique_ptr<CryptographerImpl> cryptographer,
  53. const std::vector<std::string>& keystore_keys);
  54. std::unique_ptr<CryptographerImpl> cryptographer_;
  55. std::vector<std::string> keystore_keys_;
  56. };
  57. } // namespace syncer
  58. #endif // COMPONENTS_SYNC_NIGORI_KEYSTORE_KEYS_CRYPTOGRAPHER_H_