31 #include <openssl/rand.h>
32 #include <openssl/err.h>
33 #include <openssl/rsa.h>
36 #ifdef __DEVELOPMENT_ASSERTIONS__
51 for (std::set<TLV>::const_iterator iter = begin(); iter != end(); ++iter)
61 return getPayloadLength();
68 for (std::set<SecurityBlock::TLV>::const_iterator iter = begin(); iter != end(); ++iter)
70 len += (*iter).getLength();
78 for (std::set<SecurityBlock::TLV>::const_iterator iter = begin(); iter != end(); ++iter)
80 if ((*iter).getType() == type)
82 return (*iter).getValue();
91 const std::string data =
get(type);
93 if (length < data.size())
95 ::memcpy(value, data.c_str(), length);
99 ::memcpy(value, data.c_str(), data.size());
113 const std::string data(reinterpret_cast<const char *>(value), length);
134 return _value.getLength() +
sizeof(char);
142 for (std::set<SecurityBlock::TLV>::const_iterator iter = tlvlist.begin(); iter != tlvlist.end(); ++iter)
155 while (length > read_length)
168 return (_type < tlv._type);
173 return (_type == tlv._type);
178 stream.put((
char)tlv._type);
179 stream << tlv._value;
187 stream >> tlv._value;
198 : Block(type), _ciphersuite_id(0), _ciphersuite_flags(0), _correlator(0)
234 if (
_eids.size() > 0)
236 set(Block::BLOCK_CONTAINS_EIDS,
true);
240 set(Block::BLOCK_CONTAINS_EIDS,
false);
279 bool return_val =
false;
404 #ifdef __DEVELOPMENT_ASSERTIONS__
411 #ifdef __DEVELOPMENT_ASSERTIONS__
415 assert(_ciphersuite_flags < 32);
421 if (
_eids.size() == 0)
429 if (_ciphersuite_flags & SecurityBlock::CONTAINS_SECURITY_SOURCE)
431 if (
_eids.size() < 2)
438 if (
_eids.size() == 0)
452 #ifdef __DEVELOPMENT_ASSERTIONS__
453 assert(_ciphersuite_params.getLength() > 0);
460 #ifdef __DEVELOPMENT_ASSERTIONS__
461 assert(_security_result.getLength() > 0);
485 if (include_security_result) {
497 #ifdef __DEVELOPMENT_ASSERTIONS__
506 if (!RAND_bytes(reinterpret_cast<unsigned char *>(&salt),
sizeof(uint32_t)))
509 ERR_print_errors_fp(stderr);
511 if (!RAND_bytes(key, static_cast<int>(key_size)))
514 ERR_print_errors_fp(stderr);
521 #ifdef __DEVELOPMENT_ASSERTIONS__
524 std::vector<unsigned char> encrypted_key(RSA_size(rsa));
525 int encrypted_key_len = RSA_public_encrypt(static_cast<int>(key_size), key, &encrypted_key[0], rsa, RSA_PKCS1_OAEP_PADDING);
526 if (encrypted_key_len == -1)
529 ERR_print_errors_fp(stderr);
539 const unsigned char *encrypted_key =
reinterpret_cast<const unsigned char*
>(key_string.c_str());
540 std::vector<unsigned char> the_key(RSA_size(rsa));
541 RSA_blinding_on(rsa, NULL);
542 int plaintext_key_len = RSA_private_decrypt(static_cast<int>(key_string.size()), encrypted_key, &the_key[0], rsa, RSA_PKCS1_OAEP_PADDING);
543 RSA_blinding_off(rsa);
544 if (plaintext_key_len == -1)
547 ERR_print_errors_fp(stderr);
550 #ifdef __DEVELOPMENT_ASSERTIONS__
553 std::copy(&the_key[0], &the_key[key_size], key);
560 std::list<dtn::data::EID> their_eids = from.getEIDList();
561 std::list<dtn::data::EID>::iterator it = their_eids.begin();
563 while (it != their_eids.end() && skip > 0)
569 for (; it != their_eids.end(); ++it)
612 std::stringstream plaintext;
614 decrypt << block_data << std::flush;
634 ddser >> plaintext_block;
641 ddser >> plaintext_block;
652 copyEID(plaintext_block, plaintext_block, skip);
656 ddser >> plaintext_block;
667 copyEID(plaintext_block, plaintext_block, skip);
676 std::stringstream ss;
677 ss << offset << range;