pepper_platform_verification_message_filter.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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
5#include "chrome/browser/renderer_host/pepper/pepper_platform_verification_message_filter.h"
6
7#include "base/bind_helpers.h"
8#include "content/public/browser/browser_ppapi_host.h"
9#include "content/public/browser/browser_thread.h"
10#include "content/public/browser/render_view_host.h"
11#include "content/public/browser/web_contents.h"
12#include "ppapi/c/pp_errors.h"
13#include "ppapi/host/dispatch_host_message.h"
14#include "ppapi/host/host_message_context.h"
15#include "ppapi/host/ppapi_host.h"
16#include "ppapi/proxy/ppapi_messages.h"
17
18using chromeos::attestation::PlatformVerificationFlow;
19
20namespace chrome {
21
22PepperPlatformVerificationMessageFilter::
23    PepperPlatformVerificationMessageFilter(content::BrowserPpapiHost* host,
24                                            PP_Instance instance)
25    : render_process_id_(0), render_view_id_(0) {
26  host->GetRenderViewIDsForInstance(
27      instance, &render_process_id_, &render_view_id_);
28}
29
30PepperPlatformVerificationMessageFilter::
31    ~PepperPlatformVerificationMessageFilter() {}
32
33scoped_refptr<base::TaskRunner>
34PepperPlatformVerificationMessageFilter::OverrideTaskRunnerForMessage(
35    const IPC::Message& msg) {
36  return content::BrowserThread::GetMessageLoopProxyForThread(
37      content::BrowserThread::UI);
38}
39
40int32_t PepperPlatformVerificationMessageFilter::OnResourceMessageReceived(
41    const IPC::Message& msg,
42    ppapi::host::HostMessageContext* context) {
43  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44
45  IPC_BEGIN_MESSAGE_MAP(PepperPlatformVerificationMessageFilter, msg)
46    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
47        PpapiHostMsg_PlatformVerification_ChallengePlatform,
48        OnChallengePlatform)
49  IPC_END_MESSAGE_MAP()
50
51  return PP_ERROR_FAILED;
52}
53
54int32_t PepperPlatformVerificationMessageFilter::OnChallengePlatform(
55    ppapi::host::HostMessageContext* context,
56    const std::string& service_id,
57    const std::vector<uint8_t>& challenge) {
58  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
59
60  // Ensure the RenderViewHost is still alive.
61  content::RenderViewHost* rvh =
62      content::RenderViewHost::FromID(render_process_id_, render_view_id_);
63  if (!rvh) {
64    ppapi::host::ReplyMessageContext reply_context =
65        context->MakeReplyMessageContext();
66    reply_context.params.set_result(PP_ERROR_FAILED);
67    SendReply(
68        reply_context,
69        PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
70            std::vector<uint8_t>(), std::vector<uint8_t>(), std::string()));
71    return PP_OK_COMPLETIONPENDING;
72  }
73
74  if (!pv_)
75    pv_ = new PlatformVerificationFlow();
76
77  pv_->ChallengePlatformKey(
78      content::WebContents::FromRenderViewHost(rvh),
79      service_id,
80      std::string(challenge.begin(), challenge.end()),
81      base::Bind(
82          &PepperPlatformVerificationMessageFilter::ChallengePlatformCallback,
83          this,
84          context->MakeReplyMessageContext()));
85
86  return PP_OK_COMPLETIONPENDING;
87}
88
89void PepperPlatformVerificationMessageFilter::ChallengePlatformCallback(
90    ppapi::host::ReplyMessageContext reply_context,
91    chromeos::attestation::PlatformVerificationFlow::Result challenge_result,
92    const std::string& signed_data,
93    const std::string& signature,
94    const std::string& platform_key_certificate) {
95  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
96
97  if (challenge_result == PlatformVerificationFlow::SUCCESS) {
98    reply_context.params.set_result(PP_OK);
99  } else {
100    reply_context.params.set_result(PP_ERROR_FAILED);
101    DCHECK_EQ(signed_data.size(), 0u);
102    DCHECK_EQ(signature.size(), 0u);
103    DCHECK_EQ(platform_key_certificate.size(), 0u);
104  }
105
106  SendReply(reply_context,
107            PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
108                std::vector<uint8_t>(signed_data.begin(), signed_data.end()),
109                std::vector<uint8_t>(signature.begin(), signature.end()),
110                platform_key_certificate));
111}
112
113}  // namespace chrome
114