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 ])
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)
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 __init__(self, dbConn): """ :type dbConn: Connection """ self.dbConn = dbConn dbConn.execute("CREATE TABLE IF NOT EXISTS identities (_id INTEGER PRIMARY KEY AUTOINCREMENT," "recipient_id INTEGER UNIQUE," "registration_id INTEGER, public_key BLOB, private_key BLOB," "next_prekey_id INTEGER, timestamp INTEGER);") if self.getLocalRegistrationId() is None or self.getIdentityKeyPair() is None: identity = KeyHelper.generateIdentityKeyPair() registration_id = KeyHelper.generateRegistrationId(True) self._storeLocalData(registration_id, identity)
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 ])
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 __init__(self): self.trustedKeys = {} identityKeyPairKeys = Curve.generateKeyPair() self.identityKeyPair = IdentityKeyPair(IdentityKey(identityKeyPairKeys.getPublicKey()), identityKeyPairKeys.getPrivateKey()) self.localRegistrationId = KeyHelper.generateRegistrationId()