[email protected] | 908cb9e | 2014-02-21 15:40:09 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | c523d20 | 2014-03-18 01:34:54 | [diff] [blame] | 5 | #include "components/os_crypt/ie7_password_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1cf4a78 | 2010-06-22 20:36:16 | [diff] [blame] | 11 | #include "base/sha1.h" |
[email protected] | c72674b | 2013-06-11 04:16:43 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/stringprintf.h" |
davidben | c6ac260 | 2014-10-30 00:39:38 | [diff] [blame^] | 14 | #include "crypto/wincrypt_shim.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // Structures that IE7/IE8 use to store a username/password. |
| 19 | // Some of the fields might have been incorrectly reverse engineered. |
| 20 | struct PreHeader { |
| 21 | DWORD pre_header_size; // Size of this header structure. Always 12. |
| 22 | DWORD header_size; // Size of the real Header: sizeof(Header) + |
| 23 | // item_count * sizeof(Entry); |
| 24 | DWORD data_size; // Size of the data referenced by the entries. |
| 25 | }; |
| 26 | |
| 27 | struct Header { |
| 28 | char wick[4]; // The string "WICK". I don't know what it means. |
| 29 | DWORD fixed_header_size; // The size of this structure without the entries: |
| 30 | // sizeof(Header). |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 31 | DWORD item_count; // Number of entries. Should be even. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | wchar_t two_letters[2]; // Two unknown bytes. |
| 33 | DWORD unknown[2]; // Two unknown DWORDs. |
| 34 | }; |
| 35 | |
| 36 | struct Entry { |
| 37 | DWORD offset; // Offset where the data referenced by this entry is |
| 38 | // located. |
| 39 | FILETIME time_stamp; // Timestamp when the password got added. |
| 40 | DWORD string_length; // The length of the data string. |
| 41 | }; |
| 42 | |
| 43 | // Main data structure. |
| 44 | struct PasswordEntry { |
| 45 | PreHeader pre_header; // Contains the size of the different sections. |
| 46 | Header header; // Contains the number of items. |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 47 | Entry entry[1]; // List of entries containing a string. Even-indexed |
| 48 | // are usernames, odd are passwords. There may be |
| 49 | // several sets saved for a single url hash. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | } // namespace |
| 52 | |
| 53 | namespace ie7_password { |
| 54 | |
| 55 | bool GetUserPassFromData(const std::vector<unsigned char>& data, |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 56 | std::vector<DecryptedCredentials>* credentials) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | const PasswordEntry* information = |
| 58 | reinterpret_cast<const PasswordEntry*>(&data.front()); |
| 59 | |
| 60 | // Some expected values. If it's not what we expect we don't even try to |
| 61 | // understand the data. |
| 62 | if (information->pre_header.pre_header_size != sizeof(PreHeader)) |
| 63 | return false; |
| 64 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 65 | const int entry_count = information->header.item_count; |
| 66 | if (entry_count % 2) // Usernames and Passwords |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | return false; |
| 68 | |
| 69 | if (information->header.fixed_header_size != sizeof(Header)) |
| 70 | return false; |
| 71 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 72 | const uint8* offset_to_data = &data[0] + |
| 73 | information->pre_header.header_size + |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | information->pre_header.pre_header_size; |
| 75 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 76 | for (int i = 0; i < entry_count / 2; ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 78 | const Entry* user_entry = &information->entry[2*i]; |
| 79 | const Entry* pass_entry = user_entry+1; |
| 80 | |
| 81 | DecryptedCredentials c; |
| 82 | c.username = reinterpret_cast<const wchar_t*>(offset_to_data + |
| 83 | user_entry->offset); |
| 84 | c.password = reinterpret_cast<const wchar_t*>(offset_to_data + |
| 85 | pass_entry->offset); |
| 86 | credentials->push_back(c); |
| 87 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| 91 | std::wstring GetUrlHash(const std::wstring& url) { |
[email protected] | cb1f4ac | 2014-08-07 16:55:42 | [diff] [blame] | 92 | std::wstring lower_case_url = base::StringToLowerASCII(url); |
[email protected] | 1cf4a78 | 2010-06-22 20:36:16 | [diff] [blame] | 93 | // Get a data buffer out of our std::wstring to pass to SHA1HashString. |
| 94 | std::string url_buffer( |
| 95 | reinterpret_cast<const char*>(lower_case_url.c_str()), |
| 96 | (lower_case_url.size() + 1) * sizeof(wchar_t)); |
| 97 | std::string hash_bin = base::SHA1HashString(url_buffer); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
| 99 | std::wstring url_hash; |
| 100 | |
| 101 | // Transform the buffer to an hexadecimal string. |
| 102 | unsigned char checksum = 0; |
[email protected] | 1cf4a78 | 2010-06-22 20:36:16 | [diff] [blame] | 103 | for (size_t i = 0; i < hash_bin.size(); ++i) { |
| 104 | // std::string gives signed chars, which mess with StringPrintf and |
| 105 | // check_sum. |
| 106 | unsigned char hash_byte = static_cast<unsigned char>(hash_bin[i]); |
| 107 | checksum += hash_byte; |
[email protected] | 33272e1f0 | 2011-08-17 00:22:07 | [diff] [blame] | 108 | url_hash += base::StringPrintf(L"%2.2X", static_cast<unsigned>(hash_byte)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | } |
[email protected] | 33272e1f0 | 2011-08-17 00:22:07 | [diff] [blame] | 110 | url_hash += base::StringPrintf(L"%2.2X", checksum); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | return url_hash; |
| 113 | } |
| 114 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 115 | bool DecryptPasswords(const std::wstring& url, |
| 116 | const std::vector<unsigned char>& data, |
| 117 | std::vector<DecryptedCredentials>* credentials) { |
[email protected] | cb1f4ac | 2014-08-07 16:55:42 | [diff] [blame] | 118 | std::wstring lower_case_url = base::StringToLowerASCII(url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | DATA_BLOB input = {0}; |
| 120 | DATA_BLOB output = {0}; |
| 121 | DATA_BLOB url_key = {0}; |
| 122 | |
| 123 | input.pbData = const_cast<unsigned char*>(&data.front()); |
| 124 | input.cbData = static_cast<DWORD>((data.size()) * |
| 125 | sizeof(std::string::value_type)); |
| 126 | |
| 127 | url_key.pbData = reinterpret_cast<unsigned char*>( |
| 128 | const_cast<wchar_t*>(lower_case_url.data())); |
| 129 | url_key.cbData = static_cast<DWORD>((lower_case_url.size() + 1) * |
| 130 | sizeof(std::wstring::value_type)); |
| 131 | |
| 132 | if (CryptUnprotectData(&input, NULL, &url_key, NULL, NULL, |
| 133 | CRYPTPROTECT_UI_FORBIDDEN, &output)) { |
| 134 | // Now that we have the decrypted information, we need to understand it. |
| 135 | std::vector<unsigned char> decrypted_data; |
| 136 | decrypted_data.resize(output.cbData); |
| 137 | memcpy(&decrypted_data.front(), output.pbData, output.cbData); |
| 138 | |
[email protected] | 22741ad | 2013-10-11 13:55:20 | [diff] [blame] | 139 | GetUserPassFromData(decrypted_data, credentials); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
| 141 | LocalFree(output.pbData); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | } // namespace ie7_password |