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