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 #include "components/encryptor/encryptor.h" | 5 #include "components/encryptor/os_crypt.h" |
6 | 6 |
7 #include <CommonCrypto/CommonCryptor.h> // for kCCBlockSizeAES128 | 7 #include <CommonCrypto/CommonCryptor.h> // for kCCBlockSizeAES128 |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "components/encryptor/encryptor_password_mac.h" | |
14 #include "components/encryptor/encryptor_switches.h" | 13 #include "components/encryptor/encryptor_switches.h" |
| 14 #include "components/encryptor/keychain_password_mac.h" |
15 #include "crypto/apple_keychain.h" | 15 #include "crypto/apple_keychain.h" |
16 #include "crypto/encryptor.h" | 16 #include "crypto/encryptor.h" |
17 #include "crypto/symmetric_key.h" | 17 #include "crypto/symmetric_key.h" |
18 | 18 |
19 using crypto::AppleKeychain; | 19 using crypto::AppleKeychain; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Salt for Symmetric key derivation. | 23 // Salt for Symmetric key derivation. |
24 const char kSalt[] = "saltysalt"; | 24 const char kSalt[] = "saltysalt"; |
(...skipping 19 matching lines...) Expand all Loading... |
44 crypto::SymmetricKey* GetEncryptionKey() { | 44 crypto::SymmetricKey* GetEncryptionKey() { |
45 static bool mock_keychain_command_line_flag = | 45 static bool mock_keychain_command_line_flag = |
46 CommandLine::ForCurrentProcess()->HasSwitch( | 46 CommandLine::ForCurrentProcess()->HasSwitch( |
47 encryptor::switches::kUseMockKeychain); | 47 encryptor::switches::kUseMockKeychain); |
48 | 48 |
49 std::string password; | 49 std::string password; |
50 if (use_mock_keychain || mock_keychain_command_line_flag) { | 50 if (use_mock_keychain || mock_keychain_command_line_flag) { |
51 password = "mock_password"; | 51 password = "mock_password"; |
52 } else { | 52 } else { |
53 AppleKeychain keychain; | 53 AppleKeychain keychain; |
54 EncryptorPassword encryptor_password(keychain); | 54 KeychainPassword encryptor_password(keychain); |
55 password = encryptor_password.GetEncryptorPassword(); | 55 password = encryptor_password.GetPassword(); |
56 } | 56 } |
57 | 57 |
58 if (password.empty()) | 58 if (password.empty()) |
59 return NULL; | 59 return NULL; |
60 | 60 |
61 std::string salt(kSalt); | 61 std::string salt(kSalt); |
62 | 62 |
63 // Create an encryption key from our password and salt. | 63 // Create an encryption key from our password and salt. |
64 scoped_ptr<crypto::SymmetricKey> encryption_key( | 64 scoped_ptr<crypto::SymmetricKey> encryption_key( |
65 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, | 65 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, |
66 password, | 66 password, |
67 salt, | 67 salt, |
68 kEncryptionIterations, | 68 kEncryptionIterations, |
69 kDerivedKeySizeInBits)); | 69 kDerivedKeySizeInBits)); |
70 DCHECK(encryption_key.get()); | 70 DCHECK(encryption_key.get()); |
71 | 71 |
72 return encryption_key.release(); | 72 return encryption_key.release(); |
73 } | 73 } |
74 | 74 |
75 } // namespace | 75 } // namespace |
76 | 76 |
77 bool Encryptor::EncryptString16(const base::string16& plaintext, | 77 bool OSCrypt::EncryptString16(const base::string16& plaintext, |
78 std::string* ciphertext) { | 78 std::string* ciphertext) { |
79 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); | 79 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); |
80 } | 80 } |
81 | 81 |
82 bool Encryptor::DecryptString16(const std::string& ciphertext, | 82 bool OSCrypt::DecryptString16(const std::string& ciphertext, |
83 base::string16* plaintext) { | 83 base::string16* plaintext) { |
84 std::string utf8; | 84 std::string utf8; |
85 if (!DecryptString(ciphertext, &utf8)) | 85 if (!DecryptString(ciphertext, &utf8)) |
86 return false; | 86 return false; |
87 | 87 |
88 *plaintext = base::UTF8ToUTF16(utf8); | 88 *plaintext = base::UTF8ToUTF16(utf8); |
89 return true; | 89 return true; |
90 } | 90 } |
91 | 91 |
92 bool Encryptor::EncryptString(const std::string& plaintext, | 92 bool OSCrypt::EncryptString(const std::string& plaintext, |
93 std::string* ciphertext) { | 93 std::string* ciphertext) { |
94 if (plaintext.empty()) { | 94 if (plaintext.empty()) { |
95 *ciphertext = std::string(); | 95 *ciphertext = std::string(); |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); | 99 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
100 if (!encryption_key.get()) | 100 if (!encryption_key.get()) |
101 return false; | 101 return false; |
102 | 102 |
103 std::string iv(kCCBlockSizeAES128, ' '); | 103 std::string iv(kCCBlockSizeAES128, ' '); |
104 crypto::Encryptor encryptor; | 104 crypto::Encryptor encryptor; |
105 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 105 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
106 return false; | 106 return false; |
107 | 107 |
108 if (!encryptor.Encrypt(plaintext, ciphertext)) | 108 if (!encryptor.Encrypt(plaintext, ciphertext)) |
109 return false; | 109 return false; |
110 | 110 |
111 // Prefix the cypher text with version information. | 111 // Prefix the cypher text with version information. |
112 ciphertext->insert(0, kEncryptionVersionPrefix); | 112 ciphertext->insert(0, kEncryptionVersionPrefix); |
113 return true; | 113 return true; |
114 } | 114 } |
115 | 115 |
116 bool Encryptor::DecryptString(const std::string& ciphertext, | 116 bool OSCrypt::DecryptString(const std::string& ciphertext, |
117 std::string* plaintext) { | 117 std::string* plaintext) { |
118 if (ciphertext.empty()) { | 118 if (ciphertext.empty()) { |
119 *plaintext = std::string(); | 119 *plaintext = std::string(); |
120 return true; | 120 return true; |
121 } | 121 } |
122 | 122 |
123 // Check that the incoming cyphertext was indeed encrypted with the expected | 123 // Check that the incoming cyphertext was indeed encrypted with the expected |
124 // version. If the prefix is not found then we'll assume we're dealing with | 124 // version. If the prefix is not found then we'll assume we're dealing with |
125 // old data saved as clear text and we'll return it directly. | 125 // old data saved as clear text and we'll return it directly. |
126 // Credit card numbers are current legacy data, so false match with prefix | 126 // Credit card numbers are current legacy data, so false match with prefix |
127 // won't happen. | 127 // won't happen. |
(...skipping 14 matching lines...) Expand all Loading... |
142 crypto::Encryptor encryptor; | 142 crypto::Encryptor encryptor; |
143 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 143 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
144 return false; | 144 return false; |
145 | 145 |
146 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) | 146 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
147 return false; | 147 return false; |
148 | 148 |
149 return true; | 149 return true; |
150 } | 150 } |
151 | 151 |
152 void Encryptor::UseMockKeychain(bool use_mock) { | 152 void OSCrypt::UseMockKeychain(bool use_mock) { |
153 use_mock_keychain = use_mock; | 153 use_mock_keychain = use_mock; |
154 } | 154 } |
155 | 155 |
OLD | NEW |