Home | History | Annotate | Download | only in browser
      1 // Copyright 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 #include "content/public/browser/media_device_id.h"
      5 
      6 #include "base/logging.h"
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/strings/string_util.h"
      9 #include "content/browser/browser_main_loop.h"
     10 #include "content/browser/renderer_host/media/media_stream_manager.h"
     11 #include "crypto/hmac.h"
     12 
     13 namespace content {
     14 
     15 std::string GetHMACForMediaDeviceID(const ResourceContext::SaltCallback& sc,
     16                                     const GURL& security_origin,
     17                                     const std::string& raw_unique_id) {
     18   DCHECK(security_origin.is_valid());
     19   DCHECK(!raw_unique_id.empty());
     20   crypto::HMAC hmac(crypto::HMAC::SHA256);
     21   const size_t digest_length = hmac.DigestLength();
     22   std::vector<uint8> digest(digest_length);
     23   std::string salt = sc.Run();
     24   bool result = hmac.Init(security_origin.spec()) &&
     25       hmac.Sign(raw_unique_id + salt, &digest[0], digest.size());
     26   DCHECK(result);
     27   return base::StringToLowerASCII(base::HexEncode(&digest[0], digest.size()));
     28 }
     29 
     30 bool DoesMediaDeviceIDMatchHMAC(const ResourceContext::SaltCallback& sc,
     31                                 const GURL& security_origin,
     32                                 const std::string& device_guid,
     33                                 const std::string& raw_unique_id) {
     34   DCHECK(security_origin.is_valid());
     35   DCHECK(!raw_unique_id.empty());
     36   std::string guid_from_raw_device_id =
     37       GetHMACForMediaDeviceID(sc, security_origin, raw_unique_id);
     38   return guid_from_raw_device_id == device_guid;
     39 }
     40 
     41 bool GetMediaDeviceIDForHMAC(MediaStreamType stream_type,
     42                              const ResourceContext::SaltCallback& rc,
     43                              const GURL& security_origin,
     44                              const std::string& source_id,
     45                              std::string* device_id) {
     46   content::MediaStreamManager* manager =
     47       content::BrowserMainLoop::GetInstance()->media_stream_manager();
     48 
     49   return manager->TranslateSourceIdToDeviceId(
     50       content::MEDIA_DEVICE_VIDEO_CAPTURE,
     51       rc,
     52       security_origin,
     53       source_id,
     54       device_id);
     55 }
     56 
     57 }  // namespace content
     58