Bug 1422669 - Part4 - Kill string16 and replace with our SHA1 implementation. r=gerald

The reason explained in patch Part1 of Bug 1214018 and I just copy the reason below.

GetRawMachineId was returning its generated data through a 'string16', which on
Windows was conveniently equivalent to a std::wstring.

However on Mac, wstring uses 32-bit characters, so in order to comply with the
string16 interface, a lot of non-trivial code would have to be imported and
vetted.

Also, in the end GMPLoader::Load passes this string16 to SHA256_Update() as a
sequence of bytes, the actual type of the data is lost!

So to simplify this work, GetRawMachineId will now return its data through a
vector of bytes, and the platform-dependent implementations may use whatever
data type they want internally.

The Windows GetRawMachineId actually returns the same data in this vector, so
it stays compatible with the previous code.

MozReview-Commit-ID: 7xYgjndXWDX

--HG--
extra : rebase_source : 6a46bd7f06d6e4bc10bb98b600e7e5bc14c136ce
This commit is contained in:
James Cheng 2017-12-06 16:52:31 +08:00
Родитель d9be66f8f8
Коммит 8107c910bc
3 изменённых файлов: 24 добавлений и 15 удалений

Просмотреть файл

@ -6,11 +6,15 @@
#include <stddef.h> #include <stddef.h>
#include "base/sha1.h"
#include "rlz/lib/assert.h" #include "rlz/lib/assert.h"
#include "rlz/lib/crc8.h" #include "rlz/lib/crc8.h"
#include "rlz/lib/string_utils.h" #include "rlz/lib/string_utils.h"
// Note: The original machine_id.cc code depends on Chromium's sha1 implementation.
// Using Mozilla's implmentation as replacement to reduce the dependency of
// some external files.
#include "mozilla/SHA1.h"
namespace rlz_lib { namespace rlz_lib {
bool GetMachineId(std::string* machine_id) { bool GetMachineId(std::string* machine_id) {
@ -24,12 +28,12 @@ bool GetMachineId(std::string* machine_id) {
return true; return true;
} }
base::string16 sid_string; std::vector<uint8_t> sid_bytes;
int volume_id; int volume_id;
if (!GetRawMachineId(&sid_string, &volume_id)) if (!GetRawMachineId(&sid_bytes, &volume_id))
return false; return false;
if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id)) if (!testing::GetMachineIdImpl(sid_bytes, volume_id, machine_id))
return false; return false;
calculated = true; calculated = true;
@ -39,40 +43,43 @@ bool GetMachineId(std::string* machine_id) {
namespace testing { namespace testing {
bool GetMachineIdImpl(const base::string16& sid_string, bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
int volume_id, int volume_id,
std::string* machine_id) { std::string* machine_id) {
machine_id->clear(); machine_id->clear();
// The ID should be the SID hash + the Hard Drive SNo. + checksum byte. // The ID should be the SID hash + the Hard Drive SNo. + checksum byte.
static const int kSizeWithoutChecksum = base::kSHA1Length + sizeof(int); static const int kSizeWithoutChecksum = mozilla::SHA1Sum::kHashSize + sizeof(int);
std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0); std::basic_string<unsigned char> id_binary(kSizeWithoutChecksum + 1, 0);
if (!sid_string.empty()) { if (!sid_bytes.empty()) {
// In order to be compatible with the old version of RLZ, the hash of the // In order to be compatible with the old version of RLZ, the hash of the
// SID must be done with all the original bytes from the unicode string. // SID must be done with all the original bytes from the unicode string.
// However, the chromebase SHA1 hash function takes only an std::string as // However, the chromebase SHA1 hash function takes only an std::string as
// input, so the unicode string needs to be converted to std::string // input, so the unicode string needs to be converted to std::string
// "as is". // "as is".
size_t byte_count = sid_string.size() * sizeof(base::string16::value_type); size_t byte_count = sid_bytes.size() * sizeof(std::vector<uint8_t>::value_type);
const char* buffer = reinterpret_cast<const char*>(sid_string.c_str()); const char* buffer = reinterpret_cast<const char*>(sid_bytes.data());
std::string sid_string_buffer(buffer, byte_count);
// Note that digest can have embedded nulls. // Note that digest can have embedded nulls.
std::string digest(base::SHA1HashString(sid_string_buffer)); mozilla::SHA1Sum SHA1;
VERIFY(digest.size() == base::kSHA1Length); mozilla::SHA1Sum::Hash hash;
SHA1.update(buffer, byte_count);
SHA1.finish(hash);
std::string digest(reinterpret_cast<char*>(hash), mozilla::SHA1Sum::kHashSize);
VERIFY(digest.size() == mozilla::SHA1Sum::kHashSize);
std::copy(digest.begin(), digest.end(), id_binary.begin()); std::copy(digest.begin(), digest.end(), id_binary.begin());
} }
// Convert from int to binary (makes big-endian). // Convert from int to binary (makes big-endian).
for (size_t i = 0; i < sizeof(int); i++) { for (size_t i = 0; i < sizeof(int); i++) {
int shift_bits = 8 * (sizeof(int) - i - 1); int shift_bits = 8 * (sizeof(int) - i - 1);
id_binary[base::kSHA1Length + i] = static_cast<unsigned char>( id_binary[mozilla::SHA1Sum::kHashSize + i] = static_cast<unsigned char>(
(volume_id >> shift_bits) & 0xFF); (volume_id >> shift_bits) & 0xFF);
} }
// Append the checksum byte. // Append the checksum byte.
if (!sid_string.empty() || (0 != volume_id)) if (!sid_bytes.empty() || (0 != volume_id))
rlz_lib::Crc8::Generate(id_binary.c_str(), rlz_lib::Crc8::Generate(id_binary.c_str(),
kSizeWithoutChecksum, kSizeWithoutChecksum,
&id_binary[kSizeWithoutChecksum]); &id_binary[kSizeWithoutChecksum]);

Просмотреть файл

@ -23,7 +23,7 @@ bool GetMachineId(std::string* machine_id);
bool GetRawMachineId(std::vector<uint8_t>* data, int* more_data); bool GetRawMachineId(std::vector<uint8_t>* data, int* more_data);
namespace testing { namespace testing {
bool GetMachineIdImpl(const base::string16& sid_string, bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
int volume_id, int volume_id,
std::string* machine_id); std::string* machine_id);
} // namespace testing } // namespace testing

Просмотреть файл

@ -11,6 +11,8 @@ FINAL_LIBRARY = 'xul'
if CONFIG['OS_TARGET'] in ['WINNT', 'Darwin']: if CONFIG['OS_TARGET'] in ['WINNT', 'Darwin']:
UNIFIED_SOURCES += [ UNIFIED_SOURCES += [
'lib/crc8.cc',
'lib/machine_id.cc',
'lib/string_utils.cc', 'lib/string_utils.cc',
] ]