IBR-DTN  1.0.0
SecurityManager.cpp
Go to the documentation of this file.
1 /*
2  * SecurityManager.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
24 #include "core/BundleCore.h"
30 #include <ibrcommon/Logger.h>
31 
32 #ifdef __DEVELOPMENT_ASSERTIONS__
33 #include <cassert>
34 #endif
35 
36 namespace dtn
37 {
38  namespace security
39  {
41  {
42  static SecurityManager sec_man;
43  return sec_man;
44  }
45 
47  : _accept_only_bab(false), _accept_only_pib(false)
48  {
49  }
50 
52  {
53  }
54 
56  {
57  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "auth bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
58 
59  try {
60  // try to load the local key
62 
63  // sign the bundle with BABs
65  } catch (const SecurityKey::KeyNotFoundException &ex) {
66  throw KeyMissingException(ex.what());
67  }
68  }
69 
71  {
72  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "sign bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
73 
74  try {
75  // try to load the local key
77 
78  // sign the bundle with PIB
79  dtn::security::PayloadIntegrityBlock::sign(bundle, key, bundle.destination.getNode());
80  } catch (const SecurityKey::KeyNotFoundException &ex) {
81  throw KeyMissingException(ex.what());
82  }
83  }
84 
86  {
87  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "verify signed bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
88 
89  // iterate through all blocks
90  for (dtn::data::Bundle::iterator it = bundle.begin(); it != bundle.end();)
91  {
92  const dtn::data::Block &block = (**it);
93 
95  // payload after a PCB can not verified until the payload is decrypted
96  break;
97  }
98 
99  try {
100  const dtn::security::PayloadIntegrityBlock& pib = dynamic_cast<const dtn::security::PayloadIntegrityBlock&>(block);
101 
103 
104  // try to verify the bundle with the key for the current PIB
106 
107  // if we are the security destination
109  // remove the valid PIB
110  bundle.erase(it++);
111  } else {
112  ++it;
113  }
114 
115  // set the verify bit, after verification
117 
118  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 5) << "Bundle " << bundle.toString() << " successfully verified" << IBRCOMMON_LOGGER_ENDL;
119  continue;
121  // un-set the verify bit
123  } catch (const SecurityKey::KeyNotFoundException&) {
124  // un-set the verify bit
126  } catch (const std::bad_cast&) {
127  // current block is not a PIB
128  }
129 
130  ++it;
131  }
132  }
133 
135  {
136  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "verify authenticated bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
137 
138  // iterate over all BABs of this bundle
140  while (it.next(bundle.end()))
141  {
143 
144  // look for the right BAB-factory
145  const dtn::data::EID node = bab.getSecuritySource(bundle);
146 
147  try {
148  // try to load the key of the BAB
150 
151  // verify the bundle
153 
154  // strip all BAB of this bundle
156 
157  // set the verify bit, after verification
159 
160  // at least one BAB has been authenticated, we're done!
161  break;
162  } catch (const SecurityKey::KeyNotFoundException&) {
163  // no key for this node found
164  }
165  }
166  }
167 
169  {
170  // check if the bundle has to be decrypted, return when not
171  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadConfidentialBlock::BLOCK_TYPE) <= 0) return;
172 
173  // decrypt
174  try {
175  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "decrypt bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
176 
177  // get the encryption key
179 
180  // encrypt the payload of the bundle
182 
184  } catch (const ibrcommon::Exception &ex) {
185  throw DecryptException(ex.what());
186  }
187  }
188 
190  {
191  try {
192  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "encrypt bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
193 
194  // get the encryption key
196 
197  // encrypt the payload of the bundle
199  } catch (const ibrcommon::Exception &ex) {
200  throw EncryptException(ex.what());
201  }
202  }
203  }
204 }
static SecurityKeyManager & getInstance()
static dtn::data::EID local
Definition: BundleCore.h:79
static void encrypt(dtn::data::Bundle &bundle, const dtn::security::SecurityKey &long_key, const dtn::data::EID &source)
static void auth(dtn::data::Bundle &bundle, const dtn::security::SecurityKey &key)
void verifyIntegrity(dtn::data::Bundle &bundle) const
const dtn::data::EID getSecuritySource() const
void auth(dtn::data::Bundle &bundle) const
bool isSecurityDestination(const dtn::data::Bundle &, const dtn::data::EID &) const
void sign(dtn::data::Bundle &bundle) const
static void verify(const dtn::data::Bundle &bundle, const dtn::security::SecurityKey &key)
static SecurityManager & getInstance()
void verifyAuthentication(dtn::data::Bundle &bundle) const
static const dtn::data::block_t BLOCK_TYPE
block_list::iterator iterator
Definition: Bundle.h:76
dtn::security::SecurityKey get(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
void encrypt(dtn::data::Bundle &bundle) const
ibrcommon::find_iterator< iterator, block_t > find_iterator
Definition: Bundle.h:79
static void strip(dtn::data::Bundle &bundle, const dtn::security::SecurityKey &key)
static void verify(const dtn::data::Bundle &bundle, const SecurityKey &key)
const block_t & getType() const
Definition: Block.h:73
static void decrypt(dtn::data::Bundle &bundle, const dtn::security::SecurityKey &long_key)
static void sign(dtn::data::Bundle &bundle, const SecurityKey &key, const dtn::data::EID &destination)
void decrypt(dtn::data::Bundle &bundle) const