IBR-DTNSuite
0.10
Main Page
Namespaces
Classes
Files
File List
File Members
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
22
#include "
ibrdtn/security/ExtensionSecurityBlock.h
"
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
{
38
const
dtn::data::block_t
ExtensionSecurityBlock::BLOCK_TYPE
=
SecurityBlock::EXTENSION_SECURITY_BLOCK
;
39
40
dtn::data::Block
*
ExtensionSecurityBlock::Factory::create
()
41
{
42
return
new
ExtensionSecurityBlock
();
43
}
44
45
ExtensionSecurityBlock::ExtensionSecurityBlock
()
46
:
SecurityBlock
(
EXTENSION_SECURITY_BLOCK
,
ESB_RSA_AES128_EXT
)
47
{
48
}
49
50
ExtensionSecurityBlock::~ExtensionSecurityBlock
()
51
{
52
}
53
54
void
ExtensionSecurityBlock::encrypt
(
dtn::data::Bundle
& bundle,
const
SecurityKey
&key,
dtn::data::Bundle::iterator
it,
const
dtn::data::EID
& source,
const
dtn::data::EID
& destination)
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
];
63
createSaltAndKey
(salt, 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);
73
addKey
(esb.
_ciphersuite_params
, ephemeral_key,
ibrcommon::AES128Stream::key_size_in_bytes
, rsa_key);
74
esb.
_ciphersuite_flags
|=
CONTAINS_CIPHERSUITE_PARAMS
;
75
76
// free the rsa key
77
key.
free
(rsa_key);
78
}
79
80
void
ExtensionSecurityBlock::decrypt
(
dtn::data::Bundle
& bundle,
const
SecurityKey
&key,
dtn::data::Bundle::iterator
it)
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
90
if
(!
getKey
(block.
_ciphersuite_params
, keydata,
ibrcommon::AES128Stream::key_size_in_bytes
, rsa_key))
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
102
void
ExtensionSecurityBlock::decrypt
(
dtn::data::Bundle
& bundle,
const
SecurityKey
&key,
const
dtn::data::Number
&correlator)
103
{
104
// iterate through all extension security blocks
105
dtn::data::Bundle::find_iterator
find_it(bundle.
begin
(),
ExtensionSecurityBlock::BLOCK_TYPE
);
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
}
ibrdtn
ibrdtn
security
ExtensionSecurityBlock.cpp
Generated on Mon Jul 22 2013 15:16:01 for IBR-DTNSuite by
1.8.3.1