Example #1
0
    def sendKeys(self, fresh=True, countPreKeys=_COUNT_PREKEYS):
        identityKeyPair = KeyHelper.generateIdentityKeyPair(
        ) if fresh else self.store.getIdentityKeyPair()
        registrationId = KeyHelper.generateRegistrationId(
        ) if fresh else self.store.getLocalRegistrationId()
        preKeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(),
                                            countPreKeys)
        signedPreKey = KeyHelper.generateSignedPreKey(
            identityKeyPair, KeyHelper.getRandomSequence(65536))
        preKeysDict = {}
        for preKey in preKeys:
            keyPair = preKey.getKeyPair()
            preKeysDict[self.adjustId(preKey.getId())] = self.adjustArray(
                keyPair.getPublicKey().serialize()[1:])

        signedKeyTuple = (
            self.adjustId(signedPreKey.getId()),
            self.adjustArray(
                signedPreKey.getKeyPair().getPublicKey().serialize()[1:]),
            self.adjustArray(signedPreKey.getSignature()))

        setKeysIq = SetKeysIqProtocolEntity(
            self.adjustArray(identityKeyPair.getPublicKey().serialize()[1:]),
            signedKeyTuple, preKeysDict, Curve.DJB_TYPE,
            self.adjustId(registrationId))

        onResult = lambda _, __: self.persistKeys(
            registrationId, identityKeyPair, preKeys, signedPreKey, fresh)
        self._sendIq(setKeysIq, onResult, self.onSentKeysError)
    def setUp(self):
        super(ResultIqProtocolEntityTest, self).setUp()
        self.ProtocolEntity = ResultGetKeysIqProtocolEntity
        listNode = ProtocolTreeNode("list")
        self.node.addChild(listNode)

        for i in range(0, 1):
            userNode = ProtocolTreeNode(
                "user",
                {"jid": "user_%s@%s" % (i, YowConstants.WHATSAPP_SERVER)})
            listNode.addChild(userNode)
            registrationNode = ProtocolTreeNode(
                "registration",
                data=ResultGetKeysIqProtocolEntity._intToBytes(
                    KeyHelper.generateRegistrationId()))

            typeNode = ProtocolTreeNode(
                "type",
                data=ResultGetKeysIqProtocolEntity._intToBytes(Curve.DJB_TYPE))

            identityKeyPair = KeyHelper.generateIdentityKeyPair()
            identityNode = ProtocolTreeNode("identity",
                                            data=identityKeyPair.getPublicKey(
                                            ).getPublicKey().getPublicKey())

            signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, i)

            signedPreKeyNode = ProtocolTreeNode("skey")
            signedPreKeyNode_idNode = ProtocolTreeNode(
                "id",
                data=ResultGetKeysIqProtocolEntity._intToBytes(
                    signedPreKey.getId()))

            signedPreKeyNode_valueNode = ProtocolTreeNode(
                "value",
                data=signedPreKey.getKeyPair().getPublicKey().getPublicKey())

            signedPreKeyNode_sigNode = ProtocolTreeNode(
                "signature", data=signedPreKey.getSignature())

            signedPreKeyNode.addChildren([
                signedPreKeyNode_idNode, signedPreKeyNode_valueNode,
                signedPreKeyNode_sigNode
            ])

            preKey = KeyHelper.generatePreKeys(i * 10, 1)[0]

            preKeyNode = ProtocolTreeNode("key")
            preKeyNode_idNode = ProtocolTreeNode(
                "id",
                data=ResultGetKeysIqProtocolEntity._intToBytes(preKey.getId()))
            preKeyNode_valNode = ProtocolTreeNode(
                "value",
                data=preKey.getKeyPair().getPublicKey().getPublicKey())
            preKeyNode.addChildren([preKeyNode_idNode, preKeyNode_valNode])

            userNode.addChildren([
                registrationNode, typeNode, identityNode, signedPreKeyNode,
                preKeyNode
            ])
Example #3
0
    def _generate_axolotl_keys(self):
        identityKeyPair = KeyHelper.generateIdentityKeyPair()
        registrationId = KeyHelper.generateRegistrationId()
        preKeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(),
                                            DEFAULT_PREKEY_AMOUNT)
        self.storeLocalData(registrationId, identityKeyPair)

        for preKey in preKeys:
            self.storePreKey(preKey.getId(), preKey)
