Beispiel #1
0
	void RSASHA256Stream::reset()
	{
		EVP_MD_CTX_cleanup(&_ctx);

		EVP_MD_CTX_init(&_ctx);

		if (!_verify)
		{
			if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the signature function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
		else
		{
			if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
			{
				IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the verfication function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}

		_sign_valid = false;
	}
Beispiel #2
0
		void IPNDAgent::listen(const ibrcommon::vinterface &iface) throw ()
		{
			// create sockets for all addresses on the interface
			std::list<ibrcommon::vaddress> addrs = iface.getAddresses();

			for (std::list<ibrcommon::vaddress>::iterator iter = addrs.begin(); iter != addrs.end(); ++iter) {
				ibrcommon::vaddress &addr = (*iter);

				try {
					// handle the addresses according to their family
					switch (addr.family()) {
					case AF_INET:
					case AF_INET6:
					{
						ibrcommon::udpsocket *sock = new ibrcommon::udpsocket(addr);
						if (_send_socket_state) sock->up();
						_send_socket.add(sock, iface);
						break;
					}
					default:
						break;
					}
				} catch (const ibrcommon::vaddress::address_exception &ex) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;
				} catch (const ibrcommon::socket_exception &ex) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;
				}
			}
		}
Beispiel #3
0
		void IPNDAgent::send(const DiscoveryAnnouncement &a, const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr)
		{
			// serialize announcement
			stringstream ss; ss << a;
			const std::string data = ss.str();

			ibrcommon::socketset fds = _send_socket.get(iface);

			// send out discovery announcements on all bound sockets
			// (hopefully only one per interface)
			for (ibrcommon::socketset::const_iterator iter = fds.begin(); iter != fds.end(); ++iter)
			{
				try {
					ibrcommon::udpsocket &sock = dynamic_cast<ibrcommon::udpsocket&>(**iter);

					try {
						// prevent broadcasting in the wrong address family
						if (addr.family() != sock.get_family()) continue;

						sock.sendto(data.c_str(), data.length(), 0, addr);
					} catch (const ibrcommon::socket_exception &e) {
						IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 5) << "can not send message to " << addr.toString() << " via " << sock.get_address().toString() << "/" << iface.toString() << "; socket exception: " << e.what() << IBRCOMMON_LOGGER_ENDL;
					} catch (const ibrcommon::vaddress::address_exception &ex) {
						IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;
					}
				} catch (const std::bad_cast&) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "Socket for sending isn't a udpsocket." << IBRCOMMON_LOGGER_ENDL;
				}
			}
		}
		void TCPConnection::eventConnectionUp(const dtn::streams::StreamContactHeader &header) throw ()
		{
			_peer = header;

			// copy old attributes and urls to the new node object
			Node n_old = _node;
			_node = Node(header._localeid);
			_node += n_old;

			// check if the peer has the same EID
			if (_node.getEID() == dtn::core::BundleCore::getInstance().local)
			{
				// abort the connection
				shutdown();

				IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, warning) << "connection to local endpoint rejected" << IBRCOMMON_LOGGER_ENDL;
				return;
			}

			_keepalive_timeout = header._keepalive * 1000;

			try {
				// initiate extended handshake (e.g. TLS)
				initiateExtendedHandshake();
			} catch (const ibrcommon::Exception &ex) {
				IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;

				// abort the connection
				shutdown();
				return;
			}

			// set the timer
			timeval timeout;
			timerclear(&timeout);

			// set the incoming timer if set (> 0)
			if (_peer._keepalive > 0)
			{
				timeout.tv_sec = header._keepalive * 2;
			}

			// change time-out
			_socket_stream->setTimeout(timeout);

			try {
				// enable idle timeout
				size_t _idle_timeout = dtn::daemon::Configuration::getInstance().getNetwork().getTCPIdleTimeout();
				if (_idle_timeout > 0)
				{
					(*getProtocolStream()).enableIdleTimeout(_idle_timeout);
				}
			} catch (const ibrcommon::Exception&) {};

			// raise up event
			ConnectionEvent::raise(ConnectionEvent::CONNECTION_UP, _node);
		}
