1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file.
4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "chrome/browser/renderer_host/pepper/pepper_platform_verification_message_filter.h"
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/bind_helpers.h"
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/render_frame_host.h"
11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/public/browser/web_contents.h"
12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ppapi/c/pp_errors.h"
13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ppapi/host/host_message_context.h"
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ppapi/host/ppapi_host.h"
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)using chromeos::attestation::PlatformVerificationFlow;
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace chrome {
21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)PepperPlatformVerificationMessageFilter::
23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    PepperPlatformVerificationMessageFilter(content::BrowserPpapiHost* host,
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                            PP_Instance instance)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : render_process_id_(0), render_frame_id_(0) {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host->GetRenderFrameIDsForInstance(
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      instance, &render_process_id_, &render_frame_id_);
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)PepperPlatformVerificationMessageFilter::
31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ~PepperPlatformVerificationMessageFilter() {}
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)scoped_refptr<base::TaskRunner>
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)PepperPlatformVerificationMessageFilter::OverrideTaskRunnerForMessage(
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const IPC::Message& msg) {
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return content::BrowserThread::GetMessageLoopProxyForThread(
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      content::BrowserThread::UI);
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)int32_t PepperPlatformVerificationMessageFilter::OnResourceMessageReceived(
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const IPC::Message& msg,
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_BEGIN_MESSAGE_MAP(PepperPlatformVerificationMessageFilter, msg)
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        PpapiHostMsg_PlatformVerification_ChallengePlatform,
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        OnChallengePlatform)
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_END_MESSAGE_MAP()
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return PP_ERROR_FAILED;
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)int32_t PepperPlatformVerificationMessageFilter::OnChallengePlatform(
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ppapi::host::HostMessageContext* context,
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& service_id,
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<uint8_t>& challenge) {
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Ensure the RenderFrameHost is still alive.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RenderFrameHost* rfh =
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!rfh) {
64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ppapi::host::ReplyMessageContext reply_context =
65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        context->MakeReplyMessageContext();
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    reply_context.params.set_result(PP_ERROR_FAILED);
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    SendReply(
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        reply_context,
69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            std::vector<uint8_t>(), std::vector<uint8_t>(), std::string()));
71d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return PP_OK_COMPLETIONPENDING;
72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!pv_.get())
750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    pv_ = new PlatformVerificationFlow();
76d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  pv_->ChallengePlatformKey(
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::WebContents::FromRenderFrameHost(rfh),
79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      service_id,
80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      std::string(challenge.begin(), challenge.end()),
81d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::Bind(
82d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          &PepperPlatformVerificationMessageFilter::ChallengePlatformCallback,
83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          this,
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)          context->MakeReplyMessageContext()));
85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return PP_OK_COMPLETIONPENDING;
87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void PepperPlatformVerificationMessageFilter::ChallengePlatformCallback(
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ppapi::host::ReplyMessageContext reply_context,
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    chromeos::attestation::PlatformVerificationFlow::Result challenge_result,
92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& signed_data,
93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& signature,
94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::string& platform_key_certificate) {
95d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
96d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
97d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (challenge_result == PlatformVerificationFlow::SUCCESS) {
98d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    reply_context.params.set_result(PP_OK);
99d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  } else {
100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    reply_context.params.set_result(PP_ERROR_FAILED);
101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DCHECK_EQ(signed_data.size(), 0u);
102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DCHECK_EQ(signature.size(), 0u);
103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    DCHECK_EQ(platform_key_certificate.size(), 0u);
104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SendReply(reply_context,
107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)            PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                std::vector<uint8_t>(signed_data.begin(), signed_data.end()),
109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                std::vector<uint8_t>(signature.begin(), signature.end()),
110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                platform_key_certificate));
111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
112d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
113d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}  // namespace chrome
114