IBR-DTNSuite  0.10
ExtensionSecurityBlock.cpp
Go to the documentation of this file.
1 /*
2  * ExtensionSecurityBlock.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 
23 #include <ibrcommon/Logger.h>
24 #include "ibrdtn/data/Serializer.h"
25 #include "ibrdtn/data/Bundle.h"
26 #include <openssl/err.h>
27 #include <openssl/rsa.h>
28 #include <algorithm>
29 
30 #ifdef __DEVELOPMENT_ASSERTIONS__
31 #include <cassert>
32 #endif
33 
34 namespace dtn
35 {
36  namespace security
37  {
39 
41  {
42  return new ExtensionSecurityBlock();
43  }
44 
47  {
48  }
49 
51  {
52  }
53 
55  {
56  uint32_t salt = 0;
57 
58  // load the rsa key
59  RSA *rsa_key = key.getRSA();
60 
61  // key used for encrypting the block. the key will be encrypted using RSA
62  unsigned char ephemeral_key[ibrcommon::AES128Stream::key_size_in_bytes];
64 
65  dtn::security::ExtensionSecurityBlock& esb = SecurityBlock::encryptBlock<ExtensionSecurityBlock>(bundle, it, salt, ephemeral_key);
66 
67  // set the source and destination address of the new block
68  if (source != bundle.source) esb.setSecuritySource( source );
69  if (destination != bundle.destination) esb.setSecurityDestination( destination );
70 
71  // encrypt the ephemeral key and place it in _ciphersuite_params
72  addSalt(esb._ciphersuite_params, salt);
75 
76  // free the rsa key
77  key.free(rsa_key);
78  }
79 
81  {
82  const dtn::security::ExtensionSecurityBlock& block = dynamic_cast<const dtn::security::ExtensionSecurityBlock&>(**it);
83 
84  // load the rsa key
85  RSA *rsa_key = key.getRSA();
86 
87  // get key, convert with reinterpret_cast
88  unsigned char keydata[ibrcommon::AES128Stream::key_size_in_bytes];
89 
91  {
92  IBRCOMMON_LOGGER_ex(critical) << "could not get symmetric key decrypted" << IBRCOMMON_LOGGER_ENDL;
93  throw ibrcommon::Exception("could not extract the key");
94  }
95 
96  // get salt, convert with stringstream
97  uint32_t salt = getSalt(block._ciphersuite_params);
98 
99  SecurityBlock::decryptBlock(bundle, it, salt, keydata);
100  }
101 
103  {
104  // iterate through all extension security blocks
106  while (find_it.next(bundle.end()))
107  {
108  const dtn::security::ExtensionSecurityBlock &esb = dynamic_cast<const dtn::security::ExtensionSecurityBlock&>(**find_it);
109 
110  if ((correlator == 0) || (correlator == esb._correlator))
111  {
112  decrypt(bundle, key, find_it);
113  }
114  }
115  }
116  }
117 }