OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "components/os_crypt/os_crypt.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include <algorithm> |
| 10 #include <iterator> |
| 11 #include <memory> |
| 12 |
| 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "components/os_crypt/key_storage_linux.h" |
| 18 #include "crypto/encryptor.h" |
| 19 #include "crypto/symmetric_key.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Salt for Symmetric key derivation. |
| 24 const char kSalt[] = "saltysalt"; |
| 25 |
| 26 // Key size required for 128 bit AES. |
| 27 const size_t kDerivedKeySizeInBits = 128; |
| 28 |
| 29 // Constant for Symmetic key derivation. |
| 30 const size_t kEncryptionIterations = 1; |
| 31 |
| 32 // Size of initialization vector for AES 128-bit. |
| 33 const size_t kIVBlockSizeAES128 = 16; |
| 34 |
| 35 // Password version. V10 means that the hardcoded password will be used. |
| 36 // V11 means that a password is/will be stored using an OS-level library (e.g |
| 37 // Libsecret). V11 will not be used if such a library is not available. |
| 38 // Used for array indexing. |
| 39 enum Version { |
| 40 V10 = 0, |
| 41 V11 = 1, |
| 42 }; |
| 43 |
| 44 // Prefix for cipher text returned by obfuscation version. We prefix the |
| 45 // ciphertext with this string so that future data migration can detect |
| 46 // this and migrate to full encryption without data loss. |
| 47 const char kObfuscationPrefix[][4] = { |
| 48 "v10", "v11", |
| 49 }; |
| 50 |
| 51 struct Cache { |
| 52 std::unique_ptr<KeyStorageLinux> key_storage_cache; |
| 53 std::unique_ptr<std::string> password_v10_cache; |
| 54 std::unique_ptr<std::string> password_v11_cache; |
| 55 bool is_key_storage_cached; |
| 56 bool is_password_v11_cached; |
| 57 }; |
| 58 |
| 59 base::LazyInstance<Cache>::Leaky g_cache = LAZY_INSTANCE_INITIALIZER; |
| 60 |
| 61 // Lazy acquisition and caching of a KeyStorage. Will be null if no service is |
| 62 // found. |
| 63 KeyStorageLinux* GetKeyStorage() { |
| 64 if (!g_cache.Get().is_key_storage_cached) { |
| 65 g_cache.Get().is_key_storage_cached = true; |
| 66 g_cache.Get().key_storage_cache = KeyStorageLinux::CreateService(); |
| 67 } |
| 68 return g_cache.Get().key_storage_cache.get(); |
| 69 } |
| 70 |
| 71 // Returns a cached string of "peanuts". |
| 72 std::string* GetPasswordV10() { |
| 73 if (!g_cache.Get().password_v10_cache.get()) |
| 74 g_cache.Get().password_v10_cache.reset(new std::string("peanuts")); |
| 75 return g_cache.Get().password_v10_cache.get(); |
| 76 } |
| 77 |
| 78 // Caches and returns the password from the KeyStorage or null if there is no |
| 79 // service. |
| 80 std::string* GetPasswordV11() { |
| 81 if (!g_cache.Get().is_password_v11_cached) { |
| 82 g_cache.Get().is_password_v11_cached = true; |
| 83 g_cache.Get().password_v11_cache.reset( |
| 84 GetKeyStorage() ? new std::string(GetKeyStorage()->GetKey()) : nullptr); |
| 85 } |
| 86 return g_cache.Get().password_v11_cache.get(); |
| 87 } |
| 88 |
| 89 // Pointer to a function that creates and returns the |KeyStorage| instance to |
| 90 // be used. The function maintains ownership of the pointer. |
| 91 KeyStorageLinux* (*g_key_storage_provider)() = &GetKeyStorage; |
| 92 |
| 93 // Pointers to functions that return a password for deriving the encryption key. |
| 94 // One function for each supported password version (see Version enum). |
| 95 std::string* (*g_get_password[])() = { |
| 96 &GetPasswordV10, &GetPasswordV11, |
| 97 }; |
| 98 |
| 99 // Generates a newly allocated SymmetricKey object based on a password. |
| 100 // Ownership of the key is passed to the caller. Returns null key if a key |
| 101 // generation error occurs. |
| 102 std::unique_ptr<crypto::SymmetricKey> GetEncryptionKey(Version version) { |
| 103 std::string salt(kSalt); |
| 104 |
| 105 std::string* password = g_get_password[version](); |
| 106 if (!password) |
| 107 return nullptr; |
| 108 |
| 109 // Create an encryption key from our password and salt. |
| 110 std::unique_ptr<crypto::SymmetricKey> encryption_key( |
| 111 crypto::SymmetricKey::DeriveKeyFromPassword( |
| 112 crypto::SymmetricKey::AES, *password, salt, kEncryptionIterations, |
| 113 kDerivedKeySizeInBits)); |
| 114 DCHECK(encryption_key); |
| 115 |
| 116 return encryption_key; |
| 117 } |
| 118 |
| 119 } // namespace |
| 120 |
| 121 // static |
| 122 bool OSCrypt::EncryptString16(const base::string16& plaintext, |
| 123 std::string* ciphertext) { |
| 124 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); |
| 125 } |
| 126 |
| 127 // static |
| 128 bool OSCrypt::DecryptString16(const std::string& ciphertext, |
| 129 base::string16* plaintext) { |
| 130 std::string utf8; |
| 131 if (!DecryptString(ciphertext, &utf8)) |
| 132 return false; |
| 133 |
| 134 *plaintext = base::UTF8ToUTF16(utf8); |
| 135 return true; |
| 136 } |
| 137 |
| 138 // static |
| 139 bool OSCrypt::EncryptString(const std::string& plaintext, |
| 140 std::string* ciphertext) { |
| 141 if (plaintext.empty()) { |
| 142 ciphertext->clear(); |
| 143 return true; |
| 144 } |
| 145 |
| 146 // If a |KeyStorage| is available, use a password backed by the |KeyStorage|. |
| 147 // Otherwise use the hardcoded password. |
| 148 Version version = g_key_storage_provider() ? Version::V11 : Version::V10; |
| 149 |
| 150 std::unique_ptr<crypto::SymmetricKey> encryption_key( |
| 151 GetEncryptionKey(version)); |
| 152 if (!encryption_key) |
| 153 return false; |
| 154 |
| 155 std::string iv(kIVBlockSizeAES128, ' '); |
| 156 crypto::Encryptor encryptor; |
| 157 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
| 158 return false; |
| 159 |
| 160 if (!encryptor.Encrypt(plaintext, ciphertext)) |
| 161 return false; |
| 162 |
| 163 // Prefix the cipher text with version information. |
| 164 ciphertext->insert(0, kObfuscationPrefix[version]); |
| 165 return true; |
| 166 } |
| 167 |
| 168 // static |
| 169 bool OSCrypt::DecryptString(const std::string& ciphertext, |
| 170 std::string* plaintext) { |
| 171 if (ciphertext.empty()) { |
| 172 plaintext->clear(); |
| 173 return true; |
| 174 } |
| 175 |
| 176 // Check that the incoming ciphertext was encrypted and with what version. |
| 177 // Credit card numbers are current legacy unencrypted data, so false match |
| 178 // with prefix won't happen. |
| 179 Version version; |
| 180 if (base::StartsWith(ciphertext, kObfuscationPrefix[Version::V10], |
| 181 base::CompareCase::SENSITIVE)) { |
| 182 version = Version::V10; |
| 183 } else if (base::StartsWith(ciphertext, kObfuscationPrefix[Version::V11], |
| 184 base::CompareCase::SENSITIVE)) { |
| 185 version = Version::V11; |
| 186 } else { |
| 187 // If the prefix is not found then we'll assume we're dealing with |
| 188 // old data saved as clear text and we'll return it directly. |
| 189 *plaintext = ciphertext; |
| 190 return true; |
| 191 } |
| 192 |
| 193 std::unique_ptr<crypto::SymmetricKey> encryption_key( |
| 194 GetEncryptionKey(version)); |
| 195 if (!encryption_key) |
| 196 return false; |
| 197 |
| 198 std::string iv(kIVBlockSizeAES128, ' '); |
| 199 crypto::Encryptor encryptor; |
| 200 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
| 201 return false; |
| 202 |
| 203 // Strip off the versioning prefix before decrypting. |
| 204 std::string raw_ciphertext = |
| 205 ciphertext.substr(strlen(kObfuscationPrefix[version])); |
| 206 |
| 207 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
| 208 return false; |
| 209 |
| 210 return true; |
| 211 } |
| 212 |
| 213 void UseMockKeyStorageForTesting(KeyStorageLinux* (*get_key_storage_mock)(), |
| 214 std::string* (*get_password_v11_mock)()) { |
| 215 // Save the real implementation to restore it later. |
| 216 static bool is_get_password_saved = false; |
| 217 static std::string* (*get_password_save[arraysize(g_get_password)])(); |
| 218 if (!is_get_password_saved) { |
| 219 std::copy(std::begin(g_get_password), std::end(g_get_password), |
| 220 std::begin(get_password_save)); |
| 221 is_get_password_saved = true; |
| 222 } |
| 223 |
| 224 if (get_key_storage_mock && get_password_v11_mock) { |
| 225 // Bypass calling KeyStorage::CreateService and caching of the key for V11 |
| 226 g_get_password[Version::V11] = get_password_v11_mock; |
| 227 // OSCrypt will determine the encryption version by checking if a |
| 228 // |KeyStorage| instance can be created. Enable V11 by returning the mock. |
| 229 g_key_storage_provider = get_key_storage_mock; |
| 230 } else { |
| 231 // Restore real implementation |
| 232 std::copy(std::begin(get_password_save), std::end(get_password_save), |
| 233 std::begin(g_get_password)); |
| 234 } |
| 235 } |
OLD | NEW |