Bug 1422669 - Part4 - Kill string16 and replace with our SHA1 implementation. draft
authorJames Cheng <jacheng@mozilla.com>
Wed, 06 Dec 2017 16:52:31 +0800
changeset 708826 69697068a03e6b720ac0e0518a3004fd63e2f427
parent 708170 e758328394e4e2219c1c41ee6c14ce3d34d3c291
child 743261 5a119fe62c0a8e365b298a713dc1ad019a37272f
push id92464
push userbmo:jacheng@mozilla.com
push dateThu, 07 Dec 2017 07:47:40 +0000
bugs1422669, 1214018
milestone59.0a1
Bug 1422669 - Part4 - Kill string16 and replace with our SHA1 implementation. 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
dom/media/gmp/rlz/lib/machine_id.cc
dom/media/gmp/rlz/lib/machine_id.h
dom/media/gmp/rlz/moz.build
--- a/dom/media/gmp/rlz/lib/machine_id.cc
+++ b/dom/media/gmp/rlz/lib/machine_id.cc
@@ -1,83 +1,90 @@
 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 #include "rlz/lib/machine_id.h"
 
 #include <stddef.h>
 
-#include "base/sha1.h"
 #include "rlz/lib/assert.h"
 #include "rlz/lib/crc8.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 {
 
 bool GetMachineId(std::string* machine_id) {
   if (!machine_id)
     return false;
 
   static std::string calculated_id;
   static bool calculated = false;
   if (calculated) {
     *machine_id = calculated_id;
     return true;
   }
 
-  base::string16 sid_string;
+  std::vector<uint8_t> sid_bytes;
   int volume_id;
-  if (!GetRawMachineId(&sid_string, &volume_id))
+  if (!GetRawMachineId(&sid_bytes, &volume_id))
     return false;
 
-  if (!testing::GetMachineIdImpl(sid_string, volume_id, machine_id))
+  if (!testing::GetMachineIdImpl(sid_bytes, volume_id, machine_id))
     return false;
 
   calculated = true;
   calculated_id = *machine_id;
   return true;
 }
 
 namespace testing {
 
-bool GetMachineIdImpl(const base::string16& sid_string,
+bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
                       int volume_id,
                       std::string* machine_id) {
   machine_id->clear();
 
   // 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);
 
-  if (!sid_string.empty()) {
+  if (!sid_bytes.empty()) {
     // 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.
     // However, the chromebase SHA1 hash function takes only an std::string as
     // input, so the unicode string needs to be converted to std::string
     // "as is".
-    size_t byte_count = sid_string.size() * sizeof(base::string16::value_type);
-    const char* buffer = reinterpret_cast<const char*>(sid_string.c_str());
-    std::string sid_string_buffer(buffer, byte_count);
+    size_t byte_count = sid_bytes.size() * sizeof(std::vector<uint8_t>::value_type);
+    const char* buffer = reinterpret_cast<const char*>(sid_bytes.data());
 
     // Note that digest can have embedded nulls.
-    std::string digest(base::SHA1HashString(sid_string_buffer));
-    VERIFY(digest.size() == base::kSHA1Length);
+    mozilla::SHA1Sum SHA1;
+    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());
   }
 
   // Convert from int to binary (makes big-endian).
   for (size_t i = 0; i < sizeof(int); i++) {
     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);
   }
 
   // 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(),
                             kSizeWithoutChecksum,
                             &id_binary[kSizeWithoutChecksum]);
 
   return rlz_lib::BytesToString(
       id_binary.c_str(), kSizeWithoutChecksum + 1, machine_id);
 }
 
--- a/dom/media/gmp/rlz/lib/machine_id.h
+++ b/dom/media/gmp/rlz/lib/machine_id.h
@@ -18,16 +18,16 @@ namespace rlz_lib {
 bool GetMachineId(std::string* machine_id);
 
 // Retrieves a raw machine identifier string and a machine-specific
 // 4 byte value. GetMachineId() will SHA1 |data|, append |more_data|, compute
 // the Crc8 of that, and return a hex-encoded string of that data.
 bool GetRawMachineId(std::vector<uint8_t>* data, int* more_data);
 
 namespace testing {
-bool GetMachineIdImpl(const base::string16& sid_string,
+bool GetMachineIdImpl(const std::vector<uint8_t>& sid_bytes,
                       int volume_id,
                       std::string* machine_id);
 }  // namespace testing
 
 }  // namespace rlz_lib
 
 #endif  // RLZ_LIB_MACHINE_ID_H_
--- a/dom/media/gmp/rlz/moz.build
+++ b/dom/media/gmp/rlz/moz.build
@@ -6,16 +6,18 @@
 
 # Note: build rlz in its own moz.build, so it doesn't pickup any of
 # Chromium IPC's headers used in the moz.build of the parent file.
 
 FINAL_LIBRARY = 'xul'
 
 if CONFIG['OS_TARGET'] in ['WINNT', 'Darwin']:
     UNIFIED_SOURCES += [
+        'lib/crc8.cc',
+        'lib/machine_id.cc',
         'lib/string_utils.cc',
     ]
 
 if CONFIG['OS_TARGET'] == 'WINNT':
     UNIFIED_SOURCES += [
         'win/lib/machine_id_win.cc',
     ]