Пример #1
0
QString ClientConfiguration::toBackup(QString const& password) const {
	QByteArray encryptionKey(BACKUP_ENCRYPTION_KEY_BYTES, 0x00);

	// Generate a Salt
	QByteArray salt(BACKUP_SALT_BYTES, 0x00);
	randombytes_buf(salt.data(), BACKUP_SALT_BYTES);

	// Convert the password into bytes
	QByteArray password8Bit = password.toUtf8();

	// Generate the encryption key for the Backup from the Salt and the Password
	PKCS5_PBKDF2_HMAC(reinterpret_cast<unsigned char*>(password8Bit.data()), password8Bit.size(), reinterpret_cast<unsigned char*>(salt.data()), BACKUP_SALT_BYTES, BACKUP_KEY_PBKDF_ITERATIONS, BACKUP_ENCRYPTION_KEY_BYTES, reinterpret_cast<unsigned char*>(encryptionKey.data()));

	QByteArray nonceBytes(crypto_stream_NONCEBYTES, 0x00);

	// The backup content
	QByteArray clientId(IdentityHelper::uint64ToIdentityString(getClientIdentity().getContactId()).toLatin1());
	if (clientId.size() != BACKUP_IDENTITY_BYTES) {
		throw InternalErrorException() << QString("Could not build backup - invalid client identity length (%1 vs. %2 Bytes).").arg(clientId.size()).arg(BACKUP_IDENTITY_BYTES).toStdString();
	}
	QByteArray clientSecKey(getClientLongTermKeyPair().getPrivateKey());
	if (clientSecKey.size() != PROTO_KEY_LENGTH_BYTES) {
		throw InternalErrorException() << QString("Could not build backup - invalid client secret key length (%1 vs. %2 Bytes).").arg(clientSecKey.size()).arg(PROTO_KEY_LENGTH_BYTES).toStdString();
	}

	QByteArray backup(salt);
	backup.append(clientId);
	backup.append(clientSecKey);

	// Compute Hash
	QByteArray controlHash(crypto_hash_sha256_BYTES, 0x00);
	crypto_hash_sha256(reinterpret_cast<unsigned char*>(controlHash.data()), reinterpret_cast<unsigned char*>(backup.data() + BACKUP_SALT_BYTES), BACKUP_IDENTITY_BYTES + PROTO_KEY_LENGTH_BYTES);
	backup.append(controlHash.left(BACKUP_HASH_BYTES));

	if (backup.size() != (BACKUP_SALT_BYTES + BACKUP_IDENTITY_BYTES + PROTO_KEY_LENGTH_BYTES + BACKUP_HASH_BYTES)) {
		throw InternalErrorException() << QString("Could not build backup - invalid packet length (%1 vs. %2 Bytes).").arg(clientSecKey.size()).arg(BACKUP_SALT_BYTES + BACKUP_IDENTITY_BYTES + PROTO_KEY_LENGTH_BYTES + BACKUP_HASH_BYTES).toStdString();
	}

	// The Backup is build from SALT + IDENTITY + KEY + HASH
	crypto_stream_xor(reinterpret_cast<unsigned char*>(backup.data() + BACKUP_SALT_BYTES), reinterpret_cast<unsigned char*>(backup.data() + BACKUP_SALT_BYTES), BACKUP_IDENTITY_BYTES + PROTO_KEY_LENGTH_BYTES + BACKUP_HASH_BYTES, reinterpret_cast<unsigned char*>(nonceBytes.data()), reinterpret_cast<unsigned char*>(encryptionKey.data()));

	// Encode in Base32
	return Base32::encodeBase32Sequence(backup);
}
Пример #2
0
int SplitNameVisitor::_getNextNameId(const Tags& t, int lastId)
{
    for (int i = lastId + 1; i < lastId + 100; i++)
    {
        QString k = QString("name:%1").arg(i);

        if (t.contains(k) == false)
        {
            return i;
        }
    }

    throw InternalErrorException("Unable to find a valid key for a new extra name.");
}
Пример #3
0
void WayJoin2Mapper::map(HadoopPipes::MapContext& context)
{
  _context = &context;
  if (_reader == NULL)
  {
    HadoopPipes::RecordReader* rr = pp::HadoopPipesUtils::getRecordReader(&context);
    _reader = dynamic_cast<WayJoin2RecordReader*>(rr);
    if (_reader == NULL)
    {
      throw InternalErrorException("Expected a record reader of type WayJoin2RecordReader");
    }
  }

  if (_reader->getRecordType() == WayJoin2InputSplit::PbfInputSplitType)
  {
    mapOsmMap(_reader->getMap());
  }
  else
  {
    const string& key = context.getInputKey();
    const string& value = context.getInputValue();

    if (key.size() != sizeof(int64_t))
    {
      throw InternalErrorException("Expected key to be a int64_t");
    }
    if (value.size() != sizeof(WayJoin1Reducer::Value))
    {
      throw InternalErrorException("Expected value to be a WayJoin1Reducer::Value");
    }

    int64_t* k = (int64_t*)key.data();
    WayJoin1Reducer::Value* v = (WayJoin1Reducer::Value*)value.data();
    mapWayPoints(*k, *v);
  }
}
Пример #4
0
void AmqpException::Throw(const amqp_connection_close_t& reply)
{
  std::ostringstream what;
  const char* method_name = amqp_method_name(((reply.class_id << 16) | reply.method_id));

  std::string reply_text;
  if (reply.reply_text.bytes != NULL)
  {
    reply_text = std::string((char*)reply.reply_text.bytes, reply.reply_text.len);
  }

  if (method_name != NULL)
  {
    what << "connection error: " << reply.reply_code << ": " << method_name << " caused: " << reply_text;
  }
  else
  {
    what << "connection error: " << reply.reply_code << ": " << reply_text;
  }

  switch (reply.reply_code)
  {
  case ConnectionForcedException::REPLY_CODE:
    throw ConnectionForcedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case InvalidPathException::REPLY_CODE:
    throw InvalidPathException(what.str(), reply_text, reply.class_id, reply.method_id);
  case FrameErrorException::REPLY_CODE:
    throw FrameErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case SyntaxErrorException::REPLY_CODE:
    throw SyntaxErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case CommandInvalidException::REPLY_CODE:
    throw CommandInvalidException(what.str(), reply_text, reply.class_id, reply.method_id);
  case ChannelErrorException::REPLY_CODE:
    throw ChannelErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case UnexpectedFrameException::REPLY_CODE:
    throw UnexpectedFrameException(what.str(), reply_text, reply.class_id, reply.method_id);
  case ResourceErrorException::REPLY_CODE:
    throw ResourceErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case NotAllowedException::REPLY_CODE:
    throw NotAllowedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case NotImplementedException::REPLY_CODE:
    throw NotImplementedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case InternalErrorException::REPLY_CODE:
    throw InternalErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  default:
    throw std::logic_error(std::string("Programming error: unknown connection reply code: ").append(boost::lexical_cast<std::string>(reply.reply_code)));
  }
}
Пример #5
0
QByteArray FullMessageHeader::toPacket() const {
	QByteArray result;

	result.append(sender.getContactIdAsByteArray());
	result.append(receiver.getContactIdAsByteArray());
	result.append(messageId.getMessageIdAsByteArray());
	result.append(time.getMessageTimeAsByteArray());
	result.append(flags.getFlags());
	result.append(QByteArray(PROTO_MESSAGE_RESERVED_AFTER_FLAGS_LENGTH_BYTES, 0x00));
	result.append(pushFromName.getPushFromIdAsByteArray());

	if (result.size() != (PROTO_MESSAGE_HEADER_FULL_LENGTH_BYTES)) {
		throw InternalErrorException() << "Size of Header data segment is " << result.size() << " instead of " << (PROTO_MESSAGE_HEADER_FULL_LENGTH_BYTES) << " Bytes.";
	}

	return result;
}
Пример #6
0
size_t MapReprojector::_findBestResult(vector<PlanarTestResult>& results)
{
  vector<PlanarTestResult> orderByAngle = results;
  vector<PlanarTestResult> orderByDistance = results;
  sort(orderByDistance.begin(), orderByDistance.end(), _distanceLessThan);
  sort(orderByAngle.begin(), orderByAngle.end(), _angleLessThan);

  set<size_t> foundInDistance;
  for (size_t i = 0; i < orderByDistance.size(); ++i)
  {
    foundInDistance.insert(orderByDistance[i].i);
    if (foundInDistance.find(orderByAngle[i].i) != foundInDistance.end())
    {
      return orderByAngle[i].i;
    }
  }

  throw InternalErrorException("Unable to find a best result. Bug?");
}
Пример #7
0
Client::Client(QWidget *parent) : QMainWindow(parent), protocolClient(nullptr), settings(nullptr), audioOutput(nullptr), connectionState(ConnectionState::STATE_DISCONNECTED), serverConfiguration(nullptr), clientConfiguration(nullptr), contactRegistry(ContactRegistry::getInstance()) {
	ui.setupUi(this);
	ui.listContacts->setContextMenuPolicy(Qt::CustomContextMenu);
	connectionTimer.start(500);
	OPENMITTSU_CONNECT(&connectionTimer, timeout(), this, connectionTimerOnTimer());

	QCoreApplication::setOrganizationName("BliZZarD");
	QCoreApplication::setOrganizationDomain("philippberger.de");
	QCoreApplication::setApplicationName("OpenMittsu");

	this->settings = new QSettings(this);
	// Initialize in the right thread
	MessageCenter* mc = MessageCenter::getInstance();
	mc->setTabContainer(ui.tabWidget);

	OP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QWx1djgxZGNiejBTQlg4U2dWMQo0SEVLYzZ4TUFObllzL2FZS2p2bVAwVnBPdlJVCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=");
	PublicKey const longTermServerPublicKey = PublicKey::fromHexString(QStringLiteral("b851ae1bf275ebe6851ca7f5206b495080927159787e9aaabbeb4e55af09d805"));

	serverConfiguration = new ServerConfiguration(QStringLiteral("g-xx.0.threema.ch"), 5222, longTermServerPublicKey, QStringLiteral("https://api.threema.ch/identity/%1"), QStringLiteral("Threema/2.2A"), apiServerRootCertificate, QStringLiteral("https://%1.blob.threema.ch/%2"), QStringLiteral("https://%1.blob.threema.ch/%2/done"), QStringLiteral("https://upload.blob.threema.ch/upload"), QStringLiteral("Threema/2.2A"), apiServerRootCertificate);

	// Load stored settings
	if (settings->contains("clientConfigurationFile")) {
		if (validateClientConfigurationFile(settings->value("clientConfigurationFile").toString(), true)) {
			this->clientConfiguration = new ClientConfiguration(ClientConfiguration::fromFile(settings->value("clientConfigurationFile").toString()));
			updateClientSettingsInfo(settings->value("clientConfigurationFile").toString());
		} else {
			LOGGER_DEBUG("Removing key \"clientConfigurationFile\" from stored settings as the file is not valid.");
			settings->remove("clientConfigurationFile");
		}
	}
	// Load Contacts
	if (settings->contains("contactsFile")) {
		if (validateKnownIdentitiesFile(settings->value("contactsFile").toString(), true)) {
			contactRegistry->fromFile(settings->value("contactsFile").toString());
			updateKnownContactsInfo(settings->value("contactsFile").toString());
		} else {
			LOGGER_DEBUG("Removing key \"contactsFile\" from stored settings as the file is not valid.");
			settings->remove("contactsFile");
		}
	}

	OPENMITTSU_CONNECT(contactRegistry, identitiesChanged(), this, contactRegistryOnIdentitiesChanged());
	OPENMITTSU_CONNECT(ui.btnConnect, clicked(), this, btnConnectOnClick());
	OPENMITTSU_CONNECT(ui.btnOpenClientIni, clicked(), this, btnOpenClientIniOnClick());
	OPENMITTSU_CONNECT(ui.btnOpenContacts, clicked(), this, btnOpenContactsOnClick());
	OPENMITTSU_CONNECT(ui.listContacts, itemDoubleClicked(QListWidgetItem*), this, listContactsOnDoubleClick(QListWidgetItem*));
	OPENMITTSU_CONNECT(ui.listContacts, customContextMenuRequested(const QPoint&), this, listContactsOnContextMenu(const QPoint&));
	OPENMITTSU_CONNECT(ui.tabWidget, currentChanged(int), this, chatTabWidgetOnCurrentTabChanged(int));

	// Menus
	OPENMITTSU_CONNECT(ui.actionLicense, triggered(), this, menuAboutLicenseOnClick());
	OPENMITTSU_CONNECT(ui.actionAbout, triggered(), this, menuAboutAboutOnClick());
	OPENMITTSU_CONNECT(ui.actionAbout_Qt, triggered(), this, menuAboutAboutQtOnClick());
	OPENMITTSU_CONNECT(ui.actionAdd_a_Contact, triggered(), this, menuContactAddOnClick());
	OPENMITTSU_CONNECT(ui.actionDelete_a_Contact, triggered(), this, menuContactDeleteOnClick());
	OPENMITTSU_CONNECT(ui.actionEdit_a_Contact, triggered(), this, menuContactEditOnClick());
	OPENMITTSU_CONNECT(ui.actionSave_to_file, triggered(), this, menuContactSaveToFileOnClick());
	OPENMITTSU_CONNECT(ui.actionAdd_Group, triggered(), this, menuGroupAddOnClick());
	OPENMITTSU_CONNECT(ui.actionEdit_Group, triggered(), this, menuGroupEditOnClick());
	OPENMITTSU_CONNECT(ui.actionLeave_Group, triggered(), this, menuGroupLeaveOnClick());
	OPENMITTSU_CONNECT(ui.actionCreate_Backup, triggered(), this, menuIdentityCreateBackupOnClick());
	OPENMITTSU_CONNECT(ui.actionLoad_Backup, triggered(), this, menuIdentityLoadBackupOnClick());
	OPENMITTSU_CONNECT(ui.actionShow_Fingerprint, triggered(), this, menuIdentityShowFingerprintOnClick());
	OPENMITTSU_CONNECT(ui.actionShow_Public_Key, triggered(), this, menuIdentityShowPublicKeyOnClick());
	OPENMITTSU_CONNECT(ui.actionStatistics, triggered(), this, menuAboutStatisticsOnClick());
	OPENMITTSU_CONNECT(ui.actionExit, triggered(), this, menuFileExitOnClick());

	protocolClientThread.start();
	protocolClient = nullptr;
	OPENMITTSU_CONNECT(&protocolClientThread, finished(), this, threadFinished());

	// Load audio resources
	QAudioFormat format;
	format.setSampleRate(44100);
	format.setChannelCount(2);
	format.setSampleSize(16);
	format.setCodec("audio/pcm");
	format.setByteOrder(QAudioFormat::LittleEndian);
	format.setSampleType(QAudioFormat::UnSignedInt);
	audioOutput = new QAudioOutput(format, this);

	OPENMITTSU_CONNECT(audioOutput, stateChanged(QAudio::State), this, audioOutputOnStateChanged(QAudio::State));

	receivedMessageAudioFile.setFileName(":/audio/ReceivedMessage.wav");
	if (!receivedMessageAudioFile.open(QFile::ReadOnly)) {
		throw InternalErrorException() << "Could not load audio file ReceivedMessage.wav in the Client.";
	}

	// Call Updater
	OPENMITTSU_CONNECT_QUEUED(&updater, foundNewVersion(int, int, int, int, QString, QString, QString), this, updaterFoundNewVersion(int, int, int, int, QString, QString, QString));
	QTimer::singleShot(0, &updater, SLOT(start()));


	// Call the setup() function in the thread
	QTimer::singleShot(0, protocolClient, SLOT(setup()));
	contactRegistryOnIdentitiesChanged();

	// Restore Window location and size
	restoreGeometry(settings->value("clientMainWindowGeometry").toByteArray());
	restoreState(settings->value("clientMainWindowState").toByteArray());
}
void SerializerStream::full_serialization(const Serializable &obj, bool include_typehashes){
	auto node = obj.get_object_node();
#ifdef LOG
	std::clog << "Traversing reference graph...\n";
#endif
	std::set<std::uint32_t> used_types;
	objectid_t root_object = 0;
	{
		std::vector<decltype(node)> stack, temp_stack;
		this->id_map[0] = 0;

		auto id = this->save_object(node.get_address());
		assert(id);
		root_object = id;
		this->node_map[id] = node;
		used_types.insert(node.get_typeid());
		stack.push_back(node);

		while (stack.size()){
			auto top = stack.back();
			stack.pop_back();
			if (!top.get_address())
				continue;
			top.get_children(temp_stack);
			for (auto &i : temp_stack){
				id = this->save_object(i.get_address());
				if (!id)
					continue;
				this->node_map[id] = i;
				used_types.insert(i.get_typeid());
				stack.push_back(i);
			}
			temp_stack.clear();
		}
	}
	if (include_typehashes){
#ifdef LOG
		std::clog <<
			"Travesal found " << this->node_map.size() << " objects.\n"
			"Serializing type hashes...\n";
#endif
		auto list = obj.get_metadata()->get_known_types();
		std::map<decltype(list[0].first), decltype(list[0].second) *> typemap;
		for (auto &i : list)
			typemap[i.first] = &i.second;
		this->serialize((std::uint32_t)used_types.size());
		for (auto &t : used_types){
			this->serialize(t);
			this->serialize_array(typemap[t]->digest);
		}
	}
#ifdef LOG
	std::clog << "Remapping object IDs...\n";
#endif

	{
		std::map<std::uint32_t, std::vector<objectid_t>> remap;
		for (auto &n : this->node_map)
			remap[n.second.get_typeid()].push_back(n.first);
		
		this->next_object_id = 1;
		decltype(this->node_map) temp;
		bool root_set = false;
		this->id_map.clear();
		this->id_map[0] = 0;
		for (auto &r : remap){
			for (auto i : r.second){
				auto &node = this->node_map[i];
				if (!node.get_address())
					continue;
				auto oid = this->save_object(node.get_address());
				if (!root_set && i == root_object){
					root_object = oid;
					root_set = true;
				}
				temp[oid] = node;
			}
		}
		this->node_map = temp;
	}

#ifdef LOG
	std::clog << "Serializing node map...\n";
#endif

	{
		std::vector<std::pair<std::uint32_t, objectid_t>> type_map;
		for (auto &n : this->node_map){
			auto type = n.second.get_typeid();
			if (!type)
				throw InternalErrorException();
			if (!type_map.size() || type != type_map.back().first){
				type_map.push_back(std::make_pair(type, n.first));
				continue;
			}
			type_map.back().second = std::max(type_map.back().second, n.first);
		}
		this->serialize((wire_size_t)type_map.size());
		for (auto &i : type_map){
			this->serialize(i.first);
			this->serialize(i.second);
		}
		this->serialize(root_object);
	}

#ifdef LOG
	std::clog << "Serializing nodes...\n";
#endif
	for (auto &n : this->node_map)
		n.second.serialize(*this);
#ifdef LOG
	std::clog << "Serialization done!\n";
#endif
}