OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_OS_CRYPT_OS_CRYPT_H_ | 5 #ifndef COMPONENTS_OS_CRYPT_OS_CRYPT_H_ |
6 #define COMPONENTS_OS_CRYPT_OS_CRYPT_H_ | 6 #define COMPONENTS_OS_CRYPT_OS_CRYPT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 | 13 |
14 #if defined(USE_LIBSECRET) | 14 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
15 #include "components/os_crypt/key_storage_linux.h" | 15 #include "components/os_crypt/key_storage_linux.h" |
16 #endif | 16 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
17 | 17 |
18 // The OSCrypt class gives access to simple encryption and decryption of | 18 // The OSCrypt class gives access to simple encryption and decryption of |
19 // strings. Note that on Mac, access to the system Keychain is required and | 19 // strings. Note that on Mac, access to the system Keychain is required and |
20 // these calls can block the current thread to collect user input. The same is | 20 // these calls can block the current thread to collect user input. The same is |
21 // true for Linux, if a password management tool is available. | 21 // true for Linux, if a password management tool is available. |
22 class OSCrypt { | 22 class OSCrypt { |
23 public: | 23 public: |
| 24 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 25 // If |store_type| is a known password store, we will attempt to use it. |
| 26 // In any other case, we default to auto-detecting the store. |
| 27 // This should not be changed after OSCrypt has been used. |
| 28 static void SetStore(const std::string& store_type); |
| 29 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 30 |
24 // Encrypt a string16. The output (second argument) is really an array of | 31 // Encrypt a string16. The output (second argument) is really an array of |
25 // bytes, but we're passing it back as a std::string. | 32 // bytes, but we're passing it back as a std::string. |
26 static bool EncryptString16(const base::string16& plaintext, | 33 static bool EncryptString16(const base::string16& plaintext, |
27 std::string* ciphertext); | 34 std::string* ciphertext); |
28 | 35 |
29 // Decrypt an array of bytes obtained with EncryptString16 back into a | 36 // Decrypt an array of bytes obtained with EncryptString16 back into a |
30 // string16. Note that the input (first argument) is a std::string, so you | 37 // string16. Note that the input (first argument) is a std::string, so you |
31 // need to first get your (binary) data into a string. | 38 // need to first get your (binary) data into a string. |
32 static bool DecryptString16(const std::string& ciphertext, | 39 static bool DecryptString16(const std::string& ciphertext, |
33 base::string16* plaintext); | 40 base::string16* plaintext); |
(...skipping 11 matching lines...) Expand all Loading... |
45 #if defined(OS_MACOSX) | 52 #if defined(OS_MACOSX) |
46 // For unit testing purposes we instruct the Encryptor to use a mock Keychain | 53 // For unit testing purposes we instruct the Encryptor to use a mock Keychain |
47 // on the Mac. The default is to use the real Keychain. | 54 // on the Mac. The default is to use the real Keychain. |
48 static void UseMockKeychain(bool use_mock); | 55 static void UseMockKeychain(bool use_mock); |
49 #endif | 56 #endif |
50 | 57 |
51 private: | 58 private: |
52 DISALLOW_IMPLICIT_CONSTRUCTORS(OSCrypt); | 59 DISALLOW_IMPLICIT_CONSTRUCTORS(OSCrypt); |
53 }; | 60 }; |
54 | 61 |
55 #if (defined(USE_LIBSECRET) || defined(USE_KWALLET)) && defined(UNIT_TEST) | 62 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(UNIT_TEST) |
56 // For unit testing purposes, inject methods to be used. | 63 // For unit testing purposes, inject methods to be used. |
57 // |get_key_storage_mock| provides the desired |KeyStorage| implementation. | 64 // |get_key_storage_mock| provides the desired |KeyStorage| implementation. |
58 // If the provider returns |nullptr|, a hardcoded password will be used. | 65 // If the provider returns |nullptr|, a hardcoded password will be used. |
59 // |get_password_v11_mock| provides a password to derive the encryption key from | 66 // |get_password_v11_mock| provides a password to derive the encryption key from |
60 // If both parameters are |nullptr|, the real implementation is restored. | 67 // If both parameters are |nullptr|, the real implementation is restored. |
61 void UseMockKeyStorageForTesting(KeyStorageLinux* (*get_key_storage_mock)(), | 68 void UseMockKeyStorageForTesting(KeyStorageLinux* (*get_key_storage_mock)(), |
62 std::string* (*get_password_v11_mock)()); | 69 std::string* (*get_password_v11_mock)()); |
63 #endif // (defined(USE_LIBSECRET) || defined(USE_KWALLET)) && | 70 #endif // defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(UNIT_TEST) |
64 // defined(UNIT_TEST) | |
65 | 71 |
66 #endif // COMPONENTS_OS_CRYPT_OS_CRYPT_H_ | 72 #endif // COMPONENTS_OS_CRYPT_OS_CRYPT_H_ |
OLD | NEW |