def fromProtocolTreeNode(node): entity = ResultIqProtocolEntity.fromProtocolTreeNode(node) entity.__class__ = ResultGetKeysIqProtocolEntity entity.setPreKeyBundleMap() userNodes = node.getChild("list").getAllChildren() for userNode in userNodes: preKeyNode = userNode.getChild("key") signedPreKeyNode = userNode.getChild("skey") registrationId = ResultGetKeysIqProtocolEntity._bytesToInt(userNode.getChild("registration").getData()) identityKey = IdentityKey(DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(userNode.getChild("identity").getData()))) #preKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(preKeyNode.getChild("id").getData()) #preKeyPublic = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(preKeyNode.getChild("value").getData())) try: preKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(preKeyNode.getChild("id").getData()) except: preKeyId = None; pass try: preKeyPublic = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(preKeyNode.getChild("value").getData())) except: preKeyPublic = None; pass signedPreKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(signedPreKeyNode.getChild("id").getData()) signedPreKeySig = ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("signature").getData()) signedPreKeyPub = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("value").getData())) preKeyBundle = PreKeyBundle(registrationId, 1, preKeyId, preKeyPublic, signedPreKeyId, signedPreKeyPub, signedPreKeySig, identityKey) entity.setPreKeyBundleFor(userNode["jid"], preKeyBundle) return entity
def fromProtocolTreeNode(node): entity = ResultIqProtocolEntity.fromProtocolTreeNode(node) entity.__class__ = ResultGetKeysIqProtocolEntity entity.setPreKeyBundleMap() userNodes = node.getChild("list").getAllChildren() for userNode in userNodes: preKeyNode = userNode.getChild("key") if preKeyNode is None: logger = logging.getLogger(__name__) logger.warning("Failed to receive preKeyNode for %s"%(userNode.getAttributeValue("jid") if "jid" in userNode.attributes else "unknown")) preKeyId = None preKeyPublic = None else: preKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(preKeyNode.getChild("id").getData()) preKeyPublic = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(preKeyNode.getChild("value").getData())) signedPreKeyNode = userNode.getChild("skey") registrationId = ResultGetKeysIqProtocolEntity._bytesToInt(userNode.getChild("registration").getData()) identityKey = IdentityKey(DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(userNode.getChild("identity").getData()))) signedPreKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(signedPreKeyNode.getChild("id").getData()) signedPreKeySig = ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("signature").getData()) signedPreKeyPub = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("value").getData())) preKeyBundle = PreKeyBundle(registrationId, 1, preKeyId, preKeyPublic, signedPreKeyId, signedPreKeyPub, signedPreKeySig, identityKey) entity.setPreKeyBundleFor(userNode["jid"], preKeyBundle) return entity
def fromProtocolTreeNode(node): entity = ResultIqProtocolEntity.fromProtocolTreeNode(node) entity.__class__ = ResultGetKeysIqProtocolEntity entity.setPreKeyBundleMap() userNodes = node.getChild("list").getAllChildren() for userNode in userNodes: preKeyNode = userNode.getChild("key") if (preKeyNode is None): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # create file handler which logs even debug messages fh = logging.FileHandler('/var/log/yowsup_failed_number.log') fh.setLevel(logging.DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # add the handlers to the logger logger.addHandler(fh) logger.warning("Failed to receive preKeyNode for %s" % (userNode.getAttributeValue("jid") if "jid" in userNode.attributes else "unknown")) # it seems, simply using None for IDs breaks sending, but at last allows receiving preKeyId = None preKeyPublic = None else: preKeyId = ResultGetKeysIqProtocolEntity._bytesToInt( preKeyNode.getChild("id").getData()) preKeyPublic = DjbECPublicKey( ResultGetKeysIqProtocolEntity.encStr( preKeyNode.getChild("value").getData())) signedPreKeyNode = userNode.getChild("skey") registrationId = ResultGetKeysIqProtocolEntity._bytesToInt( userNode.getChild("registration").getData()) identityKey = IdentityKey( DjbECPublicKey( ResultGetKeysIqProtocolEntity.encStr( userNode.getChild("identity").getData()))) signedPreKeyId = ResultGetKeysIqProtocolEntity._bytesToInt( signedPreKeyNode.getChild("id").getData()) signedPreKeySig = ResultGetKeysIqProtocolEntity.encStr( signedPreKeyNode.getChild("signature").getData()) signedPreKeyPub = DjbECPublicKey( ResultGetKeysIqProtocolEntity.encStr( signedPreKeyNode.getChild("value").getData())) preKeyBundle = PreKeyBundle(registrationId, 1, preKeyId, preKeyPublic, signedPreKeyId, signedPreKeyPub, signedPreKeySig, identityKey) entity.setPreKeyBundleFor(userNode["jid"], preKeyBundle) return entity
def build_session(self, recipient_id, device_id, bundle_dict): sessionBuilder = SessionBuilder(self.store, self.store, self.store, self.store, recipient_id, device_id) registration_id = self.store.getLocalRegistrationId() preKeyPublic = DjbECPublicKey(bundle_dict['preKeyPublic'][1:]) signedPreKeyPublic = DjbECPublicKey( bundle_dict['signedPreKeyPublic'][1:]) identityKey = IdentityKey( DjbECPublicKey(bundle_dict['identityKey'][1:])) prekey_bundle = PreKeyBundle(registration_id, device_id, bundle_dict['preKeyId'], preKeyPublic, bundle_dict['signedPreKeyId'], signedPreKeyPublic, bundle_dict['signedPreKeySignature'], identityKey) sessionBuilder.processPreKeyBundle(prekey_bundle) return self.get_session_cipher(recipient_id, device_id)
def fromProtocolTreeNode(node): entity = ResultGetKeysIqProtocolEntity(node["id"]) userNodes = node.getChild("list").getAllChildren() for userNode in userNodes: missing_params = [] preKeyNode = userNode.getChild("key") signedPreKeyNode = userNode.getChild("skey") registrationNode = userNode.getChild("registration") identityNode = userNode.getChild("identity") if preKeyNode is None: missing_params.append(MissingParametersException.PARAM_KEY) if signedPreKeyNode is None: missing_params.append(MissingParametersException.PARAM_SKEY) if registrationNode is None: missing_params.append(MissingParametersException.PARAM_REGISTRATION) if identityNode is None: missing_params.append(MissingParametersException.PARAM_IDENTITY) if len(missing_params): entity.setErrorFor(userNode["jid"], MissingParametersException(userNode["jid"], missing_params)) else: registrationId = ResultGetKeysIqProtocolEntity._bytesToInt(registrationNode.getData()) identityKey = IdentityKey(DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(identityNode.getData()))) preKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(preKeyNode.getChild("id").getData()) preKeyPublic = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(preKeyNode.getChild("value").getData())) signedPreKeyId = ResultGetKeysIqProtocolEntity._bytesToInt(signedPreKeyNode.getChild("id").getData()) signedPreKeySig = ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("signature").getData()) signedPreKeyPub = DjbECPublicKey(ResultGetKeysIqProtocolEntity.encStr(signedPreKeyNode.getChild("value").getData())) preKeyBundle = PreKeyBundle(registrationId, 1, preKeyId, preKeyPublic, signedPreKeyId, signedPreKeyPub, signedPreKeySig, identityKey) entity.setPreKeyBundleFor(userNode["jid"], preKeyBundle) return entity
def build_session(self, jid, device_id, bundle): session = SessionBuilder(self._storage, self._storage, self._storage, self._storage, jid, device_id) registration_id = self._storage.getLocalRegistrationId() prekey = bundle.pick_prekey() otpk = DjbECPublicKey(prekey['key'][1:]) spk = DjbECPublicKey(bundle.spk['key'][1:]) ik = IdentityKey(DjbECPublicKey(bundle.ik[1:])) prekey_bundle = PreKeyBundle(registration_id, device_id, prekey['id'], otpk, bundle.spk['id'], spk, bundle.spk_signature, ik) session.processPreKeyBundle(prekey_bundle) self._get_session_cipher(jid, device_id)
def test_basicPreKeyV3(self): aliceStore = InMemoryAxolotlStore() aliceSessionBuilder = SessionBuilder(aliceStore, aliceStore, aliceStore, aliceStore, self.__class__.BOB_RECIPIENT_ID, 1) bobStore = InMemoryAxolotlStore() bobPreKeyPair = Curve.generateKeyPair() bobSignedPreKeyPair = Curve.generateKeyPair() bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(), bobSignedPreKeyPair.getPublicKey().serialize()) bobPreKey = PreKeyBundle(bobStore.getLocalRegistrationId(), 1, 31337, bobPreKeyPair.getPublicKey(), 22, bobSignedPreKeyPair.getPublicKey(), bobSignedPreKeySignature, bobStore.getIdentityKeyPair().getPublicKey()) aliceSessionBuilder.processPreKeyBundle(bobPreKey) self.assertTrue(aliceStore.containsSession(self.__class__.BOB_RECIPIENT_ID, 1)) self.assertTrue(aliceStore.loadSession(self.__class__.BOB_RECIPIENT_ID, 1).getSessionState().getSessionVersion() == 3) originalMessage = "L'homme est condamné à être libre" aliceSessionCipher = SessionCipher(aliceStore, aliceStore, aliceStore, aliceStore, self.__class__.BOB_RECIPIENT_ID, 1) outgoingMessage = aliceSessionCipher.encrypt(originalMessage) self.assertTrue(outgoingMessage.getType() == CiphertextMessage.PREKEY_TYPE) incomingMessage = PreKeyWhisperMessage(serialized=outgoingMessage.serialize()) bobStore.storePreKey(31337, PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair)) bobStore.storeSignedPreKey(22, SignedPreKeyRecord(22, int(time.time() * 1000), bobSignedPreKeyPair, bobSignedPreKeySignature)) bobSessionCipher = SessionCipher(bobStore, bobStore, bobStore, bobStore, self.__class__.ALICE_RECIPIENT_ID, 1) plaintext = bobSessionCipher.decryptPkmsg(incomingMessage) self.assertEqual(originalMessage, plaintext) # @@TODO: in callback assertion # self.assertFalse(bobStore.containsSession(self.__class__.ALICE_RECIPIENT_ID, 1)) self.assertTrue(bobStore.containsSession(self.__class__.ALICE_RECIPIENT_ID, 1)) self.assertTrue(bobStore.loadSession(self.__class__.ALICE_RECIPIENT_ID, 1).getSessionState().getSessionVersion() == 3) self.assertTrue(bobStore.loadSession(self.__class__.ALICE_RECIPIENT_ID, 1).getSessionState().getAliceBaseKey() != None) self.assertEqual(originalMessage, plaintext) bobOutgoingMessage = bobSessionCipher.encrypt(originalMessage) self.assertTrue(bobOutgoingMessage.getType() == CiphertextMessage.WHISPER_TYPE) alicePlaintext = aliceSessionCipher.decryptMsg(WhisperMessage(serialized=bobOutgoingMessage.serialize())) self.assertEqual(alicePlaintext, originalMessage) self.runInteraction(aliceStore, bobStore) aliceStore = InMemoryAxolotlStore() aliceSessionBuilder = SessionBuilder(aliceStore, aliceStore, aliceStore, aliceStore, self.__class__.BOB_RECIPIENT_ID, 1) aliceSessionCipher = SessionCipher(aliceStore, aliceStore, aliceStore, aliceStore, self.__class__.BOB_RECIPIENT_ID, 1) bobPreKeyPair = Curve.generateKeyPair() bobSignedPreKeyPair = Curve.generateKeyPair() bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(), bobSignedPreKeyPair.getPublicKey().serialize()) bobPreKey = PreKeyBundle(bobStore.getLocalRegistrationId(), 1, 31338, bobPreKeyPair.getPublicKey(), 23, bobSignedPreKeyPair.getPublicKey(), bobSignedPreKeySignature, bobStore.getIdentityKeyPair().getPublicKey()) bobStore.storePreKey(31338, PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair)) bobStore.storeSignedPreKey(23, SignedPreKeyRecord(23, int(time.time() * 1000), bobSignedPreKeyPair, bobSignedPreKeySignature)) aliceSessionBuilder.processPreKeyBundle(bobPreKey) outgoingMessage = aliceSessionCipher.encrypt(originalMessage) try: plaintext = bobSessionCipher.decryptPkmsg(PreKeyWhisperMessage(serialized=outgoingMessage)) raise AssertionError("shouldn't be trusted!") except Exception: bobStore.saveIdentity(self.__class__.ALICE_RECIPIENT_ID, PreKeyWhisperMessage(serialized=outgoingMessage.serialize()).getIdentityKey()) plaintext = bobSessionCipher.decryptPkmsg(PreKeyWhisperMessage(serialized=outgoingMessage.serialize())) self.assertEqual(plaintext, originalMessage) bobPreKey = PreKeyBundle(bobStore.getLocalRegistrationId(), 1, 31337, Curve.generateKeyPair().getPublicKey(), 23, bobSignedPreKeyPair.getPublicKey(), bobSignedPreKeySignature, aliceStore.getIdentityKeyPair().getPublicKey()) try: aliceSessionBuilder.process(bobPreKey) raise AssertionError("shouldn't be trusted!") except Exception: #good pass