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") 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 getIdentityKeyPair(self): q = "SELECT public_key, private_key FROM identities WHERE recipient_id = -1" c = self.dbConn.cursor() c.execute(q) result = c.fetchone() publicKey, privateKey = result return IdentityKeyPair(IdentityKey(DjbECPublicKey(publicKey[1:])), DjbECPrivateKey(privateKey))
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 getIdentityKeyPair(self): q = "SELECT public_key, private_key FROM {}_identities WHERE recipient_id = -1".format( self.phoneNumber) dbConn = self.get_conn() c = dbConn.cursor() c.execute(q) result = c.fetchone() dbConn.close() publicKey, privateKey = result return IdentityKeyPair(IdentityKey(DjbECPublicKey(publicKey[1:])), DjbECPrivateKey(privateKey))
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)