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 #include "trunks/hmac_session_impl.h"
18 
19 #include <string>
20 
21 #include <base/logging.h>
22 #include <base/macros.h>
23 #include <base/stl_util.h>
24 #include <openssl/rand.h>
25 
26 namespace trunks {
27 
HmacSessionImpl(const TrunksFactory & factory)28 HmacSessionImpl::HmacSessionImpl(const TrunksFactory& factory)
29     : factory_(factory) {
30   session_manager_ = factory_.GetSessionManager();
31 }
32 
~HmacSessionImpl()33 HmacSessionImpl::~HmacSessionImpl() {
34   session_manager_->CloseSession();
35 }
36 
GetDelegate()37 AuthorizationDelegate* HmacSessionImpl::GetDelegate() {
38   if (session_manager_->GetSessionHandle() == kUninitializedHandle) {
39     return nullptr;
40   }
41   return &hmac_delegate_;
42 }
43 
StartBoundSession(TPMI_DH_ENTITY bind_entity,const std::string & bind_authorization_value,bool enable_encryption)44 TPM_RC HmacSessionImpl::StartBoundSession(
45     TPMI_DH_ENTITY bind_entity,
46     const std::string& bind_authorization_value,
47     bool enable_encryption) {
48   return session_manager_->StartSession(TPM_SE_HMAC, bind_entity,
49                                         bind_authorization_value,
50                                         enable_encryption, &hmac_delegate_);
51 }
52 
StartUnboundSession(bool enable_encryption)53 TPM_RC HmacSessionImpl::StartUnboundSession(bool enable_encryption) {
54   // Starting an unbound session is the same as starting a session bound to
55   // TPM_RH_NULL. In this case, the authorization is the zero length buffer.
56   // We can therefore simply call StartBoundSession with TPM_RH_NULL as the
57   // binding entity, and the empty string as the authorization.
58   return StartBoundSession(TPM_RH_NULL, "", enable_encryption);
59 }
60 
SetEntityAuthorizationValue(const std::string & value)61 void HmacSessionImpl::SetEntityAuthorizationValue(
62     const std::string& value) {
63   hmac_delegate_.set_entity_authorization_value(value);
64 }
65 
SetFutureAuthorizationValue(const std::string & value)66 void HmacSessionImpl::SetFutureAuthorizationValue(
67     const std::string& value) {
68   hmac_delegate_.set_future_authorization_value(value);
69 }
70 
71 }  // namespace trunks
72