Example #4
0
    def _generate_axolotl_keys(self):
        identityKeyPair = KeyHelper.generateIdentityKeyPair()
        registrationId = KeyHelper.generateRegistrationId()
        preKeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(),
                                            DEFAULT_PREKEY_AMOUNT)
        self.storeLocalData(registrationId, identityKeyPair)

        for preKey in preKeys:
            self.storePreKey(preKey.getId(), preKey)
Example #5
0
    def init_store(self):
        """Create new identity key pair and initialize database"""
        logger.info('Creating new identity...')
        identityKeyPair = KeyHelper.generateIdentityKeyPair()
        registrationId = KeyHelper.generateRegistrationId()
        preKeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(), self.COUNT_PREKEYS)
        signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair,
                                                      KeyHelper.getRandomSequence(65536))

        self.store.storeLocalData(registrationId, identityKeyPair)
        self.store.storeSignedPreKey(signedPreKey.getId(), signedPreKey)

        for preKey in preKeys:
            self.store.storePreKey(preKey.getId(), preKey)
    def _generate_axolotl_keys(self):
        identity_key_pair = KeyHelper.generateIdentityKeyPair()
        registration_id = KeyHelper.getRandomSequence(2147483647)
        pre_keys = KeyHelper.generatePreKeys(
            KeyHelper.getRandomSequence(4294967296), DEFAULT_PREKEY_AMOUNT)
        self.storeLocalData(registration_id, identity_key_pair)

        signed_pre_key = KeyHelper.generateSignedPreKey(
            identity_key_pair, KeyHelper.getRandomSequence(65536))

        self.storeSignedPreKey(signed_pre_key.getId(), signed_pre_key)

        for pre_key in pre_keys:
            self.storePreKey(pre_key.getId(), pre_key)
Example #7
0
    def init_store(self):
        """Create new identity key pair and initialize database"""
        logger.info('Creating new identity...')
        identityKeyPair = KeyHelper.generateIdentityKeyPair()
        registrationId = KeyHelper.generateRegistrationId()
        preKeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(),
                                            self.COUNT_PREKEYS)
        signedPreKey = KeyHelper.generateSignedPreKey(
            identityKeyPair, KeyHelper.getRandomSequence(65536))

        self.store.storeLocalData(registrationId, identityKeyPair)
        self.store.storeSignedPreKey(signedPreKey.getId(), signedPreKey)

        for preKey in preKeys:
            self.store.storePreKey(preKey.getId(), preKey)
    def setUp(self):
        super(ResultIqProtocolEntityTest, self).setUp()
        self.ProtocolEntity = ResultGetKeysIqProtocolEntity
        listNode = ProtocolTreeNode("list")
        self.node.addChild(listNode)

        for i in range(0, 1):
            userNode = ProtocolTreeNode("user", {"jid": "user_%s@%s" % (i, YowConstants.WHATSAPP_SERVER)})
            listNode.addChild(userNode)
            registrationNode = ProtocolTreeNode("registration",
                                                data = ResultGetKeysIqProtocolEntity._intToBytes(
                                                    KeyHelper.generateRegistrationId()))

            typeNode = ProtocolTreeNode("type", data = ResultGetKeysIqProtocolEntity._intToBytes(Curve.DJB_TYPE))

            identityKeyPair = KeyHelper.generateIdentityKeyPair()
            identityNode = ProtocolTreeNode("identity", data=identityKeyPair.getPublicKey().getPublicKey().getPublicKey())

            signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, i)

            signedPreKeyNode = ProtocolTreeNode("skey")
            signedPreKeyNode_idNode = ProtocolTreeNode("id",
                                                       data = ResultGetKeysIqProtocolEntity._intToBytes(
                                                           signedPreKey.getId()))

            signedPreKeyNode_valueNode = ProtocolTreeNode("value",
                                                          data = signedPreKey.getKeyPair().getPublicKey().getPublicKey())

            signedPreKeyNode_sigNode = ProtocolTreeNode("signature",
                                                        data = signedPreKey.getSignature())

            signedPreKeyNode.addChildren([signedPreKeyNode_idNode, signedPreKeyNode_valueNode, signedPreKeyNode_sigNode])

            preKey = KeyHelper.generatePreKeys(i * 10, 1)[0]

            preKeyNode = ProtocolTreeNode("key")
            preKeyNode_idNode = ProtocolTreeNode("id", data = ResultGetKeysIqProtocolEntity._intToBytes(preKey.getId()))
            preKeyNode_valNode = ProtocolTreeNode("value", data = preKey.getKeyPair().getPublicKey().getPublicKey())
            preKeyNode.addChildren([preKeyNode_idNode, preKeyNode_valNode])

            userNode.addChildren([
                registrationNode,
                typeNode,
                identityNode,
                signedPreKeyNode,
                preKeyNode
            ])
