OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/hwid_checker.h" | 5 #include "chrome/browser/chromeos/login/hwid_checker.h" |
6 | 6 |
7 #include <cstdio> | 7 #include <cstdio> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 bool IsCorrectExceptionalHWID(const std::string& hwid) { | 61 bool IsCorrectExceptionalHWID(const std::string& hwid) { |
62 if (!IsExceptionalHWID(hwid)) | 62 if (!IsExceptionalHWID(hwid)) |
63 return false; | 63 return false; |
64 std::string bom; | 64 std::string bom; |
65 if (!RE2::FullMatch(hwid, "[A-Z0-9]+ ((?:[A-Z2-7]{4}-)*[A-Z2-7]{1,4})", &bom)) | 65 if (!RE2::FullMatch(hwid, "[A-Z0-9]+ ((?:[A-Z2-7]{4}-)*[A-Z2-7]{1,4})", &bom)) |
66 return false; | 66 return false; |
67 if (bom.length() < 2) | 67 if (bom.length() < 2) |
68 return false; | 68 return false; |
69 std::string hwid_without_dashes; | 69 std::string hwid_without_dashes; |
70 RemoveChars(hwid, "-", &hwid_without_dashes); | 70 base::RemoveChars(hwid, "-", &hwid_without_dashes); |
71 LOG_ASSERT(hwid_without_dashes.length() >= 2); | 71 LOG_ASSERT(hwid_without_dashes.length() >= 2); |
72 std::string not_checksum = | 72 std::string not_checksum = |
73 hwid_without_dashes.substr(0, hwid_without_dashes.length() - 2); | 73 hwid_without_dashes.substr(0, hwid_without_dashes.length() - 2); |
74 std::string checksum = | 74 std::string checksum = |
75 hwid_without_dashes.substr(hwid_without_dashes.length() - 2); | 75 hwid_without_dashes.substr(hwid_without_dashes.length() - 2); |
76 return CalculateExceptionalHWIDChecksum(not_checksum) == checksum; | 76 return CalculateExceptionalHWIDChecksum(not_checksum) == checksum; |
77 } | 77 } |
78 | 78 |
79 std::string CalculateHWIDv3Checksum(const std::string& data) { | 79 std::string CalculateHWIDv3Checksum(const std::string& data) { |
80 static const char base8_alphabet[] = "23456789"; | 80 static const char base8_alphabet[] = "23456789"; |
81 static const char base32_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | 81 static const char base32_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
82 unsigned crc32 = CalculateCRC32(data); | 82 unsigned crc32 = CalculateCRC32(data); |
83 // We take 8 least significant bits of CRC-32 and encode them in 2 characters. | 83 // We take 8 least significant bits of CRC-32 and encode them in 2 characters. |
84 std::string checksum; | 84 std::string checksum; |
85 checksum += base8_alphabet[(crc32 >> 5) & 0x7]; | 85 checksum += base8_alphabet[(crc32 >> 5) & 0x7]; |
86 checksum += base32_alphabet[crc32 & 0x1f]; | 86 checksum += base32_alphabet[crc32 & 0x1f]; |
87 return checksum; | 87 return checksum; |
88 } | 88 } |
89 | 89 |
90 bool IsCorrectHWIDv3(const std::string& hwid) { | 90 bool IsCorrectHWIDv3(const std::string& hwid) { |
91 if (IsExceptionalHWID(hwid)) | 91 if (IsExceptionalHWID(hwid)) |
92 return false; | 92 return false; |
93 std::string regex = | 93 std::string regex = |
94 "([A-Z0-9]+ (?:[A-Z2-7][2-9][A-Z2-7]-)*[A-Z2-7])([2-9][A-Z2-7])"; | 94 "([A-Z0-9]+ (?:[A-Z2-7][2-9][A-Z2-7]-)*[A-Z2-7])([2-9][A-Z2-7])"; |
95 std::string not_checksum, checksum; | 95 std::string not_checksum, checksum; |
96 if (!RE2::FullMatch(hwid, regex, ¬_checksum, &checksum)) | 96 if (!RE2::FullMatch(hwid, regex, ¬_checksum, &checksum)) |
97 return false; | 97 return false; |
98 RemoveChars(not_checksum, "-", ¬_checksum); | 98 base::RemoveChars(not_checksum, "-", ¬_checksum); |
99 return CalculateHWIDv3Checksum(not_checksum) == checksum; | 99 return CalculateHWIDv3Checksum(not_checksum) == checksum; |
100 } | 100 } |
101 | 101 |
102 } // anonymous namespace | 102 } // anonymous namespace |
103 | 103 |
104 namespace chromeos { | 104 namespace chromeos { |
105 | 105 |
106 bool IsHWIDCorrect(const std::string& hwid) { | 106 bool IsHWIDCorrect(const std::string& hwid) { |
107 return IsCorrectHWIDv2(hwid) || IsCorrectExceptionalHWID(hwid) || | 107 return IsCorrectHWIDv2(hwid) || IsCorrectExceptionalHWID(hwid) || |
108 IsCorrectHWIDv3(hwid); | 108 IsCorrectHWIDv3(hwid); |
(...skipping 17 matching lines...) Expand all Loading... |
126 return false; | 126 return false; |
127 } | 127 } |
128 if (!chromeos::IsHWIDCorrect(hwid)) { | 128 if (!chromeos::IsHWIDCorrect(hwid)) { |
129 LOG(ERROR) << "Machine has malformed HWID '" << hwid << "'."; | 129 LOG(ERROR) << "Machine has malformed HWID '" << hwid << "'."; |
130 return false; | 130 return false; |
131 } | 131 } |
132 return true; | 132 return true; |
133 } | 133 } |
134 | 134 |
135 } // namespace chromeos | 135 } // namespace chromeos |
OLD | NEW |