summaryrefslogtreecommitdiff
path: root/tpm_manager/server/binder_service.h
blob: c4de0c62792650f145dcff476c49ca5b5c9a6ce8 (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
129
130
//
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef TPM_MANAGER_SERVER_BINDER_SERVICE_H_
#define TPM_MANAGER_SERVER_BINDER_SERVICE_H_

#include <brillo/binder_watcher.h>
#include <brillo/daemons/daemon.h>

#include "android/tpm_manager/BnTpmNvram.h"
#include "android/tpm_manager/BnTpmOwnership.h"
#include "tpm_manager/common/tpm_nvram_interface.h"
#include "tpm_manager/common/tpm_ownership_interface.h"

namespace tpm_manager {

// BinderService registers for and handles all incoming binder calls for the
// tpm_managerd system daemon.
//
// Example Usage:
//
// BinderService service(&nvram_service, &ownership_service);
// service.Run();
class BinderService : public brillo::Daemon {
 public:
  BinderService(TpmNvramInterface* nvram_service,
                TpmOwnershipInterface* ownership_service);
  ~BinderService() override = default;

  // Does basic setup but does not register with the binder subsystem.
  void InitForTesting();

  // Getters for binder interfaces. Callers do not take ownership. These should
  // only be used for testing.
  android::tpm_manager::ITpmNvram* GetITpmNvram();
  android::tpm_manager::ITpmOwnership* GetITpmOwnership();

 protected:
  int OnInit() override;

 private:
  friend class NvramServiceInternal;
  class NvramServiceInternal : public android::tpm_manager::BnTpmNvram {
   public:
    NvramServiceInternal(TpmNvramInterface* service);
    ~NvramServiceInternal() override = default;

    // ITpmNvram interface.
    android::binder::Status DefineSpace(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status DestroySpace(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status WriteSpace(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status ReadSpace(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status ListSpaces(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status GetSpaceInfo(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status LockSpace(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;

   private:
    TpmNvramInterface* nvram_service_;
  };

  friend class OwnershipServiceInternal;
  class OwnershipServiceInternal : public android::tpm_manager::BnTpmOwnership {
   public:
    OwnershipServiceInternal(TpmOwnershipInterface* service);
    ~OwnershipServiceInternal() override = default;

    // ITpmOwnership interface.
    android::binder::Status GetTpmStatus(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status TakeOwnership(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;
    android::binder::Status RemoveOwnerDependency(
        const std::vector<uint8_t>& command_proto,
        const android::sp<android::tpm_manager::ITpmManagerClient>& client)
        override;

   private:
    TpmOwnershipInterface* ownership_service_;
  };

  brillo::BinderWatcher watcher_;
  android::sp<NvramServiceInternal> nvram_binder_;
  android::sp<OwnershipServiceInternal> ownership_binder_;
  TpmNvramInterface* nvram_service_;
  TpmOwnershipInterface* ownership_service_;

  DISALLOW_COPY_AND_ASSIGN(BinderService);
};

}  // namespace tpm_manager

#endif  // TPM_MANAGER_SERVER_BINDER_SERVICE_H_