IBR-DTN  1.0.0
KeyExchangeSession.h
Go to the documentation of this file.
1 /*
2  * KeyExchangeSession.h
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 
23 #ifndef KEYEXCHANGESESSION_H_
24 #define KEYEXCHANGESESSION_H_
25 
26 #include "config.h"
27 #include <ibrdtn/data/EID.h>
29 #include <vector>
30 #include <string>
31 #include <typeinfo>
32 
33 namespace dtn
34 {
35  namespace security
36  {
38  {
39  static const std::string TAG;
40 
41  public:
43  {
44  public:
45  virtual ~SessionState() = 0;
46  };
47 
48  KeyExchangeSession(int protocol, const dtn::data::EID &peer, unsigned int uniqueId, SessionState *state = NULL);
49  virtual ~KeyExchangeSession();
50 
55 
59  void touch();
60 
64  const dtn::data::EID& getPeer() const;
65 
69  int getProtocol() const;
70 
74  unsigned int getUniqueId() const;
75 
79  const std::string& getSessionKey() const;
80 
84  static std::string getSessionKey(const dtn::data::EID &peer, unsigned int uniqueId);
85 
90 
94  void putKey(const std::string &data, const dtn::security::SecurityKey::KeyType type, const dtn::security::SecurityKey::TrustLevel trust) const;
95 
99  void clearKeys() const;
100 
105  template<class T>
106  T& getState()
107  {
108  if (!_state) throw ibrcommon::Exception("no state found");
109 
110  try {
111  return dynamic_cast<T&>(*_state);
112  } catch (std::bad_cast&) {
113  throw ibrcommon::Exception("State does not match the exchange protocol.");
114  }
115  }
116 
117  private:
118  int _protocol;
119  unsigned int _unique_id;
120  dtn::data::EID _peer;
121  std::string _session_key;
122 
123  // a state object which contains the state of the exchange approach
124  SessionState *_state;
125 
126  dtn::data::Timestamp _expiration;
127  };
128 
129  } /* namespace security */
130 } /* namespace dtn */
131 #endif /* KEYEXCHANGESESSION_H_ */
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
dtn::data::Timestamp getExpiration() const