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.
5 #include "chrome/browser/renderer_host/pepper/pepper_platform_verification_message_filter.h"
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_frame_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"
18 using chromeos::attestation::PlatformVerificationFlow;
22 PepperPlatformVerificationMessageFilter::
23 PepperPlatformVerificationMessageFilter(content::BrowserPpapiHost* host,
25 : render_process_id_(0), render_frame_id_(0) {
26 host->GetRenderFrameIDsForInstance(
27 instance, &render_process_id_, &render_frame_id_);
30 PepperPlatformVerificationMessageFilter::
31 ~PepperPlatformVerificationMessageFilter() {}
33 scoped_refptr<base::TaskRunner>
34 PepperPlatformVerificationMessageFilter::OverrideTaskRunnerForMessage(
35 const IPC::Message& msg) {
36 return content::BrowserThread::GetMessageLoopProxyForThread(
37 content::BrowserThread::UI);
40 int32_t PepperPlatformVerificationMessageFilter::OnResourceMessageReceived(
41 const IPC::Message& msg,
42 ppapi::host::HostMessageContext* context) {
43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
45 IPC_BEGIN_MESSAGE_MAP(PepperPlatformVerificationMessageFilter, msg)
46 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
47 PpapiHostMsg_PlatformVerification_ChallengePlatform, OnChallengePlatform)
50 return PP_ERROR_FAILED;
53 int32_t PepperPlatformVerificationMessageFilter::OnChallengePlatform(
54 ppapi::host::HostMessageContext* context,
55 const std::string& service_id,
56 const std::vector<uint8_t>& challenge) {
57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
59 // Ensure the RenderFrameHost is still alive.
60 content::RenderFrameHost* rfh =
61 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
63 ppapi::host::ReplyMessageContext reply_context =
64 context->MakeReplyMessageContext();
65 reply_context.params.set_result(PP_ERROR_FAILED);
68 PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
69 std::vector<uint8_t>(), std::vector<uint8_t>(), std::string()));
70 return PP_OK_COMPLETIONPENDING;
74 pv_ = new PlatformVerificationFlow();
76 pv_->ChallengePlatformKey(
77 content::WebContents::FromRenderFrameHost(rfh),
79 std::string(challenge.begin(), challenge.end()),
81 &PepperPlatformVerificationMessageFilter::ChallengePlatformCallback,
83 context->MakeReplyMessageContext()));
85 return PP_OK_COMPLETIONPENDING;
88 void PepperPlatformVerificationMessageFilter::ChallengePlatformCallback(
89 ppapi::host::ReplyMessageContext reply_context,
90 chromeos::attestation::PlatformVerificationFlow::Result challenge_result,
91 const std::string& signed_data,
92 const std::string& signature,
93 const std::string& platform_key_certificate) {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
96 if (challenge_result == PlatformVerificationFlow::SUCCESS) {
97 reply_context.params.set_result(PP_OK);
99 reply_context.params.set_result(PP_ERROR_FAILED);
100 DCHECK_EQ(signed_data.size(), 0u);
101 DCHECK_EQ(signature.size(), 0u);
102 DCHECK_EQ(platform_key_certificate.size(), 0u);
105 SendReply(reply_context,
106 PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
107 std::vector<uint8_t>(signed_data.begin(), signed_data.end()),
108 std::vector<uint8_t>(signature.begin(), signature.end()),
109 platform_key_certificate));
112 } // namespace chrome