void NLPacket::writeSourceID(const QUuid& sourceID) const { Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type)); auto offset = Packet::totalHeaderSize(isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion); memcpy(_packet.get() + offset, sourceID.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID); _sourceID = sourceID; }
int populatePacketHeader(char* packet, PacketType type, const QUuid& connectionUUID) { int numTypeBytes = packArithmeticallyCodedValue(type, packet); packet[numTypeBytes] = versionForPacketType(type); QUuid packUUID = connectionUUID.isNull() ? NodeList::getInstance()->getOwnerUUID() : connectionUUID; QByteArray rfcUUID = packUUID.toRfc4122(); memcpy(packet + numTypeBytes + sizeof(PacketVersion), rfcUUID.constData(), NUM_BYTES_RFC4122_UUID); // return the number of bytes written for pointer pushing return numTypeBytes + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID; }
bool OctreePacketData::appendValue(const QUuid& uuid) { QByteArray bytes = uuid.toRfc4122(); if (uuid.isNull()) { return appendValue((uint16_t)0); // zero length for null uuid } else { uint16_t length = bytes.size(); bool success = appendValue(length); if (success) { success = appendRawData((const unsigned char*)bytes.constData(), bytes.size()); } return success; } }
QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUuid& connectionSecret) { QCryptographicHash hash(QCryptographicHash::Md5); int offset = Packet::totalHeaderSize(packet.isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID + NUM_BYTES_MD5_HASH; // add the packet payload and the connection UUID hash.addData(packet.getData() + offset, packet.getDataSize() - offset); hash.addData(connectionSecret.toRfc4122()); // return the hash return hash.result(); }
QByteArray DataServerAccountInfo::getUsernameSignature(const QUuid& connectionToken) { auto lowercaseUsername = _username.toLower().toUtf8(); auto plaintext = lowercaseUsername.append(connectionToken.toRfc4122()); auto signature = signPlaintext(plaintext); if (!signature.isEmpty()) { qDebug(networking) << "Returning username" << _username << "signed with connection UUID" << uuidStringWithoutCurlyBraces(connectionToken); } else { qCDebug(networking) << "Error signing username with connection token"; qCDebug(networking) << "Will re-attempt on next domain-server check in."; } return signature; }
int populatePacketHeader(char* packet, PacketType type, const QUuid& connectionUUID) { int numTypeBytes = packArithmeticallyCodedValue(type, packet); packet[numTypeBytes] = versionForPacketType(type); char* position = packet + numTypeBytes + sizeof(PacketVersion); QUuid packUUID = connectionUUID.isNull() ? NodeList::getInstance()->getSessionUUID() : connectionUUID; QByteArray rfcUUID = packUUID.toRfc4122(); memcpy(position, rfcUUID.constData(), NUM_BYTES_RFC4122_UUID); position += NUM_BYTES_RFC4122_UUID; // pack 16 bytes of zeros where the md5 hash will be placed one data is packed memset(position, 0, NUM_BYTES_MD5_HASH); position += NUM_BYTES_MD5_HASH; // return the number of bytes written for pointer pushing return position - packet; }
static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version) { QByteArray hashResult; // create a scope so later resize won't reallocate { QCryptographicHash hash(algorithm); hash.addData(ns.toRfc4122()); hash.addData(baseData); hashResult = hash.result(); } hashResult.resize(16); // Sha1 will be too long QUuid result = QUuid::fromRfc4122(hashResult); result.data3 &= 0x0FFF; result.data3 |= (version << 12); result.data4[0] &= 0x3F; result.data4[0] |= 0x80; return result; }
// NOTE: This version will only encode the portion of the edit message immediately following the // header it does not include the send times and sequence number because that is handled by the // edit packet sender... bool EntityItemProperties::encodeEraseEntityMessage(const EntityItemID& entityItemID, unsigned char* outputBuffer, size_t maxLength, size_t& outputLength) { unsigned char* copyAt = outputBuffer; uint16_t numberOfIds = 1; // only one entity ID in this message if (maxLength < sizeof(numberOfIds) + NUM_BYTES_RFC4122_UUID) { qDebug() << "ERROR - encodeEraseEntityMessage() called with buffer that is too small!"; outputLength = 0; return false; } memcpy(copyAt, &numberOfIds, sizeof(numberOfIds)); copyAt += sizeof(numberOfIds); outputLength = sizeof(numberOfIds); QUuid entityID = entityItemID.id; QByteArray encodedEntityID = entityID.toRfc4122(); memcpy(copyAt, encodedEntityID.constData(), NUM_BYTES_RFC4122_UUID); copyAt += NUM_BYTES_RFC4122_UUID; outputLength += NUM_BYTES_RFC4122_UUID; return true; }
QByteArray hashForPacketAndConnectionUUID(const QByteArray& packet, const QUuid& connectionUUID) { return QCryptographicHash::hash(packet.mid(numBytesForPacketHeader(packet)) + connectionUUID.toRfc4122(), QCryptographicHash::Md5); }
QByteArray createGUID() { QUuid uuid = QUuid::createUuid(); return uuid.toRfc4122(); }
int tcp_ping(QStringList command) { qDebug() << "tcp_ping(" << command.join(" ") << ")" << endl; /** * Check input */ QTextStream errorStream(stderr); if(command.size() != 3 || command.at(0)!="ping" || command.at(1)!="tcp" ) { errorStream << "Error: tcp_ping(" << command.join(" ") << ") is no valid call (ping tcp <ip_address> <port> rzv|max|random|default)" << endl; return 1; } QByteArray byteArray; /** * CIP for "rzv" */ if(command.at(2)=="rzv") { qDebug() << "rzv" << endl; byteArray.append(QByteArray(42, '\0')); } /** * CIP for "default" */ if(command.at(2)=="default") { qDebug() << "default" << endl; // Header: request (1), profile (1), version (1), channel (1) byteArray.append(QByteArray(4, '\0')); // Header: UUID (16) QUuid uuid; uuid = QUuid::createUuid(); QByteArray uuid_arr = uuid.toRfc4122(); for(int j=0; j<16;j++) { byteArray.append(uuid_arr.at(j)); } // Header: Empty IP address (4), port number (2), time (8), type (1), size (1) byteArray.append(QByteArray(16, '\0')); // Contextinformation: type (1), root-CIC (2), size (1) byteArray.append(QByteArray(4, '\0')); // Application: type (1), size (1) byteArray.append(QByteArray(2, '\0')); } /** * CIP for "max" */ if(command.at(2)=="max") { qDebug() << "max" << endl; // Header: fix byteArray.append(QByteArray(34, '\0')); // Header: size (1) byteArray.append(QByteArray(QByteArray::fromHex("0xff"))); byteArray.append(QByteArray(255, '\0')); // Contextinformation: fix byteArray.append(QByteArray(2, '\0')); // Contextinformation: size (255) byteArray.append(QByteArray(QByteArray::fromHex("0xff"))); byteArray.append(QByteArray(255*2, '\0')); // Application Data: fix byteArray.append(QByteArray(0, '\0')); // Application Data: size (255) byteArray.append(QByteArray(QByteArray::fromHex("0xff"))); byteArray.append(QByteArray(255, '\0')); } /** * CIP for "random" */ if(command.at(2)=="random") { qDebug() << "random" << endl; QByteArray randValues; qsrand(QTime::currentTime().msec()); for(int i=0; i <= 1064; i++) { randValues.append(qFloor(qrand()/CRN_RANDOM_DIVISOR)); } int i = 0; quint8 rand; // Header: request (1) byteArray.append(randValues.at(i++)); // Header: profile (1) byteArray.append(randValues.at(i++)); // Header: version (1) byteArray.append(randValues.at(i++)); // Header: channel (1) byteArray.append(randValues.at(i++)); // Header: UUID (16) QUuid uuid; uuid = QUuid::createUuid(); QByteArray uuid_arr = uuid.toRfc4122(); for(int j=0; j<16;j++) { byteArray.append(uuid_arr.at(j)); } // Header: Empty IP address (4) and port number (2) byteArray.append(QByteArray(6, '\0')); // Header: time (8) byteArray.append(QByteArray(8, '\0')); // Header: type (1) byteArray.append(randValues.at(i++)); // Header: size (1) and data rand = randValues.at(i++); byteArray.append(rand); byteArray.append(QByteArray(rand, rand)); // Contextinformation: type (1) byteArray.append(randValues.at(i++)); // Contextinformation: root-CIC (2) byteArray.append(randValues.at(i++)); byteArray.append(randValues.at(i++)); // Contextinformation: size (1) rand = randValues.at(i++); byteArray.append(rand); byteArray.append(QByteArray(rand*2, rand)); // Application: type (1) byteArray.append(randValues.at(i++)); // Application: size (1) rand = randValues.at(i++); byteArray.append(rand); byteArray.append(QByteArray(rand, rand)); } // rand /** * Sent via TCP */ QTcpSocket *tcpSocket; tcpSocket = new QTcpSocket(); QTextStream outStream(stdout); QString out; tcpSocket->abort(); tcpSocket->connectToHost("127.0.0.1", 22365); qDebug() << "waitForConnected!"; if (tcpSocket->waitForConnected(5000)) { qDebug() << "Connected!"; } else { errorStream << "Error: tcp_ping(" << command.join(" ") << ") No connection available!" << endl; return 1; } out = QString("BytesWritten: %1").arg(tcpSocket->write(byteArray, byteArray.length())); qDebug() << out; int numRead = 0, numReadTotal = 0; char buffer[MAXMSG]; forever { numRead = tcpSocket->read(buffer, MAXMSG); qDebug() << "read buffer"; qDebug() << buffer; numReadTotal += numRead; if (numRead == 0 && !tcpSocket->waitForReadyRead()) break; } qDebug() << numReadTotal << " bytes red"; tcpSocket->flush(); tcpSocket->disconnectFromHost(); tcpSocket->close(); outStream << out << endl; return 0; }
void KdbxXmlWriter::writeUuid(const QString& qualifiedName, const QUuid& uuid) { writeString(qualifiedName, uuid.toRfc4122().toBase64()); }
QByteArray DataServerAccountInfo::getUsernameSignature(const QUuid& connectionToken) { if (!_privateKey.isEmpty()) { const char* privateKeyData = _privateKey.constData(); RSA* rsaPrivateKey = d2i_RSAPrivateKey(NULL, reinterpret_cast<const unsigned char**>(&privateKeyData), _privateKey.size()); if (rsaPrivateKey) { QByteArray lowercaseUsername = _username.toLower().toUtf8(); QByteArray usernameWithToken = QCryptographicHash::hash(lowercaseUsername.append(connectionToken.toRfc4122()), QCryptographicHash::Sha256); QByteArray usernameSignature(RSA_size(rsaPrivateKey), 0); unsigned int usernameSignatureSize = 0; int encryptReturn = RSA_sign(NID_sha256, reinterpret_cast<const unsigned char*>(usernameWithToken.constData()), usernameWithToken.size(), reinterpret_cast<unsigned char*>(usernameSignature.data()), &usernameSignatureSize, rsaPrivateKey); // free the private key RSA struct now that we are done with it RSA_free(rsaPrivateKey); if (encryptReturn == -1) { qCDebug(networking) << "Error encrypting username signature."; qCDebug(networking) << "Will re-attempt on next domain-server check in."; } else { qDebug(networking) << "Returning username" << _username << "signed with connection UUID" << uuidStringWithoutCurlyBraces(connectionToken); return usernameSignature; } } else { qCDebug(networking) << "Could not create RSA struct from QByteArray private key."; qCDebug(networking) << "Will re-attempt on next domain-server check in."; } } return QByteArray(); }
bool HMACAuth::setKey(const QUuid& uidKey) { const QByteArray rfcBytes(uidKey.toRfc4122()); return setKey(rfcBytes.constData(), rfcBytes.length()); }
int offer(QStringList command) { qDebug() << "offer(" << command.join(" ") << ")" << endl; /** * Debug */ for(int i=0;i<command.size();i++) { qDebug() << "command.at(" << i << ")" << command.at(i) << endl; } /** * Check input */ QTextStream errorStream(stderr); if(command.size() != 3) { errorStream << "Error: offer(" << command.join(" ") << "): No valid number of arguments!" << endl; man("usage offer"); return 1; } if(command.at(0)!="offer") { errorStream << "Error: offer(" << command.join(" ") << "): No valid command!" << endl; man("usage offer"); return 1; } if(! command.at(2).contains(QRegExp("^(random)$"))) { errorStream << "Error: offer(" << command.join(" ") << "): No valid mode!" << endl; man("usage offer"); return 1; } QByteArray byteArray; /** * Offer CIP for "random" */ if(command.at(2)=="random") { qDebug() << "random" << endl; /* * Create random CIP */ QByteArray randValues; qsrand(QTime::currentTime().msec()); for(int i=0; i <= 1064; i++) { randValues.append((quint8) qFloor(qrand()/CRN_RANDOM_DIVISOR)); } quint8 i = 0; quint8 rand; // Header: request (1) byteArray.append(randValues.at(i++)); // Header: profile (1) byteArray.append(randValues.at(i++)); // Header: version (1) byteArray.append(randValues.at(i++)); // Header: channel (1) byteArray.append(randValues.at(i++)); // Header: UUID (16) QUuid uuid; uuid = QUuid::createUuid(); QByteArray uuid_arr = uuid.toRfc4122(); for(int j=0; j<16;j++) { byteArray.append(uuid_arr.at(j)); } // Header: Empty IP address (4) and port number (2) byteArray.append(QByteArray(6, '\0')); // Header: time (8) byteArray.append(QByteArray(8, '\0')); // Header: type (1) byteArray.append(randValues.at(i++)); // Header: size (1) and data qDebug() << "xxxxxxxxxxxxxxx: " << i; rand = randValues.at(i++); qDebug() << "xxxxxxxxxxxxxxx: " << i; qDebug() << "xxxxxxxxxxxxxxx: " << rand; byteArray.append(rand); byteArray.append(QByteArray(rand, rand)); // Contextinformation: type (1) byteArray.append(randValues.at(i++)); // Contextinformation: root-CIC (2) byteArray.append(randValues.at(i++)); byteArray.append(randValues.at(i++)); // Contextinformation: size (1) rand = randValues.at(i++); byteArray.append(rand); byteArray.append(QByteArray(rand*2, rand)); // Application: type (1) byteArray.append(randValues.at(i++)); // Application: size (1) rand = randValues.at(i++); byteArray.append(rand); byteArray.append(QByteArray(rand, rand)); /** * Define map with start position */ QMap<QString, quint8> keys; quint8 pos; keys["request"] = 0; keys["profile"] = 1; keys["version"] = 2; keys["channel"] = 3; keys["uuid"] = 4; keys["ip"] = 20; keys["port"] = 24; keys["time"] = 26; keys["head_type"] = 34; keys["head_size"] = 35; i = byteArray.at(35) + 35; qDebug() << "XXXXXXXXXXXX: " << i; keys["ci_type"] = ++i; keys["content"] = ++i; keys["mask"] = ++i; keys["ci_size"] = ++i; keys["contents"] = ++i; QByteArray value; /* * Transform to Offer */ // Header request -> 2 pos = keys["request"]; value.clear(); value.append((quint8) 2); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); // Header channel -> 1 pos = keys["channel"]; value.clear(); value.append((quint8) 1); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); // Header version -> 1 pos = keys["version"]; value.clear(); value.append((quint8) 1); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); // CI type -> 1 pos = keys["ci_type"]; value.clear(); value.append((quint8) 1); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); // CI root-CIC->content -> 1 pos = keys["content"]; value.clear(); value.append((quint8) 1); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); // CI root-CIC->mask -> 0 pos = keys["mask"]; value.clear(); value.append('\0'); qDebug() << "byteArray.replace(" << pos << ", 1, " << value << ")" << endl; byteArray.replace(pos, 1, value); } // rand /** * Write file */ QString filePath; filePath = CIP_ROOT; filePath += "/" + command.at(1); QFile file(filePath); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { errorStream << "Error: touch(" << command.join(" ") << ") can not write to file " << filePath << endl; return 1; } file.write(byteArray); file.close(); qDebug() << "filePath: " << filePath << endl; return 0; }