Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gmp/CDMStorageIdProvider.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "CDMStorageIdProvider.h"
7
#include "GMPLog.h"
8
#include "nsCOMPtr.h"
9
#include "nsComponentManagerUtils.h"
10
#include "nsICryptoHash.h"
11
12
#ifdef SUPPORT_STORAGE_ID
13
#include "rlz/lib/machine_id.h"
14
#endif
15
16
namespace mozilla {
17
18
/*static*/ nsCString
19
CDMStorageIdProvider::ComputeStorageId(const nsCString& aOriginSalt)
20
0
{
21
0
#ifndef SUPPORT_STORAGE_ID
22
0
  return EmptyCString();
23
#else
24
  GMP_LOG("CDMStorageIdProvider::ComputeStorageId");
25
26
  std::string machineId;
27
  if (!rlz_lib::GetMachineId(&machineId)) {
28
    GMP_LOG("CDMStorageIdProvider::ComputeStorageId: get machineId failed.");
29
    return EmptyCString();
30
  }
31
32
  std::string originSalt(aOriginSalt.BeginReading(), aOriginSalt.Length());
33
  std::string input = machineId + originSalt + CDMStorageIdProvider::kBrowserIdentifier;
34
  nsAutoCString storageId;
35
  nsresult rv;
36
  nsCOMPtr<nsICryptoHash> hasher = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
37
  if (NS_WARN_IF(NS_FAILED(rv))) {
38
    GMP_LOG("CDMStorageIdProvider::ComputeStorageId: no crypto hash(0x%08" PRIx32 ")",
39
            static_cast<uint32_t>(rv));
40
    return EmptyCString();
41
  }
42
43
  rv = hasher->Init(nsICryptoHash::SHA256);
44
  if (NS_WARN_IF(NS_FAILED(rv))) {
45
    GMP_LOG("CDMStorageIdProvider::ComputeStorageId: failed to initialize hash(0x%08" PRIx32 ")",
46
            static_cast<uint32_t>(rv));
47
    return EmptyCString();
48
  }
49
50
  rv = hasher->Update(reinterpret_cast<const uint8_t*>(input.c_str()), input.size());
51
  if (NS_WARN_IF(NS_FAILED(rv))) {
52
    GMP_LOG("CDMStorageIdProvider::ComputeStorageId: failed to update hash(0x%08" PRIx32 ")",
53
            static_cast<uint32_t>(rv));
54
    return EmptyCString();
55
  }
56
57
  rv = hasher->Finish(false, storageId);
58
  if (NS_WARN_IF(NS_FAILED(rv))) {
59
    GMP_LOG("CDMStorageIdProvider::ComputeStorageId: failed to get the final hash result(0x%08" PRIx32 ")",
60
            static_cast<uint32_t>(rv));
61
    return EmptyCString();
62
  }
63
  return std::move(storageId);
64
#endif
65
}
66
67
} // namespace mozilla