Beispiel #5
0
		void TCPConnection::initiateExtendedHandshake() throw (ibrcommon::Exception)
		{
#ifdef WITH_TLS
			/* if both nodes support TLS, activate it */
			if (_peer._flags.getBit(dtn::streams::StreamContactHeader::REQUEST_TLS)
					&& _flags.getBit(dtn::streams::StreamContactHeader::REQUEST_TLS))
			{
				try{
					ibrcommon::TLSStream &tls = dynamic_cast<ibrcommon::TLSStream&>(*_sec_stream);
					X509 *peer_cert = tls.activate();

					// check the full EID first
					const std::string cn = _peer.getEID().getString();

					try {
						try {
							dtn::security::SecurityCertificateManager::validateSubject(peer_cert, cn);
						} catch (const dtn::security::SecurityCertificateException &ex) {
							// check using the hostname
							std::string weak_cn = _peer.getEID().getHost();

							// strip leading "//"
							if (weak_cn.find_first_of("//") == 0) {
								weak_cn = weak_cn.substr(2, weak_cn.length() - 2);
							}

							try {
								dtn::security::SecurityCertificateManager::validateSubject(peer_cert, weak_cn);
							} catch (const dtn::security::SecurityCertificateException &ex_weak) {
								throw ex;
							}
						}
					} catch (const dtn::security::SecurityCertificateException &ex) {
						IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;
						throw ibrcommon::TLSCertificateVerificationException(ex.what());
					}
				} catch (const std::exception&) {
					if (dtn::daemon::Configuration::getInstance().getSecurity().TLSRequired()){
						/* close the connection */
						IBRCOMMON_LOGGER_DEBUG_TAG(TCPConnection::TAG, 20) << "TLS failed, closing the connection." << IBRCOMMON_LOGGER_ENDL;
						throw;
					} else {
						IBRCOMMON_LOGGER_DEBUG_TAG(TCPConnection::TAG, 20) << "TLS failed, continuing unauthenticated." << IBRCOMMON_LOGGER_ENDL;
					}
				}
			} else {
				/* TLS not supported by both Nodes, check if its required */
				if (dtn::daemon::Configuration::getInstance().getSecurity().TLSRequired()){
					/* close the connection */
					throw ibrcommon::TLSException("TLS not supported by peer.");
				} else if(_flags & dtn::streams::StreamContactHeader::REQUEST_TLS){
					IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, notice) << "TLS not supported by peer. Continuing without TLS." << IBRCOMMON_LOGGER_ENDL;
				}
				/* else: this node does not support TLS, should have already printed a warning */
			}
#endif
		}
Beispiel #6
0
		void IPNDAgent::join(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr) throw ()
		{
			IBRCOMMON_LOGGER_DEBUG_TAG(TAG, 10) << "Join on " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << IBRCOMMON_LOGGER_ENDL;

			// only join IPv6 and IPv4 addresses
			if ((addr.family() != AF_INET) && (addr.family() != AF_INET6)) return;

			// do not join on loopback interfaces
			if (addr.isLocal()) return;

			// create a multicast socket and bind to given addr
			ibrcommon::multicastsocket *msock = new ibrcommon::multicastsocket(addr);

			// if we are in UP state
			if (_state) {
				try {
					// bring up
					msock->up();

					// listen to multicast addresses
					for (std::set<ibrcommon::vaddress>::const_iterator it_addr = _destinations.begin(); it_addr != _destinations.end(); ++it_addr)
					{
                        if (msock->get_family() != it_addr->family())
                            continue;
                        
						try {
							msock->join(*it_addr, iface);
						} catch (const ibrcommon::socket_raw_error &e) {
							if (e.error() == EADDRINUSE) {
								// silent error
							} else if (e.error() == 92) {
								// silent error - protocol not available
							} else {
								IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << "Join to " << (*it_addr).toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL;
							}
						} catch (const ibrcommon::socket_exception &e) {
							IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "Join to " << (*it_addr).toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL;
						}
					}
				} catch (const ibrcommon::socket_exception &ex) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "Join failed on " << iface.toString() << " (" << addr.toString() << ", family: " << addr.family() << ")" << "; " << ex.what() << IBRCOMMON_LOGGER_ENDL;
					delete msock;
					return;
				}
			}

			// add multicast socket to _socket
			_socket.add(msock, iface);
		}
