public function KeyExchangeMessage($messageVersion = null, $sequence = null, $flags = null, $baseKey = null, $baseKeySignature = null, $ratchetKey = null, $identityKey = null, $serialized = null) { /* :type messageVersion: int :type sequence: int :type flags:int :type baseKey: ECPublicKey :type baseKeySignature: bytearray :type ratchetKey: ECPublicKey :type identityKey: IdentityKey :type serialized: bytearray */ if ($serialized == null) { $this->supportedVersion = CiphertextMessage::CURRENT_VERSION; $this->version = $messageVersion; $this->sequence = $sequence; $this->flags = $flags; $this->baseKey = $baseKey; $this->baseKeySignature = $baseKeySignature; $this->ratchetKey = $ratchetKey; $this->identityKey = $identityKey; $version = ByteUtil::intsToByteHighAndLow($this->version, $this->supportedVersion); $keyExchangeMessage = new Textsecure_KeyExchangeMessage(); $keyExchangeMessage->setId($this->sequence << 5 | $this->flags); $keyExchangeMessage->setBaseKey($baseKey->serialize()); $keyExchangeMessage->setRatchetKey($ratchetKey->serialize()); $keyExchangeMessage->setIdentityKey($identityKey->serialize()); if ($messageVersion >= 3) { $keyExchangeMessage->setBaseKeySignature($baseKeySignature); } $this->serialized = ByteUtil::combine([chr((int) $version), $keyExchangeMessage->serializeToString()]); } else { try { $parts = ByteUtil::split($serialized, 1, strlen($serialized) - 1); $this->version = ByteUtil::highBitsToInt(ord($parts[0][0])); $this->supportedVersion = ByteUtil::lowBitsToInt(ord($parts[0][0])); if ($this->version <= CiphertextMessage::UNSUPPORTED_VERSION) { throw new LegacyMessageException('Unsupported legacy version: ' . $this->version); } if ($this->version > CiphertextMessage::CURRENT_VERSION) { throw new InvalidVersionException('Unkown version: ' . $this->version); } $message = new Textsecure_KeyExchangeMessage(); $message->parseFromString($parts[1]); if ($message->getId() == null || $message->getBaseKey() == null || $message->getRatchetKey() == null || $message->getIdentityKey() == null || $this->version >= 3 && $message->getBaseKeySignature() == null) { throw new InvalidMessageException('Some required fields are missing!'); } $this->sequence = $message->getId() >> 5; $this->flags = $message->getId() & 0x1f; $this->serialized = $serialized; $this->baseKey = Curve::decodePoint($message->getBaseKey(), 0); $this->baseKeySignature = $message->getBaseKeySignature(); $this->ratchetKey = Curve::decodePoint($message->getRatchetKey(), 0); $this->identityKey = new IdentityKey($message->getIdentityKey(), 0); } catch (InvalidKeyException $ex) { throw new InvalidMessageException($ex->getMessage()); } } }
public function SenderKeyMessage($keyId = null, $iteration = null, $ciphertext = null, $signatureKey = null, $serialized = null) { if ($serialized == null) { $version = ByteUtil::intsToByteHighAndLow(self::CURRENT_VERSION, self::CURRENT_VERSION); $proto_message = new Textsecure_SenderKeyMessage(); $proto_message->setId($keyId); $proto_message->setIteration($iteration); $proto_message->setCiphertext($ciphertext); $message = $proto_message->serializeToString(); $signature = $this->getSignature($signatureKey, ByteUtil::combine([chr((int) $version), $message])); $this->serialized = ByteUtil::combine([chr((int) $version), $message, $signature]); $this->messageVersion = self::CURRENT_VERSION; $this->keyId = $keyId; $this->iteration = $iteration; $this->ciphertext = $ciphertext; } else { try { $messageParts = ByteUtil::split($serialized, 1, strlen($serialized) - 1 - self::SIGNATURE_LENGTH, self::SIGNATURE_LENGTH); $version = ord($messageParts[0][0]); $message = $messageParts[1]; $signature = $messageParts[2]; if (ByteUtil::highBitsToInt($version) < 3) { throw new LegacyMessageException("Legacy message: " . ByteUtil::highBitsToInt($version)); } if (ByteUtil::highBitsToInt($version) > self::CURRENT_VERSION) { throw new InvalidMessageException("Unknown version: " . ByteUtil::highBitsToInt($version)); } $proto_message = new Textsecure_SenderKeyMessage(); try { $proto_message->parseFromString($message); } catch (Exception $ex) { throw new InvalidMessageException("Incomplete message"); } if ($proto_message->getId() === null || $proto_message->getIteration() === null || $proto_message->getCiphertext() == null) { throw new InvalidMessageException("Incomplete message"); } $this->serialized = $serialized; $this->messageVersion = ByteUtil::highBitsToInt($version); $this->keyId = $proto_message->getId(); $this->iteration = $proto_message->getIteration(); $this->ciphertext = $proto_message->getCiphertext(); } catch (Exception $ex) { throw new InvalidMessageException($ex->getMessage()); } } }
public function PreKeyWhisperMessage($messageVersion = null, $registrationId = null, $preKeyId = null, $signedPreKeyId = null, $ecPublicBaseKey = null, $identityKey = null, $whisperMessage = null, $serialized = null) { if ($serialized != null) { $this->version = ByteUtil::highBitsToInt($serialized[0]); if ($this->version > self::CURRENT_VERSION) { throw new InvalidVersionException("Unknown version " . $this->version); } $preKeyWhisperMessage = new Textsecure_PreKeyWhisperMessage(); $preKeyWhisperMessage->parseFromString(substr($serialized, 1)); if ($this->version == 2 && $preKeyWhisperMessage->getPreKeyId() == null || $this->version == 3 && $preKeyWhisperMessage->getSignedPreKeyId() == null || $preKeyWhisperMessage->getBaseKey() == null || $preKeyWhisperMessage->getIdentityKey() == null || $preKeyWhisperMessage->getMessage() == null) { throw new InvalidMessageException("Incomplete message"); } $this->serialized = $serialized; $this->registrationId = $preKeyWhisperMessage->getRegistrationId(); $this->preKeyId = $preKeyWhisperMessage->getPreKeyId(); $this->signedPreKeyId = $preKeyWhisperMessage->getSignedPreKeyId(); $this->baseKey = Curve::decodePoint((string) $preKeyWhisperMessage->getBaseKey(), 0); $this->identityKey = new IdentityKey(Curve::decodePoint((string) $preKeyWhisperMessage->getIdentityKey(), 0)); $this->message = new WhisperMessage(null, null, null, null, null, null, null, null, $preKeyWhisperMessage->getMessage()); } else { try { $this->version = $messageVersion; $this->registrationId = $registrationId; $this->preKeyId = $preKeyId; $this->signedPreKeyId = $signedPreKeyId; $this->baseKey = $ecPublicBaseKey; $this->identityKey = $identityKey; $this->message = $whisperMessage; $builder = new Textsecure_PreKeyWhisperMessage(); $builder->setSignedPreKeyId($this->signedPreKeyId); $builder->setBaseKey($this->baseKey->serialize()); $builder->setIdentityKey($this->identityKey->serialize()); $builder->setMessage($whisperMessage->serialize()); $builder->setRegistrationId($this->registrationId); if ($preKeyId != null) { $builder->setPreKeyId($preKeyId); } $versionBytes = ByteUtil::intsToByteHighAndLow($this->version, self::CURRENT_VERSION); $messageBytes = $builder->serializeToString(); $this->serialized = ByteUtil::combine([chr($versionBytes), $messageBytes]); } catch (Exception $ex) { throw new InvalidMessageException($ex->getMessage() . " - " . $ex->getLine() . " - " . $ex->getFile()); } } }
public function SenderKeyDistributionMessage($id = null, $iteration = null, $chainKey = null, $signatureKey = null, $serialized = null) { if ($serialized == null) { $version = ByteUtil::intsToByteHighAndLow(self::CURRENT_VERSION, self::CURRENT_VERSION); $this->id = $id; $this->iteration = $iteration; $this->chainKey = $chainKey; $this->signatureKey = $signatureKey; $proto_skdm = new Textsecure_SenderKeyDistributionMessage(); $proto_skdm->setId($id); $proto_skdm->setIteration($iteration); $proto_skdm->setChainKey((string) $chainKey); $proto_skdm->setSigningKey((string) $signatureKey->serialize()); $this->serialized = chr($version) . $proto_skdm->serializeToString(); } else { $parts = ByteUtil::split($serialized, 1, strlen($serialized) - 1); $version = ord($parts[0][0]); $message = $parts[1]; if (ByteUtil::highBitsToInt($version) < self::CURRENT_VERSION) { throw new LegacyMessageException("Legacy message: " + ByteUtil::highBitsToInt($version)); } if (ByteUtil::highBitsToInt($version) > self::CURRENT_VERSION) { throw new InvalidMessageException("Unknown version: " + ByteUtil::highBitsToInt($version)); } $proto_skdm = new Textsecure_SenderKeyDistributionMessage(); try { $proto_skdm->parseFromString($message); } catch (Exception $ex) { throw new InvalidMessageException("Incomplete message."); } if ($proto_skdm->getId() === null || $proto_skdm->getIteration() === null || $proto_skdm->getChainKey() === null || $proto_skdm->getSigningKey() === null) { throw new InvalidMessageException("Incomplete message."); } $this->serialized = $serialized; $this->id = $proto_skdm->getId(); $this->iteration = $proto_skdm->getIteration(); $this->chainKey = $proto_skdm->getChainKey(); $this->signatureKey = Curve::decodePoint($proto_skdm->getSigningKey(), 0); } }
public function isLegacy($message) { return $message != null && strlen($message) >= 1 && ByteUtil::highBitsToInt($message[0]) <= self::UNSUPPORTED_VERSION; }