1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef TPM_MANAGER_SERVER_BINDER_SERVICE_H_
18 #define TPM_MANAGER_SERVER_BINDER_SERVICE_H_
19 
20 #include <brillo/binder_watcher.h>
21 #include <brillo/daemons/daemon.h>
22 
23 #include "android/tpm_manager/BnTpmNvram.h"
24 #include "android/tpm_manager/BnTpmOwnership.h"
25 #include "tpm_manager/common/tpm_nvram_interface.h"
26 #include "tpm_manager/common/tpm_ownership_interface.h"
27 
28 namespace tpm_manager {
29 
30 // BinderService registers for and handles all incoming binder calls for the
31 // tpm_managerd system daemon.
32 //
33 // Example Usage:
34 //
35 // BinderService service(&nvram_service, &ownership_service);
36 // service.Run();
37 class BinderService : public brillo::Daemon {
38  public:
39   BinderService(TpmNvramInterface* nvram_service,
40                 TpmOwnershipInterface* ownership_service);
41   ~BinderService() override = default;
42 
43   // Does basic setup but does not register with the binder subsystem.
44   void InitForTesting();
45 
46   // Getters for binder interfaces. Callers do not take ownership. These should
47   // only be used for testing.
48   android::tpm_manager::ITpmNvram* GetITpmNvram();
49   android::tpm_manager::ITpmOwnership* GetITpmOwnership();
50 
51  protected:
52   int OnInit() override;
53 
54  private:
55   friend class NvramServiceInternal;
56   class NvramServiceInternal : public android::tpm_manager::BnTpmNvram {
57    public:
58     explicit NvramServiceInternal(TpmNvramInterface* service);
59     ~NvramServiceInternal() override = default;
60 
61     // ITpmNvram interface.
62     android::binder::Status DefineSpace(
63         const std::vector<uint8_t>& command_proto,
64         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
65         override;
66     android::binder::Status DestroySpace(
67         const std::vector<uint8_t>& command_proto,
68         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
69         override;
70     android::binder::Status WriteSpace(
71         const std::vector<uint8_t>& command_proto,
72         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
73         override;
74     android::binder::Status ReadSpace(
75         const std::vector<uint8_t>& command_proto,
76         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
77         override;
78     android::binder::Status ListSpaces(
79         const std::vector<uint8_t>& command_proto,
80         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
81         override;
82     android::binder::Status GetSpaceInfo(
83         const std::vector<uint8_t>& command_proto,
84         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
85         override;
86     android::binder::Status LockSpace(
87         const std::vector<uint8_t>& command_proto,
88         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
89         override;
90 
91    private:
92     TpmNvramInterface* nvram_service_;
93   };
94 
95   friend class OwnershipServiceInternal;
96   class OwnershipServiceInternal : public android::tpm_manager::BnTpmOwnership {
97    public:
98     explicit OwnershipServiceInternal(TpmOwnershipInterface* service);
99     ~OwnershipServiceInternal() override = default;
100 
101     // ITpmOwnership interface.
102     android::binder::Status GetTpmStatus(
103         const std::vector<uint8_t>& command_proto,
104         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
105         override;
106     android::binder::Status TakeOwnership(
107         const std::vector<uint8_t>& command_proto,
108         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
109         override;
110     android::binder::Status RemoveOwnerDependency(
111         const std::vector<uint8_t>& command_proto,
112         const android::sp<android::tpm_manager::ITpmManagerClient>& client)
113         override;
114 
115    private:
116     TpmOwnershipInterface* ownership_service_;
117   };
118 
119   brillo::BinderWatcher watcher_;
120   android::sp<NvramServiceInternal> nvram_binder_;
121   android::sp<OwnershipServiceInternal> ownership_binder_;
122   TpmNvramInterface* nvram_service_;
123   TpmOwnershipInterface* ownership_service_;
124 
125   DISALLOW_COPY_AND_ASSIGN(BinderService);
126 };
127 
128 }  // namespace tpm_manager
129 
130 #endif  // TPM_MANAGER_SERVER_BINDER_SERVICE_H_
131