IBR-DTN  1.0.0
KeyExchangeSession.cpp
Go to the documentation of this file.
1 /*
2  * KeyExchangeSession.cpp
3  *
4  * Copyright (C) 2014 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Thomas Schrader <schrader.thomas@gmail.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
25 #include <ibrdtn/utils/Clock.h>
26 #include <ibrcommon/Logger.h>
27 
28 namespace dtn
29 {
30  namespace security
31  {
32  const std::string KeyExchangeSession::TAG = "KeyExchangeSession";
33 
35  {
36  }
37 
38  KeyExchangeSession::KeyExchangeSession(int protocol, const dtn::data::EID &peer, unsigned int uniqueId, SessionState *state)
39  : _protocol(protocol), _unique_id(uniqueId), _peer(peer.getNode()), _state(state), _expiration(0)
40  {
41  // generate session key
42  _session_key = getSessionKey(_peer, _unique_id);
43 
44  // set a expiration time (10 minutes)
45  _expiration = dtn::utils::Clock::getMonotonicTimestamp() + 600;
46  }
47 
49  {
50  // clear session state
51  if (_state != NULL) delete _state;
52 
53  // delete all session files
54  clearKeys();
55  }
56 
58  {
59  return _expiration;
60  }
61 
63  {
64  // set new expiration time
65  _expiration = dtn::utils::Clock::getMonotonicTimestamp() + 600;
66  }
67 
68  std::string KeyExchangeSession::getSessionKey(const dtn::data::EID &peer, unsigned int uniqueId)
69  {
70  // generate session key
71  std::stringstream sstm;
72  sstm << uniqueId << "." << peer.getString();
73 
74  return sstm.str();
75  }
76 
78  {
79  return _peer;
80  }
81 
83  {
84  return _protocol;
85  }
86 
87  unsigned int KeyExchangeSession::getUniqueId() const
88  {
89  return _unique_id;
90  }
91 
92  const std::string& KeyExchangeSession::getSessionKey() const
93  {
94  return _session_key;
95  }
96 
98  {
99  unsigned int id = getUniqueId();
100  std::string prefix((char*)&id, sizeof id);
101 
102  return SecurityKeyManager::getInstance().get(prefix, _peer, type);
103  }
104 
106  {
107  unsigned int id = getUniqueId();
108  std::string prefix((char*)&id, sizeof id);
109 
111 
112  // assign key type
113  keydata.type = type;
114 
115  // assign reference
116  keydata.reference = _peer;
117 
118  // assign trust level
119  keydata.trustlevel = trust;
120 
121  // set protocol flags
122  keydata.flags = (1 << getProtocol());
123 
124  // store security key
125  SecurityKeyManager::getInstance().store(prefix, keydata, data);
126  }
127 
129  {
130  std::list<dtn::security::SecurityKey::KeyType> types;
134 
135  for (std::list<dtn::security::SecurityKey::KeyType>::const_iterator it = types.begin(); it != types.end(); ++it)
136  {
137  try {
139  } catch (const SecurityKey::KeyNotFoundException &e) {
140  // key not found
141  IBRCOMMON_LOGGER_DEBUG_TAG(TAG, 25) << e.what() << IBRCOMMON_LOGGER_ENDL;
142  }
143  }
144  }
145  } /* namespace security */
146 } /* namespace dtn */
static SecurityKeyManager & getInstance()
KeyExchangeSession(int protocol, const dtn::data::EID &peer, unsigned int uniqueId, SessionState *state=NULL)
const dtn::data::EID & getPeer() const
dtn::security::SecurityKey getKey(const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
const std::string & getSessionKey() const
void putKey(const std::string &data, const dtn::security::SecurityKey::KeyType type, const dtn::security::SecurityKey::TrustLevel trust) const
void remove(const SecurityKey &key)
dtn::security::SecurityKey get(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
dtn::data::EID reference
Definition: SecurityKey.h:74
dtn::data::SDNV< unsigned int > flags
Definition: SecurityKey.h:86
std::string getString() const
Definition: EID.cpp:374
static dtn::data::Timestamp getMonotonicTimestamp()
Definition: Clock.cpp:175
void store(const dtn::security::SecurityKey &key, const std::string &data)
dtn::data::Timestamp getExpiration() const