1 //
2 // Copyright (C) 2015 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 TRUNKS_POLICY_SESSION_IMPL_H_
18 #define TRUNKS_POLICY_SESSION_IMPL_H_
19 
20 #include "trunks/policy_session.h"
21 
22 #include <string>
23 #include <vector>
24 
25 #include "trunks/hmac_authorization_delegate.h"
26 #include "trunks/session_manager.h"
27 #include "trunks/trunks_factory.h"
28 
29 namespace trunks {
30 
31 // This class implements the PolicySession interface. It is used for
32 // keeping track of the HmacAuthorizationDelegate used for commands, and to
33 // provide authorization for commands that need it. It can also be used to
34 // create custom policies to restrict the usage of keys.
35 // TrunksFactoryImpl factory;
36 // PolicySessionImpl session(factory);
37 // session.StartBoundSession(bind_entity, bind_authorization, true);
38 // session.PolicyPCR(pcr_index, pcr_value);
39 // factory.GetTpm()->RSA_EncrpytSync(_,_,_,_, session.GetDelegate());
40 // NOTE: StartBoundSession/StartUnboundSession should not be called before
41 // TPM Ownership is taken. This is because starting a session uses the
42 // SaltingKey, which is only created after ownership is taken.
43 class TRUNKS_EXPORT PolicySessionImpl: public PolicySession {
44  public:
45   explicit PolicySessionImpl(const TrunksFactory& factory);
46   // |session_type| specifies what type of session this is. It can only
47   // be TPM_SE_TRIAL or TPM_SE_POLICY. If other values are used,
48   // StartBoundSession will return SAPI_RC_INVALID_SESSIONS.
49   PolicySessionImpl(const TrunksFactory& factory, TPM_SE session_type);
50   ~PolicySessionImpl() override;
51 
52   // PolicySession methods
53   AuthorizationDelegate* GetDelegate() override;
54   TPM_RC StartBoundSession(TPMI_DH_ENTITY bind_entity,
55                            const std::string& bind_authorization_value,
56                            bool enable_encryption) override;
57   TPM_RC StartUnboundSession(bool enable_encryption) override;
58   TPM_RC GetDigest(std::string* digest) override;
59   TPM_RC PolicyOR(const std::vector<std::string>& digests) override;
60   TPM_RC PolicyPCR(uint32_t pcr_index, const std::string& pcr_value) override;
61   TPM_RC PolicyCommandCode(TPM_CC command_code) override;
62   TPM_RC PolicyAuthValue() override;
63   void SetEntityAuthorizationValue(const std::string& value) override;
64 
65  private:
66   // This factory is only set in the constructor and is used to instantiate
67   // The TPM class to forward commands to the TPM chip.
68   const TrunksFactory& factory_;
69   // This field determines if this session is of type TPM_SE_TRIAL or
70   // TPM_SE_POLICY.
71   TPM_SE session_type_;
72   // This delegate is what provides authorization to commands. It is what is
73   // returned when the GetDelegate method is called.
74   HmacAuthorizationDelegate hmac_delegate_;
75   // This object is used to manage the TPM session associated with this
76   // AuthorizationSession.
77   scoped_ptr<SessionManager> session_manager_;
78 
79   friend class PolicySessionTest;
80   DISALLOW_COPY_AND_ASSIGN(PolicySessionImpl);
81 };
82 
83 }  // namespace trunks
84 
85 #endif  // TRUNKS_POLICY_SESSION_IMPL_H_
86