blob: 9bb3225021a3e4254f31f15aae7bdbb7e637d122 [file] [log] [blame]
[email protected]908cb9e2014-02-21 15:40:091// Copyright 2014 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]c523d202014-03-18 01:34:545#include "components/os_crypt/ie7_password_win.h"
initial.commit09911bf2008-07-26 23:55:296
initial.commit09911bf2008-07-26 23:55:297#include <string>
8#include <vector>
9
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
[email protected]1cf4a782010-06-22 20:36:1611#include "base/sha1.h"
[email protected]c72674b2013-06-11 04:16:4312#include "base/strings/string_util.h"
13#include "base/strings/stringprintf.h"
davidbenc6ac2602014-10-30 00:39:3814#include "crypto/wincrypt_shim.h"
initial.commit09911bf2008-07-26 23:55:2915
16namespace {
17
18// Structures that IE7/IE8 use to store a username/password.
19// Some of the fields might have been incorrectly reverse engineered.
20struct 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
27struct 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]22741ad2013-10-11 13:55:2031 DWORD item_count; // Number of entries. Should be even.
initial.commit09911bf2008-07-26 23:55:2932 wchar_t two_letters[2]; // Two unknown bytes.
33 DWORD unknown[2]; // Two unknown DWORDs.
34};
35
36struct 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.
44struct PasswordEntry {
45 PreHeader pre_header; // Contains the size of the different sections.
46 Header header; // Contains the number of items.
[email protected]22741ad2013-10-11 13:55:2047 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.commit09911bf2008-07-26 23:55:2950};
initial.commit09911bf2008-07-26 23:55:2951} // namespace
52
53namespace ie7_password {
54
55bool GetUserPassFromData(const std::vector<unsigned char>& data,
[email protected]22741ad2013-10-11 13:55:2056 std::vector<DecryptedCredentials>* credentials) {
initial.commit09911bf2008-07-26 23:55:2957 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]22741ad2013-10-11 13:55:2065 const int entry_count = information->header.item_count;
66 if (entry_count % 2) // Usernames and Passwords
initial.commit09911bf2008-07-26 23:55:2967 return false;
68
69 if (information->header.fixed_header_size != sizeof(Header))
70 return false;
71
[email protected]22741ad2013-10-11 13:55:2072 const uint8* offset_to_data = &data[0] +
73 information->pre_header.header_size +
initial.commit09911bf2008-07-26 23:55:2974 information->pre_header.pre_header_size;
75
[email protected]22741ad2013-10-11 13:55:2076 for (int i = 0; i < entry_count / 2; ++i) {
initial.commit09911bf2008-07-26 23:55:2977
[email protected]22741ad2013-10-11 13:55:2078 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.commit09911bf2008-07-26 23:55:2988 return true;
89}
90
91std::wstring GetUrlHash(const std::wstring& url) {
[email protected]cb1f4ac2014-08-07 16:55:4292 std::wstring lower_case_url = base::StringToLowerASCII(url);
[email protected]1cf4a782010-06-22 20:36:1693 // 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.commit09911bf2008-07-26 23:55:2998
99 std::wstring url_hash;
100
101 // Transform the buffer to an hexadecimal string.
102 unsigned char checksum = 0;
[email protected]1cf4a782010-06-22 20:36:16103 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]33272e1f02011-08-17 00:22:07108 url_hash += base::StringPrintf(L"%2.2X", static_cast<unsigned>(hash_byte));
initial.commit09911bf2008-07-26 23:55:29109 }
[email protected]33272e1f02011-08-17 00:22:07110 url_hash += base::StringPrintf(L"%2.2X", checksum);
initial.commit09911bf2008-07-26 23:55:29111
initial.commit09911bf2008-07-26 23:55:29112 return url_hash;
113}
114
[email protected]22741ad2013-10-11 13:55:20115bool DecryptPasswords(const std::wstring& url,
116 const std::vector<unsigned char>& data,
117 std::vector<DecryptedCredentials>* credentials) {
[email protected]cb1f4ac2014-08-07 16:55:42118 std::wstring lower_case_url = base::StringToLowerASCII(url);
initial.commit09911bf2008-07-26 23:55:29119 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]22741ad2013-10-11 13:55:20139 GetUserPassFromData(decrypted_data, credentials);
initial.commit09911bf2008-07-26 23:55:29140
141 LocalFree(output.pbData);
142 return true;
143 }
144
145 return false;
146}
147
148} // namespace ie7_password