File: attestation_certificate_generator_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,356 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "attestation_certificate_generator_impl.h"

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "chrome/browser/ash/attestation/soft_bind_attestation_flow.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/services/device_sync/cryptauth_key_registry_impl.h"
#include "components/user_manager/user.h"

namespace ash::phonehub {

namespace {
constexpr base::TimeDelta kOfflineRetryTimeout = base::Minutes(1);
}

AttestationCertificateGeneratorImpl::AttestationCertificateGeneratorImpl(
    Profile* profile,
    std::unique_ptr<attestation::SoftBindAttestationFlow>
        soft_bind_attestation_flow)
    : soft_bind_attestation_flow_(std::move(soft_bind_attestation_flow)),
      profile_(profile) {
  auto key_registry = device_sync::CryptAuthKeyRegistryImpl::Factory::Create(
      profile->GetPrefs());
  key_registry_ = std::move(key_registry);
  if (NetworkHandler::IsInitialized()) {
    NetworkHandler::Get()->network_state_handler()->AddObserver(this,
                                                                FROM_HERE);
  }
  GenerateCertificate();
}

AttestationCertificateGeneratorImpl::~AttestationCertificateGeneratorImpl() {
  if (NetworkHandler::IsInitialized()) {
    NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
                                                                   FROM_HERE);
  }
}

bool AttestationCertificateGeneratorImpl::
    ShouldRegenerateAttestationCertificate() {
  if (!is_valid_) {
    return true;
  }

  if (last_attestation_completed_time_.is_null()) {
    return true;
  }

  if ((base::Time::NowFromSystemTime() - last_attestation_completed_time_) >
      base::Hours(24)) {
    return true;
  }
  return false;
}

void AttestationCertificateGeneratorImpl::RetrieveCertificate() {
  // TODO(b/278933392): Add a daily task to update certificate.
  if (ShouldRegenerateAttestationCertificate()) {
    GenerateCertificate();
    return;
  }
  NotifyCertificateGenerated(attestation_certs_, is_valid_);
}

void AttestationCertificateGeneratorImpl::GenerateCertificate() {
  const user_manager::User* user =
      ProfileHelper::Get()->GetUserByProfile(profile_);

  if (user == nullptr) {
    PA_LOG(WARNING) << __func__ << ": User unavailable for current profile.";
    OnAttestationCertificateGenerated({}, false);
    return;
  }

  const device_sync::CryptAuthKey* user_key_pair = key_registry_->GetActiveKey(
      device_sync::CryptAuthKeyBundle::Name::kUserKeyPair);

  if (user_key_pair == nullptr) {
    PA_LOG(WARNING) << __func__ << ": User missing key pair.";
    OnAttestationCertificateGenerated({}, false);
    return;
  }

  soft_bind_attestation_flow_->GetCertificate(
      base::BindOnce(&AttestationCertificateGeneratorImpl::
                         OnAttestationCertificateGenerated,
                     weak_ptr_factory_.GetWeakPtr()),
      user ? user->GetAccountId() : EmptyAccountId(),
      user_key_pair->public_key());
}

void AttestationCertificateGeneratorImpl::OnAttestationCertificateGenerated(
    const std::vector<std::string>& attestation_certs,
    bool is_valid) {
  attestation_certs_ = attestation_certs;
  is_valid_ = is_valid;
  last_attestation_completed_time_ = base::Time::NowFromSystemTime();
  NotifyCertificateGenerated(attestation_certs_, is_valid_);
}

void AttestationCertificateGeneratorImpl::DefaultNetworkChanged(
    const NetworkState* network) {
  // Only retry when we have an active connected network.
  if (!network || !network->IsConnectedState()) {
    return;
  }

  // Throttle attempts to prevent too many requests.
  if ((base::Time::NowFromSystemTime() -
       last_attestation_attempt_from_network_change_time_) <
      kOfflineRetryTimeout) {
    return;
  }

  if (ShouldRegenerateAttestationCertificate()) {
    last_attestation_attempt_from_network_change_time_ =
        base::Time::NowFromSystemTime();
    GenerateCertificate();
  }
}

}  // namespace ash::phonehub