blob: a2691f9951051f9987f48ea7c56ba2b974b7d8e1 [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
thakis56e8586a2015-05-01 18:55:2153IE7PasswordInfo::IE7PasswordInfo() {
54}
55
56IE7PasswordInfo::~IE7PasswordInfo() {
57}
58
initial.commit09911bf2008-07-26 23:55:2959namespace ie7_password {
60
61bool GetUserPassFromData(const std::vector<unsigned char>& data,
[email protected]22741ad2013-10-11 13:55:2062 std::vector<DecryptedCredentials>* credentials) {
initial.commit09911bf2008-07-26 23:55:2963 const PasswordEntry* information =
64 reinterpret_cast<const PasswordEntry*>(&data.front());
65
66 // Some expected values. If it's not what we expect we don't even try to
67 // understand the data.
68 if (information->pre_header.pre_header_size != sizeof(PreHeader))
69 return false;
70
[email protected]22741ad2013-10-11 13:55:2071 const int entry_count = information->header.item_count;
72 if (entry_count % 2) // Usernames and Passwords
initial.commit09911bf2008-07-26 23:55:2973 return false;
74
75 if (information->header.fixed_header_size != sizeof(Header))
76 return false;
77
[email protected]22741ad2013-10-11 13:55:2078 const uint8* offset_to_data = &data[0] +
79 information->pre_header.header_size +
initial.commit09911bf2008-07-26 23:55:2980 information->pre_header.pre_header_size;
81
[email protected]22741ad2013-10-11 13:55:2082 for (int i = 0; i < entry_count / 2; ++i) {
initial.commit09911bf2008-07-26 23:55:2983
[email protected]22741ad2013-10-11 13:55:2084 const Entry* user_entry = &information->entry[2*i];
85 const Entry* pass_entry = user_entry+1;
86
87 DecryptedCredentials c;
88 c.username = reinterpret_cast<const wchar_t*>(offset_to_data +
89 user_entry->offset);
90 c.password = reinterpret_cast<const wchar_t*>(offset_to_data +
91 pass_entry->offset);
92 credentials->push_back(c);
93 }
initial.commit09911bf2008-07-26 23:55:2994 return true;
95}
96
97std::wstring GetUrlHash(const std::wstring& url) {
[email protected]cb1f4ac2014-08-07 16:55:4298 std::wstring lower_case_url = base::StringToLowerASCII(url);
[email protected]1cf4a782010-06-22 20:36:1699 // Get a data buffer out of our std::wstring to pass to SHA1HashString.
100 std::string url_buffer(
101 reinterpret_cast<const char*>(lower_case_url.c_str()),
102 (lower_case_url.size() + 1) * sizeof(wchar_t));
103 std::string hash_bin = base::SHA1HashString(url_buffer);
initial.commit09911bf2008-07-26 23:55:29104
105 std::wstring url_hash;
106
107 // Transform the buffer to an hexadecimal string.
108 unsigned char checksum = 0;
[email protected]1cf4a782010-06-22 20:36:16109 for (size_t i = 0; i < hash_bin.size(); ++i) {
110 // std::string gives signed chars, which mess with StringPrintf and
111 // check_sum.
112 unsigned char hash_byte = static_cast<unsigned char>(hash_bin[i]);
113 checksum += hash_byte;
[email protected]33272e1f02011-08-17 00:22:07114 url_hash += base::StringPrintf(L"%2.2X", static_cast<unsigned>(hash_byte));
initial.commit09911bf2008-07-26 23:55:29115 }
[email protected]33272e1f02011-08-17 00:22:07116 url_hash += base::StringPrintf(L"%2.2X", checksum);
initial.commit09911bf2008-07-26 23:55:29117
initial.commit09911bf2008-07-26 23:55:29118 return url_hash;
119}
120
[email protected]22741ad2013-10-11 13:55:20121bool DecryptPasswords(const std::wstring& url,
122 const std::vector<unsigned char>& data,
123 std::vector<DecryptedCredentials>* credentials) {
[email protected]cb1f4ac2014-08-07 16:55:42124 std::wstring lower_case_url = base::StringToLowerASCII(url);
initial.commit09911bf2008-07-26 23:55:29125 DATA_BLOB input = {0};
126 DATA_BLOB output = {0};
127 DATA_BLOB url_key = {0};
128
129 input.pbData = const_cast<unsigned char*>(&data.front());
130 input.cbData = static_cast<DWORD>((data.size()) *
131 sizeof(std::string::value_type));
132
133 url_key.pbData = reinterpret_cast<unsigned char*>(
134 const_cast<wchar_t*>(lower_case_url.data()));
135 url_key.cbData = static_cast<DWORD>((lower_case_url.size() + 1) *
136 sizeof(std::wstring::value_type));
137
138 if (CryptUnprotectData(&input, NULL, &url_key, NULL, NULL,
139 CRYPTPROTECT_UI_FORBIDDEN, &output)) {
140 // Now that we have the decrypted information, we need to understand it.
141 std::vector<unsigned char> decrypted_data;
142 decrypted_data.resize(output.cbData);
143 memcpy(&decrypted_data.front(), output.pbData, output.cbData);
144
[email protected]22741ad2013-10-11 13:55:20145 GetUserPassFromData(decrypted_data, credentials);
initial.commit09911bf2008-07-26 23:55:29146
147 LocalFree(output.pbData);
148 return true;
149 }
150
151 return false;
152}
153
154} // namespace ie7_password