policy_cert_service.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/chromeos/policy/policy_cert_service.h"
6
7#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/logging.h"
10#include "chrome/browser/chromeos/login/users/user_manager.h"
11#include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
12#include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
13#include "content/public/browser/browser_thread.h"
14#include "net/cert/x509_certificate.h"
15
16namespace policy {
17
18PolicyCertService::~PolicyCertService() {
19  DCHECK(cert_verifier_)
20      << "CreatePolicyCertVerifier() must be called after construction.";
21}
22
23PolicyCertService::PolicyCertService(
24    const std::string& user_id,
25    UserNetworkConfigurationUpdater* net_conf_updater,
26    chromeos::UserManager* user_manager)
27    : cert_verifier_(NULL),
28      user_id_(user_id),
29      net_conf_updater_(net_conf_updater),
30      user_manager_(user_manager),
31      has_trust_anchors_(false),
32      weak_ptr_factory_(this) {
33  DCHECK(net_conf_updater_);
34  DCHECK(user_manager_);
35}
36
37PolicyCertService::PolicyCertService(const std::string& user_id,
38                                     PolicyCertVerifier* verifier,
39                                     chromeos::UserManager* user_manager)
40    : cert_verifier_(verifier),
41      user_id_(user_id),
42      net_conf_updater_(NULL),
43      user_manager_(user_manager),
44      has_trust_anchors_(false),
45      weak_ptr_factory_(this) {}
46
47scoped_ptr<PolicyCertVerifier> PolicyCertService::CreatePolicyCertVerifier() {
48  base::Closure callback = base::Bind(
49      &PolicyCertServiceFactory::SetUsedPolicyCertificates, user_id_);
50  cert_verifier_ = new PolicyCertVerifier(
51      base::Bind(base::IgnoreResult(&content::BrowserThread::PostTask),
52                 content::BrowserThread::UI,
53                 FROM_HERE,
54                 callback));
55  // Certs are forwarded to |cert_verifier_|, thus register here after
56  // |cert_verifier_| is created.
57  net_conf_updater_->AddTrustedCertsObserver(this);
58
59  // Set the current list of trust anchors.
60  net::CertificateList trust_anchors;
61  net_conf_updater_->GetWebTrustedCertificates(&trust_anchors);
62  OnTrustAnchorsChanged(trust_anchors);
63
64  return make_scoped_ptr(cert_verifier_);
65}
66
67void PolicyCertService::OnTrustAnchorsChanged(
68    const net::CertificateList& trust_anchors) {
69  DCHECK(cert_verifier_);
70
71  // Do not use certificates installed via ONC policy if the current session has
72  // multiple profiles. This is important to make sure that any possibly tainted
73  // data is absolutely confined to the managed profile and never, ever leaks to
74  // any other profile.
75  if (!trust_anchors.empty() && user_manager_->GetLoggedInUsers().size() > 1u) {
76    LOG(ERROR) << "Ignoring ONC-pushed certificates update because multiple "
77               << "users are logged in.";
78    return;
79  }
80
81  has_trust_anchors_ = !trust_anchors.empty();
82
83  // It's safe to use base::Unretained here, because it's guaranteed that
84  // |cert_verifier_| outlives this object (see description of
85  // CreatePolicyCertVerifier).
86  // Note: ProfileIOData, which owns the CertVerifier is deleted by a
87  // DeleteSoon on IO, i.e. after all pending tasks on IO are finished.
88  content::BrowserThread::PostTask(
89      content::BrowserThread::IO,
90      FROM_HERE,
91      base::Bind(&PolicyCertVerifier::SetTrustAnchors,
92                 base::Unretained(cert_verifier_),
93                 trust_anchors));
94}
95
96bool PolicyCertService::UsedPolicyCertificates() const {
97  return PolicyCertServiceFactory::UsedPolicyCertificates(user_id_);
98}
99
100void PolicyCertService::Shutdown() {
101  weak_ptr_factory_.InvalidateWeakPtrs();
102  if (net_conf_updater_)
103    net_conf_updater_->RemoveTrustedCertsObserver(this);
104  OnTrustAnchorsChanged(net::CertificateList());
105  net_conf_updater_ = NULL;
106}
107
108// static
109scoped_ptr<PolicyCertService> PolicyCertService::CreateForTesting(
110    const std::string& user_id,
111    PolicyCertVerifier* verifier,
112    chromeos::UserManager* user_manager) {
113  return make_scoped_ptr(
114      new PolicyCertService(user_id, verifier, user_manager));
115}
116
117}  // namespace policy
118