IBR-DTNSuite  0.12
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"
27 #include <ibrcommon/Logger.h>
28 
29 #ifdef __DEVELOPMENT_ASSERTIONS__
30 #include <cassert>
31 #endif
32 
33 namespace dtn
34 {
35  namespace security
36  {
38  {
39  static SecurityManager sec_man;
40  return sec_man;
41  }
42 
44  : _accept_only_bab(false), _accept_only_pib(false)
45  {
46  }
47 
49  {
50  }
51 
53  {
54  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "auth bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
55 
56  try {
57  // try to load the local key
59 
60  // sign the bundle with BABs
62  } catch (const SecurityKeyManager::KeyNotFoundException &ex) {
63  throw KeyMissingException(ex.what());
64  }
65  }
66 
68  {
69  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "sign bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
70 
71  try {
72  // try to load the local key
74 
75  // sign the bundle with PIB
76  dtn::security::PayloadIntegrityBlock::sign(bundle, key, bundle.destination.getNode());
77  } catch (const SecurityKeyManager::KeyNotFoundException &ex) {
78  throw KeyMissingException(ex.what());
79  }
80  }
81 
83  {
84  verifyBAB(bundle);
85  verifyPIB(bundle);
86  }
87 
89  {
90  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "verify signed bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
91 
92  // iterate through all blocks
93  for (dtn::data::Bundle::iterator it = bundle.begin(); it != bundle.end();)
94  {
95  const dtn::data::Block &block = (**it);
96 
98  // payload after a PCB can not verified until the payload is decrypted
99  break;
100  }
101 
102  try {
103  const dtn::security::PayloadIntegrityBlock& pib = dynamic_cast<const dtn::security::PayloadIntegrityBlock&>(block);
104 
106 
107  // try to verify the bundle with the key for the current PIB
109 
110  // if we are the security destination
112  // remove the valid PIB
113  bundle.erase(it++);
114  } else {
115  ++it;
116  }
117 
118  // set the verify bit, after verification
120 
121  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 5) << "Bundle " << bundle.toString() << " successfully verified" << IBRCOMMON_LOGGER_ENDL;
122  continue;
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;
163  // no key for this node found
164  }
165  }
166  }
167 
169  {
170  // do a fast verify without manipulating the bundle
172 
174  {
175  // check if the bundle is encrypted and throw an exception if not
176  //throw VerificationFailedException("Bundle is not encrypted");
177  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "encryption required, verify bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
178 
179  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadConfidentialBlock::BLOCK_TYPE) == 0)
180  throw VerificationFailedException("No PCB available!");
181  }
182 
184  {
185  // check if the bundle is signed and throw an exception if not
186  //throw VerificationFailedException("Bundle is not signed");
187  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "signature required, verify bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
188 
189  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadIntegrityBlock::BLOCK_TYPE) == 0)
190  throw VerificationFailedException("No PIB available!");
191  }
192 
194  {
195  // check if the bundle is signed and throw an exception if not
196  //throw VerificationFailedException("Bundle is not signed");
197  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "authentication required, verify bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
198 
199  if (std::count(bundle.begin(), bundle.end(), dtn::security::BundleAuthenticationBlock::BLOCK_TYPE) == 0)
200  throw VerificationFailedException("No BAB available!");
201  }
202  }
203 
205  {
206  // check if the bundle has to be decrypted, return when not
207  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadConfidentialBlock::BLOCK_TYPE) <= 0) return;
208 
209  // decrypt
210  try {
211  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "decrypt bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
212 
213  // get the encryption key
215 
216  // encrypt the payload of the bundle
218 
220  } catch (const ibrcommon::Exception &ex) {
221  throw DecryptException(ex.what());
222  }
223  }
224 
226  {
227  try {
228  IBRCOMMON_LOGGER_DEBUG_TAG("SecurityManager", 10) << "encrypt bundle: " << bundle.toString() << IBRCOMMON_LOGGER_ENDL;
229 
230  // get the encryption key
232 
233  // encrypt the payload of the bundle
235  } catch (const ibrcommon::Exception &ex) {
236  throw EncryptException(ex.what());
237  }
238  }
239  }
240 }