summaryrefslogtreecommitdiffstats
path: root/chromeos/tpm/tpm_token_info_getter.h
blob: 1612dc5f5294d2350043b70e3683006a86a1f3ee (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_TPM_TPM_TOKEN_INFO_GETTER_H_
#define CHROMEOS_TPM_TPM_TOKEN_INFO_GETTER_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"

namespace base {
class TaskRunner;
}

namespace chromeos {
class CryptohomeClient;
}

namespace chromeos {

// Information retrieved from cryptohome by TPMTokenInfoGetter.
// For invalid token |token_name| and |user_pin| will be empty, while
// |token_slot_id| will be set to -1.
struct TPMTokenInfo {
  // Default constructor creates token info for disabled TPM.
  TPMTokenInfo();
  ~TPMTokenInfo();

  bool tpm_is_enabled;
  std::string token_name;
  std::string user_pin;
  int token_slot_id;
};

// Class for getting a user or the system TPM token info from cryptohome during
// TPM token loading.
class CHROMEOS_EXPORT TPMTokenInfoGetter {
 public:
  using TPMTokenInfoCallback = base::Callback<void(const TPMTokenInfo& info)>;

  // Factory method for TPMTokenInfoGetter for a user token.
  static scoped_ptr<TPMTokenInfoGetter> CreateForUserToken(
      const std::string& user_id,
      CryptohomeClient* cryptohome_client,
      const scoped_refptr<base::TaskRunner>& delayed_task_runner);

  // Factory method for TPMTokenGetter for the system token.
  static scoped_ptr<TPMTokenInfoGetter> CreateForSystemToken(
      CryptohomeClient* cryptohome_client,
      const scoped_refptr<base::TaskRunner>& delayed_task_runner);

  ~TPMTokenInfoGetter();

  // Starts getting TPM token info. Should be called at most once.
  // |callback| will be called when all the info is fetched.
  // The object may get deleted before |callback| is called, which is equivalent
  // to cancelling the info getting (in which case |callback| will never get
  // called).
  void Start(const TPMTokenInfoCallback& callback);

 private:
  enum Type {
    TYPE_SYSTEM,
    TYPE_USER
  };

  enum State {
    STATE_INITIAL,
    STATE_STARTED,
    STATE_TPM_ENABLED,
    STATE_DONE
  };

  TPMTokenInfoGetter(
      Type type,
      const std::string& user_id,
      CryptohomeClient* cryptohome_client,
      const scoped_refptr<base::TaskRunner>& delayed_task_runner);

  // Continues TPM token info getting procedure by starting the task associated
  // with the current TPMTokenInfoGetter state.
  void Continue();

  // If token initialization step fails (e.g. if tpm token is not yet ready)
  // schedules the initialization step retry attempt after a timeout.
  void RetryLater();

  // Cryptohome methods callbacks.
  void OnTpmIsEnabled(DBusMethodCallStatus call_status,
                      bool tpm_is_enabled);
  void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status,
                               const std::string& token_name,
                               const std::string& user_pin,
                               int token_slot_id);

  // The task runner used to run delayed tasks when retrying failed Cryptohome
  // calls.
  scoped_refptr<base::TaskRunner> delayed_task_runner_;

  Type type_;
  State state_;

  // The user id associated with the TPMTokenInfoGetter. Empty for system token.
  std::string user_id_;

  TPMTokenInfoCallback callback_;

  // The current request delay before the next attempt to initialize the
  // TPM. Will be adapted after each attempt.
  base::TimeDelta tpm_request_delay_;

  CryptohomeClient* cryptohome_client_;

  base::WeakPtrFactory<TPMTokenInfoGetter> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(TPMTokenInfoGetter);
};

}  // namespace chromeos

#endif  // CHROMEOS_TPM_TPM_TOKEN_INFO_GETTER_H_