Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 1 | // Copyright 2018 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_OS_CRYPT_ENCRYPTION_KEY_CREATION_UTIL_H_ |
| 6 | #define COMPONENTS_OS_CRYPT_ENCRYPTION_KEY_CREATION_UTIL_H_ |
| 7 | |
| 8 | #include "base/component_export.h" |
| 9 | |
Vasilii Sukhanov | 5bb3b468 | 2018-12-04 12:00:04 | [diff] [blame] | 10 | namespace crypto { |
| 11 | class AppleKeychain; |
| 12 | } |
| 13 | |
Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 14 | namespace os_crypt { |
| 15 | |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 16 | // An interface for the utility that logs statistics on the encryption key on |
Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 17 | // Mac. |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 18 | // This class is used on Mac and iOS, but does nothing on iOS. The object for |
Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 19 | // the Mac class has to be created on the main thread. |
| 20 | class EncryptionKeyCreationUtil { |
| 21 | public: |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 22 | // The action that is taken by KeychainPassword::GetPassword method. |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 23 | // This enum is used for reporting metrics. |
| 24 | enum class GetKeyAction { |
| 25 | // Key was found in the Keychain and the preference that it was created in |
| 26 | // the past is set. |
| 27 | kKeyFound = 0, |
| 28 | // Key was found in the Keychain, but the preference that it was created in |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 29 | // the past was not set. |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 30 | kKeyFoundFirstTime = 1, |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 31 | kOverwritingPrevented_OBSOLETE = 2, |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 32 | // Key was added to the Keychain and the preference is set. |
| 33 | kNewKeyAddedToKeychain = 3, |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 34 | // Some other error occurred during lookup. |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 35 | kKeychainLookupFailed = 4, |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 36 | // The preference was set but a new key was added to the Keychain. |
| 37 | kKeyPotentiallyOverwritten = 5, |
| 38 | // The preference was set but a new key was not created due to an error. |
| 39 | kKeyOverwriteFailed = 6, |
| 40 | // A new key should be created but an error occured. |
| 41 | kNewKeyAddError = 7, |
| 42 | kMaxValue = kNewKeyAddError, |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 43 | }; |
| 44 | |
Vasilii Sukhanov | 5bb3b468 | 2018-12-04 12:00:04 | [diff] [blame] | 45 | // Result of FindGenericPassword. This enum is used for reporting metrics. |
| 46 | // These values are persisted to logs. Entries should not be renumbered and |
| 47 | // numeric values should never be reused. |
| 48 | enum class FindPasswordResult { |
| 49 | kOtherError = 0, |
| 50 | kFound = 1, |
| 51 | kNotFound = 2, |
| 52 | kMaxValue = kNotFound, |
| 53 | }; |
| 54 | |
Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 55 | virtual ~EncryptionKeyCreationUtil() = default; |
| 56 | |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 57 | // This method is called when the encryption key is successfully retrieved |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 58 | // from the Keychain. If this is called for the very first time, it |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 59 | // asynchronously updates the preference on the main thread that the key was |
| 60 | // created. This method doesn't need to be called on the main thread. |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 61 | virtual void OnKeyWasFound() = 0; |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 62 | |
Vasilii Sukhanov | 5bb3b468 | 2018-12-04 12:00:04 | [diff] [blame] | 63 | // Called when the encryption key was not in the Keychain just before a new |
| 64 | // key is stored. This method doesn't need to be called on the main thread. |
| 65 | virtual void OnKeyNotFound(const crypto::AppleKeychain& keychain) = 0; |
| 66 | |
Vasilii Sukhanov | f1ab938 | 2018-11-22 15:54:53 | [diff] [blame] | 67 | // Called when the encryption key was not in the Keychain. |new_key_stored| |
| 68 | // is true iff a new key was stored successfully. This method doesn't need to |
| 69 | // be called on the main thread. |
Vasilii Sukhanov | 5bb3b468 | 2018-12-04 12:00:04 | [diff] [blame] | 70 | virtual void OnKeyStored(bool new_key_stored) = 0; |
Tonko Sabolčec | b32d055 | 2018-09-13 14:27:13 | [diff] [blame] | 71 | |
| 72 | // This method is called when the Keychain returns error other than |
| 73 | // errSecItemNotFound (e.g., user is not authorized to use Keychain, or |
| 74 | // Keychain is unavailable for some other reasons). |
Vasilii Sukhanov | 5bb3b468 | 2018-12-04 12:00:04 | [diff] [blame] | 75 | virtual void OnKeychainLookupFailed(int error) = 0; |
Tonko Sabolčec | 43615a9 | 2018-09-12 12:43:41 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace os_crypt |
| 79 | |
| 80 | #endif // COMPONENTS_OS_CRYPT_ENCRYPTION_KEY_CREATION_UTIL_H_ |