Beispiel #7
0
		void SQLiteBundleSet::expire(const dtn::data::Timestamp timestamp) throw ()
		{
			// we can not expire bundles if we have no idea of time
			if (timestamp == 0) return;

			// do not expire if its not the time
			if (_next_expiration > timestamp) return;

			// look for expired bundles and announce them in the listener
			if (_listener != NULL) {
				try {
					SQLiteDatabase::Statement st(_sqldb._database, SQLiteDatabase::_sql_queries[SQLiteDatabase::BUNDLE_SET_GET_EXPIRED]);
					sqlite3_bind_int64(*st, 1, _set_id);

					// set expiration timestamp
					sqlite3_bind_int64(*st, 2, timestamp.get<uint64_t>());

					while (st.step() == SQLITE_ROW)
					{
						dtn::data::BundleID id;
						get_bundleid(st, id);

						const dtn::data::MetaBundle bundle = dtn::data::MetaBundle::create(id);

						// raise bundle expired event
						_listener->eventBundleExpired(bundle);
					}
				} catch (const SQLiteDatabase::SQLiteQueryException &ex) {
					IBRCOMMON_LOGGER_TAG(SQLiteDatabase::TAG, error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
				}
			}

			// delete expired bundles
			try {
				SQLiteDatabase::Statement st(_sqldb._database, SQLiteDatabase::_sql_queries[SQLiteDatabase::BUNDLE_SET_EXPIRE]);
				sqlite3_bind_int64(*st, 1, _set_id);

				// set expiration timestamp
				sqlite3_bind_int64(*st, 2, timestamp.get<uint64_t>());

				st.step();
			} catch (const SQLiteDatabase::SQLiteQueryException &ex) {
				IBRCOMMON_LOGGER_TAG(SQLiteDatabase::TAG, error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}

			// rebuild the bloom filter
			rebuild_bloom_filter();
		}
		dtn::data::Bundle MemoryBundleStorage::get(const dtn::data::BundleID &id)
		{
			try {
				ibrcommon::MutexLock l(_bundleslock);

				for (bundle_list::const_iterator iter = _bundles.begin(); iter != _bundles.end(); ++iter)
				{
					const dtn::data::Bundle &bundle = (*iter);
					if (id == bundle)
					{
						if (_faulty) {
							throw dtn::SerializationFailedException("bundle get failed due to faulty setting");
						}

						return bundle;
					}
				}
			} catch (const dtn::SerializationFailedException &ex) {
				// bundle loading failed
				IBRCOMMON_LOGGER_TAG(MemoryBundleStorage::TAG, error) << "Error while loading bundle data: " << ex.what() << IBRCOMMON_LOGGER_ENDL;

				// the bundle is broken, delete it
				remove(id);

				throw BundleStorage::BundleLoadException();
			}

			throw NoBundleFoundException();
		}
Beispiel #9
0
		void Clock::setOffset(const struct timeval &tv)
		{
			if (!Clock::shouldModifyClock())
			{
				if (!Clock::_offset_init)
				{
					timerclear(&Clock::_offset);
					Clock::_offset_init = true;
				}

				timeradd(&Clock::_offset, &tv, &Clock::_offset);
				IBRCOMMON_LOGGER_TAG("Clock", info) << "new local offset: " << Clock::toDouble(_offset) << "s" << IBRCOMMON_LOGGER_ENDL;
			}
			else
			{
				struct timezone tz;
				struct timeval now;
				::gettimeofday(&now, &tz);

				// adjust by the offset
				timersub(&now, &tv, &now);

#ifndef __WIN32__
				// set the local clock to the new timestamp
				::settimeofday(&now, &tz);
#endif
			}
		}
		bool PayloadConfidentialBlock::decryptPayload(dtn::data::Bundle& bundle, const unsigned char ephemeral_key[ibrcommon::AES128Stream::key_size_in_bytes], const uint32_t salt)
		{
			// TODO handle fragmentation
			PayloadConfidentialBlock& pcb = bundle.find<PayloadConfidentialBlock>();
			dtn::data::PayloadBlock& plb = bundle.find<dtn::data::PayloadBlock>();

			// the array for the extracted iv
			unsigned char iv[ibrcommon::AES128Stream::iv_len];
			pcb._ciphersuite_params.get(SecurityBlock::initialization_vector, iv, ibrcommon::AES128Stream::iv_len);

			// the array for the extracted tag
			unsigned char tag[ibrcommon::AES128Stream::tag_len];
			pcb._security_result.get(SecurityBlock::PCB_integrity_check_value, tag, ibrcommon::AES128Stream::tag_len);

			// get the reference to the corresponding BLOB object
			ibrcommon::BLOB::Reference blobref = plb.getBLOB();

			// decrypt the payload and get the integrity signature (tag)
			{
				ibrcommon::BLOB::iostream stream = blobref.iostream();
				ibrcommon::AES128Stream decrypt(ibrcommon::CipherStream::CIPHER_DECRYPT, *stream, ephemeral_key, salt, iv);
				((ibrcommon::CipherStream&)decrypt).decrypt(*stream);

				// get the decrypt tag
				if (!decrypt.verify(tag))
				{
					IBRCOMMON_LOGGER_TAG("PayloadConfidentialBlock", error) << "integrity signature of the decrypted payload is invalid" << IBRCOMMON_LOGGER_ENDL;
					return false;
				}
			}

			return true;
		}
Beispiel #11
0
		void IPNDAgent::join(const ibrcommon::vinterface &iface) throw ()
		{
			// register as discovery handler for this interface
			dtn::core::BundleCore::getInstance().getDiscoveryAgent().registerService(iface, this);

			// do not create sockets for any interface
			if (!iface.isAny()) {
				// subscribe to NetLink events on our interfaces
				ibrcommon::LinkManager::getInstance().addEventListener(iface, this);

				/**
				 * create sockets for each address on the interface
				 */
				const std::list<ibrcommon::vaddress> addrs = iface.getAddresses();

				for (std::list<ibrcommon::vaddress>::const_iterator it = addrs.begin(); it != addrs.end(); ++it)
				{
					const ibrcommon::vaddress addr = (*it);

					// join to all multicast addresses on this interface
					join(iface, addr);
				}
			}

			/**
			 * subscribe to multicast address on this interface using the sockets bound to any address
			 */
			const ibrcommon::vinterface any_iface(ibrcommon::vinterface::ANY);
			ibrcommon::socketset anysocks = _socket.get(any_iface);

			for (ibrcommon::socketset::iterator it = anysocks.begin(); it != anysocks.end(); ++it)
			{
				ibrcommon::multicastsocket *msock = dynamic_cast<ibrcommon::multicastsocket*>(*it);
				if (msock == NULL) continue;

				for (std::set<ibrcommon::vaddress>::const_iterator addr_it = _destinations.begin(); addr_it != _destinations.end(); ++addr_it)
				{
					const ibrcommon::vaddress &addr = (*addr_it);

					// join only if family matches
					if (addr.family() != msock->get_family()) continue;

					try {
						msock->join(addr, iface);

						IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "Joined " << addr.toString() << " on " << iface.toString() << IBRCOMMON_LOGGER_ENDL;
					} catch (const ibrcommon::socket_raw_error &e) {
						if (e.error() == EADDRINUSE) {
							// silent error
						} else if (e.error() == 92) {
							// silent error - protocol not available
						} else {
							IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << "Join to " << addr.toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL;
						}
					} catch (const ibrcommon::socket_exception &e) {
						IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "Join to " << addr.toString() << " failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL;
					}
				}
			}
		}
Beispiel #12
0
void sighandler_func(int signal)
{
	IBRCOMMON_LOGGER_TAG(TAG,notice) << "got signal " << signal << IBRCOMMON_LOGGER_ENDL;
	switch (signal)
	{
	case SIGTERM:
	case SIGINT:
	{
		//stop waiting and stop running, on SIGINT or SIGTERM
		ibrcommon::MutexLock l(_wait_cond);
		_running = false;
		_wait_cond.signal(true);
		break;
	}
#ifndef __WIN32__
	case SIGUSR1:
	{
		//stop waiting on SIGUSR1 -> "quickscan"
		ibrcommon::MutexLock l(_wait_cond);
		_wait_abort = true;
		_wait_cond.signal(true);
		break;
	}
#endif
	default:
		break;
	}
}
Beispiel #13
0
		void EventDebugger::raiseEvent(const dtn::core::Event *evt) throw ()
		{
			// print event
			if (evt->isLoggable())
			{
				IBRCOMMON_LOGGER_TAG(evt->getName(), notice) << evt->getMessage() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #14
0
		void IPNDAgent::componentUp() throw ()
		{
			// routine checked for throw() on 15.02.2013

			try {
				// setup the receive socket
				_recv_socket.up();
			} catch (const ibrcommon::socket_exception &ex) {
				IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}

			// join multicast groups
			try {
				ibrcommon::MutexLock l(_interface_lock);

				for (std::set<ibrcommon::vinterface>::const_iterator it_iface = _interfaces.begin(); it_iface != _interfaces.end(); ++it_iface)
				{
					const ibrcommon::vinterface &iface = (*it_iface);

					// subscribe to NetLink events on our interfaces
					ibrcommon::LinkManager::getInstance().addEventListener(iface, this);

					// listen on the interface
					listen(iface);

					// join to all multicast addresses on this interface
					join_interface(iface);
				}
			} catch (std::bad_cast&) {
				throw ibrcommon::socket_exception("no multicast socket found");
			}

			try {
				// setup all send sockets
				_send_socket.up();

				// mark the send socket as up
				_send_socket_state = true;
			} catch (const ibrcommon::socket_exception &ex) {
				IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}

			// listen on P2P dial-up events
			dtn::core::EventDispatcher<dtn::net::P2PDialupEvent>::add(this);
		}
		void FileMonitor::componentUp() throw ()
		{
			// routine checked for throw() on 15.02.2013
			try {
				_socket.up();
			} catch (const ibrcommon::socket_exception &ex) {
				IBRCOMMON_LOGGER_TAG("FileMonitor", error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #16
0
		void TCPConnection::initialize() throw ()
		{
			// start the receiver for incoming bundles + handshake
			try {
				start();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #17
0
		void IPNDAgent::leave_interface(const ibrcommon::vinterface &iface) throw ()
		{
			ibrcommon::multicastsocket &msock = dynamic_cast<ibrcommon::multicastsocket&>(**_recv_socket.getAll().begin());

			for (std::set<ibrcommon::vaddress>::const_iterator it_addr = _destinations.begin(); it_addr != _destinations.end(); ++it_addr)
			{
				try {
					msock.leave(*it_addr, iface);
				} catch (const ibrcommon::socket_raw_error &e) {
					if (e.error() != EADDRNOTAVAIL) {
						IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "leave failed on " << iface.toString() << "; " << e.what() << IBRCOMMON_LOGGER_ENDL;
					}
				} catch (const ibrcommon::socket_exception &e) {
					IBRCOMMON_LOGGER_DEBUG_TAG(IPNDAgent::TAG, 10) << "can not leave " << (*it_addr).toString() << " on " << iface.toString() << IBRCOMMON_LOGGER_ENDL;
				} catch (const ibrcommon::Exception&) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, error) << "can not leave " << (*it_addr).toString() << " on " << iface.toString() << IBRCOMMON_LOGGER_ENDL;
				}
			}
		}
		void FloodRoutingExtension::componentDown() throw ()
		{
			try {
				// stop the thread
				stop();
				join();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG(FloodRoutingExtension::TAG, error) << "componentDown failed: " << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #19
0
		void Client::eventConnectionDown() throw ()
		{
			_inqueue.abort();

			try {
				_receiver.stop();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG("Client", error) << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #20
0
		void AbstractWorker::initialize(const std::string &uri, bool async)
		{
			_eid.setApplication(uri);

			try {
				if (async) _thread.start();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG("AbstractWorker", error) << "initialize failed: " << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
	int FATFile::getFiles(std::list<FATFile> &files) const
	{
		try {
			_reader.list(*this, files);
		} catch (const FatImageReader::FatImageException &e) {
			return e.getErrorCode();
		}
		IBRCOMMON_LOGGER_TAG(TAG,notice) << "getFile returning " << files.size() << " files" << IBRCOMMON_LOGGER_ENDL;
		return 0;
	}
		void SecurityKeyManager::createRSA(const dtn::data::EID &ref, const int bits)
		{
			const ibrcommon::File privkey = getKeyFile(ref, SecurityKey::KEY_PRIVATE);
			const ibrcommon::File pubkey = getKeyFile(ref, SecurityKey::KEY_PUBLIC);
			RSA* rsa = RSA_new();
			BIGNUM* e = BN_new();

			BN_set_word(e, 65537);

			RSA_generate_key_ex(rsa, bits, e, NULL);

			BN_free(e);
			e = NULL;

			// write private key
			int fd = ::open(privkey.getPath().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0600);

			FILE * rsa_privkey_file = fdopen(fd, "w");
			if (!rsa_privkey_file) {
				IBRCOMMON_LOGGER_TAG(SecurityKeyManager::TAG, error) << "Failed to open " << privkey.getPath() << IBRCOMMON_LOGGER_ENDL;
				RSA_free(rsa);
				return;
			}
			PEM_write_RSAPrivateKey(rsa_privkey_file, rsa, NULL, NULL, 0, NULL, NULL);
			fclose(rsa_privkey_file);

			// write public key
			FILE * rsa_pubkey_file = fopen(pubkey.getPath().c_str(), "w+");
			if (!rsa_pubkey_file) {
				IBRCOMMON_LOGGER_TAG(SecurityKeyManager::TAG, error) << "Failed to open " << privkey.getPath() << IBRCOMMON_LOGGER_ENDL;
				RSA_free(rsa);
				return;
			}
			PEM_write_RSA_PUBKEY(rsa_pubkey_file, rsa);
			fclose(rsa_pubkey_file);

			RSA_free(rsa);

			// set trust-level to high
			SecurityKey key = get(ref, SecurityKey::KEY_PUBLIC);
			key.trustlevel = SecurityKey::HIGH;
			store(key);
		}
		void FileMonitor::scan()
		{
			IBRCOMMON_LOGGER_DEBUG_TAG("FileMonitor", 5) << "scan for file changes" << IBRCOMMON_LOGGER_ENDL;

			std::set<ibrcommon::File> watch_set;

			for (watch_set::iterator iter = _watchset.begin(); iter != _watchset.end(); ++iter)
			{
				const ibrcommon::File &path = (*iter);
				std::list<ibrcommon::File> files;

				if (path.getFiles(files) == 0)
				{
					for (std::list<ibrcommon::File>::iterator iter = files.begin(); iter != files.end(); ++iter)
					{
						watch_set.insert(*iter);
					}
				}
				else
				{
					IBRCOMMON_LOGGER_TAG("FileMonitor", error) << "scan of " << path.getPath() << " failed" << IBRCOMMON_LOGGER_ENDL;
				}
			}

			// check for new directories
			for (std::set<ibrcommon::File>::iterator iter = watch_set.begin(); iter != watch_set.end(); ++iter)
			{
				const ibrcommon::File &path = (*iter);
				if (path.isDirectory() && !path.isSystem())
				{
					if (!isActive(path)) {
						adopt(path);
					}
				}
			}

			// look for gone directories
			for (std::map<ibrcommon::File, dtn::core::Node>::iterator iter = _active_paths.begin(); iter != _active_paths.end();)
			{
				const ibrcommon::File &path = (*iter).first;
				const dtn::core::Node &node = (*iter).second;

				if (watch_set.find(path) == watch_set.end())
				{
					dtn::core::BundleCore::getInstance().getConnectionManager().remove(node);
					IBRCOMMON_LOGGER_DEBUG_TAG("FileMonitor", 5) << "Node on drive gone: " << node.getEID().getString() << IBRCOMMON_LOGGER_ENDL;
					_active_paths.erase(iter++);
				}
				else
				{
					++iter;
				}
			}
		}
Beispiel #24
0
		void NativeSession::getInfo(RegisterIndex ri) throw ()
		{
			ibrcommon::RWLock l(_cb_mutex, ibrcommon::RWMutex::LOCK_READONLY);
			if (_serializer_cb == NULL) return;

			NativeSerializer serializer(*_serializer_cb, NativeSerializer::BUNDLE_INFO);
			try {
				serializer << _bundle[ri];
			} catch (const ibrcommon::Exception &ex) {
				IBRCOMMON_LOGGER_TAG(NativeSession::TAG, error) << "Get failed " << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
/**
 * Reads the own EID and publishes all the available Convergence Layer in the DHT
 */
void dtn::dht::DHTNameService::announce(const dtn::core::Node &n,
		enum dtn_dht_lookup_type type) {
	if (this->_announced) {
		std::string eid_ = n.getEID().getNode().getString();
		ibrcommon::MutexLock l(this->_libmutex);
		int rc = dtn_dht_announce(&_context, eid_.c_str(), eid_.size(), type);
		if (rc > 0) {
			IBRCOMMON_LOGGER_TAG("DHTNameService", info) << "DHT Announcing: " << eid_
						<< IBRCOMMON_LOGGER_ENDL;
		}
	}
}
		void EpidemicRoutingExtension::componentDown() throw ()
		{
			dtn::core::EventDispatcher<dtn::routing::NodeHandshakeEvent>::remove(this);

			try {
				// stop the thread
				stop();
				join();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG(EpidemicRoutingExtension::TAG, error) << "componentDown failed: " << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #27
0
	RSASHA256Stream::traits::int_type RSASHA256Stream::overflow(RSASHA256Stream::traits::int_type c)
	{
		char *ibegin = &out_buf_[0];
		char *iend = pptr();

		// mark the buffer as free
		setp(&out_buf_[0], &out_buf_[BUFF_SIZE - 1]);

		if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
		{
			//TODO writing one byte after the buffer?
			*iend++ = std::char_traits<char>::to_char_type(c);
		}

		// if there is nothing to send, just return
		if ((iend - ibegin) == 0)
		{
			return std::char_traits<char>::not_eof(c);
		}

		if (!_verify)
			// hashing
		{
			if (!EVP_SignUpdate(&_ctx, &out_buf_[0], iend - ibegin))
			{
				IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to feed data into the signature function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}
		else
		{
			if (!EVP_VerifyUpdate(&_ctx, &out_buf_[0], iend - ibegin))
			{
				IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to feed data into the verification function" << IBRCOMMON_LOGGER_ENDL;
				ERR_print_errors_fp(stderr);
			}
		}

		return std::char_traits<char>::not_eof(c);
	}
		void FloodRoutingExtension::componentUp() throw ()
		{
			// reset the task queue
			_taskqueue.reset();

			// routine checked for throw() on 15.02.2013
			try {
				// run the thread
				start();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG(FloodRoutingExtension::TAG, error) << "componentUp failed: " << ex.what() << IBRCOMMON_LOGGER_ENDL;
			}
		}
Beispiel #29
0
		void IPNDAgent::unlisten(const ibrcommon::vinterface &iface) throw ()
		{
			ibrcommon::socketset socks = _send_socket.get(iface);
			for (ibrcommon::socketset::iterator iter = socks.begin(); iter != socks.end(); ++iter) {
				ibrcommon::udpsocket *sock = dynamic_cast<ibrcommon::udpsocket*>(*iter);
				_send_socket.remove(sock);
				try {
					sock->down();
				} catch (const ibrcommon::socket_exception &ex) {
					IBRCOMMON_LOGGER_TAG(IPNDAgent::TAG, warning) << ex.what() << IBRCOMMON_LOGGER_ENDL;
				}
				delete sock;
			}
		}
Beispiel #30
0
		void TCPConnection::shutdown() throw ()
		{
			try {
				// shutdown
				(*getProtocolStream()).shutdown(dtn::streams::StreamConnection::CONNECTION_SHUTDOWN_ERROR);
			} catch (const ibrcommon::Exception&) {};

			try {
				// abort the connection thread
				ibrcommon::DetachedThread::stop();
			} catch (const ibrcommon::ThreadException &ex) {
				IBRCOMMON_LOGGER_TAG(TCPConnection::TAG, error) << "shutdown failed (" << ex.what() << ")" << IBRCOMMON_LOGGER_ENDL;
			}
		}