30 #include <openssl/rand.h>
31 #include <openssl/err.h>
32 #include <openssl/rsa.h>
33 #include <netinet/in.h>
36 #ifdef __DEVELOPMENT_ASSERTIONS__
48 for (std::set<TLV>::const_iterator iter = begin(); iter != end(); ++iter)
58 return getPayloadLength();
65 for (std::set<SecurityBlock::TLV>::const_iterator iter = begin(); iter != end(); ++iter)
67 len += (*iter).getLength();
75 for (std::set<SecurityBlock::TLV>::const_iterator iter = begin(); iter != end(); ++iter)
77 if ((*iter).getType() == type)
79 return (*iter).getValue();
88 const std::string data =
get(type);
90 if (length < data.size())
92 ::memcpy(value, data.c_str(), length);
96 ::memcpy(value, data.c_str(), data.size());
110 const std::string data(reinterpret_cast<const char *>(value), length);
131 return _value.getLength() +
sizeof(char);
139 for (std::set<SecurityBlock::TLV>::const_iterator iter = tlvlist.begin(); iter != tlvlist.end(); ++iter)
152 while (length > read_length)
165 return (_type < tlv._type);
170 return (_type == tlv._type);
175 stream.put((
char)tlv._type);
176 stream << tlv._value;
184 stream >> tlv._value;
195 : Block(type), _ciphersuite_id(0), _ciphersuite_flags(0), _correlator(0)
231 if (
_eids.size() > 0)
233 set(Block::BLOCK_CONTAINS_EIDS,
true);
237 set(Block::BLOCK_CONTAINS_EIDS,
false);
276 bool return_val =
false;
401 #ifdef __DEVELOPMENT_ASSERTIONS__
408 #ifdef __DEVELOPMENT_ASSERTIONS__
412 assert(_ciphersuite_flags < 32);
418 if (
_eids.size() == 0)
426 if (_ciphersuite_flags & SecurityBlock::CONTAINS_SECURITY_SOURCE)
428 if (
_eids.size() < 2)
435 if (
_eids.size() == 0)
449 #ifdef __DEVELOPMENT_ASSERTIONS__
450 assert(_ciphersuite_params.getLength() > 0);
457 #ifdef __DEVELOPMENT_ASSERTIONS__
458 assert(_security_result.getLength() > 0);
509 #ifdef __DEVELOPMENT_ASSERTIONS__
518 if (!RAND_bytes(reinterpret_cast<unsigned char *>(&salt),
sizeof(uint32_t)))
521 ERR_print_errors_fp(stderr);
523 if (!RAND_bytes(key, static_cast<int>(key_size)))
526 ERR_print_errors_fp(stderr);
533 #ifdef __DEVELOPMENT_ASSERTIONS__
536 std::vector<unsigned char> encrypted_key(RSA_size(rsa));
537 int encrypted_key_len = RSA_public_encrypt(static_cast<int>(key_size), key, &encrypted_key[0], rsa, RSA_PKCS1_OAEP_PADDING);
538 if (encrypted_key_len == -1)
541 ERR_print_errors_fp(stderr);
551 const unsigned char *encrypted_key =
reinterpret_cast<const unsigned char*
>(key_string.c_str());
552 std::vector<unsigned char> the_key(RSA_size(rsa));
553 RSA_blinding_on(rsa, NULL);
554 int plaintext_key_len = RSA_private_decrypt(static_cast<int>(key_string.size()), encrypted_key, &the_key[0], rsa, RSA_PKCS1_OAEP_PADDING);
555 RSA_blinding_off(rsa);
556 if (plaintext_key_len == -1)
559 ERR_print_errors_fp(stderr);
562 #ifdef __DEVELOPMENT_ASSERTIONS__
565 std::copy(&the_key[0], &the_key[key_size], key);
572 std::list<dtn::data::EID> their_eids = from.getEIDList();
573 std::list<dtn::data::EID>::iterator it = their_eids.begin();
575 while (it != their_eids.end() && skip > 0)
581 for (; it != their_eids.end(); ++it)
587 uint32_t nsalt = htonl(salt);
624 std::stringstream plaintext;
626 decrypt << block_data << std::flush;
646 ddser >> plaintext_block;
653 ddser >> plaintext_block;
664 copyEID(plaintext_block, plaintext_block, skip);
668 ddser >> plaintext_block;
679 copyEID(plaintext_block, plaintext_block, skip);
688 std::stringstream ss;
689 ss << offset << range;