Example #9
0
    def sendKeys(self, fresh = True, countPreKeys = _COUNT_PREKEYS):
        identityKeyPair     = KeyHelper.generateIdentityKeyPair() if fresh else self.store.getIdentityKeyPair()
        registrationId      = KeyHelper.generateRegistrationId() if fresh else self.store.getLocalRegistrationId()
        preKeys             = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(), countPreKeys)
        signedPreKey        = KeyHelper.generateSignedPreKey(identityKeyPair, KeyHelper.getRandomSequence(65536))
        preKeysDict = {}
        for preKey in preKeys:
            keyPair = preKey.getKeyPair()
            preKeysDict[self.adjustId(preKey.getId())] = self.adjustArray(keyPair.getPublicKey().serialize()[1:])

        signedKeyTuple = (self.adjustId(signedPreKey.getId()),
                          self.adjustArray(signedPreKey.getKeyPair().getPublicKey().serialize()[1:]),
                          self.adjustArray(signedPreKey.getSignature()))

        setKeysIq = SetKeysIqProtocolEntity(self.adjustArray(identityKeyPair.getPublicKey().serialize()[1:]), signedKeyTuple, preKeysDict, Curve.DJB_TYPE, self.adjustId(registrationId))

        onResult = lambda _, __: self.persistKeys(registrationId, identityKeyPair, preKeys, signedPreKey, fresh)
        self._sendIq(setKeysIq, onResult, self.onSentKeysError)
Example #10
0
 def level_prekeys(self, force=False):
     logger.debug("level_prekeys(force=%s)" % force)
     pending_prekeys = self._store.loadPreKeys()
     logger.debug("len(pending_prekeys) = %d" % len(pending_prekeys))
     if force or len(pending_prekeys) < self.THRESHOLD_REGEN:
         count_gen = self.COUNT_GEN_PREKEYS - len(pending_prekeys)
         logger.info("Generating %d prekeys" % count_gen)
         ## arbitrary, should keep track of generated prekey ids and create from there
         prekeys = KeyHelper.generatePreKeys(KeyHelper.getRandomSequence(2**32 // 2), count_gen)
         logger.info("Storing %d prekeys" % len(prekeys))
         for i in range(0, len(prekeys)):
             key = prekeys[i]
             if logger.level <= logging.DEBUG:
                 sys.stdout.write("Storing prekey %d/%d \r" % (i + 1, len(prekeys)))
                 sys.stdout.flush()
             self._store.storePreKey(key.getId(), key)
         return prekeys
     return []
Example #11
0
 def level_prekeys(self, force=False):
     logger.debug("level_prekeys(force=%s)" % force)
     len_pending_prekeys = len(self._store.loadPreKeys())
     logger.debug("len(pending_prekeys) = %d" % len_pending_prekeys)
     if force or len_pending_prekeys < self.THRESHOLD_REGEN:
         count_gen = self.COUNT_GEN_PREKEYS
         max_prekey_id = self._store.preKeyStore.loadMaxPreKeyId()
         logger.info("Generating %d prekeys, current max_prekey_id=%d" %
                     (count_gen, max_prekey_id))
         prekeys = KeyHelper.generatePreKeys(max_prekey_id + 1, count_gen)
         logger.info("Storing %d prekeys" % len(prekeys))
         for i in range(0, len(prekeys)):
             key = prekeys[i]
             if logger.level <= logging.DEBUG:
                 sys.stdout.write("Storing prekey %d/%d \r" %
                                  (i + 1, len(prekeys)))
                 sys.stdout.flush()
             self._store.storePreKey(key.getId(), key)
         return prekeys
     return []
 def generateNewPreKeys(self, count):
     prekey_id = self.getCurrentPreKeyId() or 0
     pre_keys = KeyHelper.generatePreKeys(prekey_id + 1, count)
     for pre_key in pre_keys:
         self.storePreKey(pre_key.getId(), pre_key)
    def generateNewPreKeys(self, count):
        startId = self.getCurrentPreKeyId() + 1
        preKeys = KeyHelper.generatePreKeys(startId, count)

        for preKey in preKeys:
            self.storePreKey(preKey.getId(), preKey)