OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/login/hwid_checker.h" | |
6 | |
7 #include <cstdio> | |
8 | |
9 #include "base/chromeos/chromeos_version.h" | |
10 #include "base/command_line.h" | |
11 #include "base/logging.h" | |
12 #include "chrome/browser/chromeos/system/statistics_provider.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "third_party/re2/re2/re2.h" | |
15 #include "third_party/zlib/zlib.h" | |
16 | |
17 namespace chromeos { | |
18 | |
19 std::string CalculateHWIDChecksum(const std::string& data) { | |
20 unsigned crc32_checksum = static_cast<unsigned>(crc32( | |
21 0, | |
22 reinterpret_cast<const Bytef*>(data.c_str()), | |
23 data.length())); | |
24 // We take four least significant decimal digits of CRC-32. | |
25 char checksum[5]; | |
26 int snprintf_result = | |
27 snprintf(checksum, 5, "%04u", crc32_checksum % 10000); | |
28 LOG_ASSERT(snprintf_result == 4); | |
29 return checksum; | |
30 } | |
31 | |
32 bool IsHWIDCorrect(const std::string& hwid) { | |
33 // TODO(dzhioev): add support of HWID v3 format. | |
34 std::string body; | |
35 std::string checksum; | |
36 if (!RE2::FullMatch(hwid, "([\\s\\S]*) (\\d{4})", &body, &checksum)) | |
37 return false; | |
38 return CalculateHWIDChecksum(body) == checksum; | |
39 } | |
40 | |
41 bool IsMachineHWIDCorrect() { | |
42 #if !defined(GOOGLE_CHROME_BUILD) | |
43 return true; | |
44 #endif | |
45 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) | |
46 return true; | |
47 if (!base::chromeos::IsRunningOnChromeOS()) | |
48 return true; | |
49 std::string hwid; | |
50 chromeos::system::StatisticsProvider* stats = | |
51 chromeos::system::StatisticsProvider::GetInstance(); | |
52 if (!stats->GetMachineStatistic("hardware_class", &hwid)) { | |
53 LOG(ERROR) << "Couldn't get machine statistic `hardware_class'."; | |
54 return false; | |
55 } | |
56 if (!chromeos::IsHWIDCorrect(hwid)) { | |
57 LOG(ERROR) << "Machine has malformed HWID `" << hwid << "'."; | |
Nikita (slow)
2013/02/12 07:51:50
nit: Use same style quotes.
dzhioev (left Google)
2013/02/12 13:57:51
Done.
| |
58 return false; | |
59 } | |
60 return true; | |
61 } | |
62 | |
63 } // namespace chromeos | |
64 | |
OLD | NEW |