Skip to content

Commit 893cd58

Browse files
committed
Send/receive axolotl enc v2 msg,pkmsg
1 parent 6ebbbe2 commit 893cd58

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

yowsup/layers/axolotl/layer.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(self):
4444
self.pendingMessages = {}
4545
self.pendingIncomingMessages = {}
4646
self.skipEncJids = []
47+
self.v2Jids = [] #people we're going to send v2 enc messages
4748

4849
def __str__(self):
4950
return "Axolotl Layer"
@@ -162,12 +163,20 @@ def handlePlaintextNode(self, node):
162163

163164
sessionCipher = self.getSessionCipher(recipient_id)
164165

165-
166-
166+
if node["to"] in self.v2Jids:
167+
version = 2
168+
padded = bytearray()
169+
padded.append(ord("\n"))
170+
padded.extend(self.encodeInt7bit(len(plaintext)))
171+
padded.extend(plaintext)
172+
padded.append(ord("\x01"))
173+
plaintext = padded
174+
else:
175+
version = 1
167176
ciphertext = sessionCipher.encrypt(plaintext)
168177
encEntity = EncryptedMessageProtocolEntity(
169178
EncryptedMessageProtocolEntity.TYPE_MSG if ciphertext.__class__ == WhisperMessage else EncryptedMessageProtocolEntity.TYPE_PKMSG ,
170-
1,
179+
version,
171180
ciphertext.serialize(),
172181
MessageProtocolEntity.MESSAGE_TYPE_TEXT,
173182
_id= node["id"],
@@ -180,8 +189,21 @@ def handlePlaintextNode(self, node):
180189
)
181190
self.toLower(encEntity.toProtocolTreeNode())
182191

192+
def encodeInt7bit(self, value):
193+
v = value
194+
out = bytearray()
195+
while v >= 0x80:
196+
out.append((v | 0x80) % 256)
197+
v >>= 7
198+
out.append(v % 256)
199+
200+
return out
201+
183202
def handleEncMessage(self, node):
184203
try:
204+
if node.getChild("enc")["v"] == "2" and node["from"] not in self.v2Jids:
205+
self.v2Jids.append(node["from"])
206+
185207
if node.getChild("enc")["type"] == "pkmsg":
186208
self.handlePreKeyWhisperMessage(node)
187209
else:
@@ -216,6 +238,10 @@ def handlePreKeyWhisperMessage(self, node):
216238
sessionCipher = self.getSessionCipher(pkMessageProtocolEntity.getFrom(False))
217239
plaintext = sessionCipher.decryptPkmsg(preKeyWhisperMessage)
218240

241+
if pkMessageProtocolEntity.getVersion() == 2:
242+
plaintext = self.unpadV2Plaintext(plaintext)
243+
244+
219245
bodyNode = ProtocolTreeNode("body", data = plaintext)
220246
node.addChild(bodyNode)
221247
self.toUpper(node)
@@ -227,9 +253,19 @@ def handleWhisperMessage(self, node):
227253
sessionCipher = self.getSessionCipher(encMessageProtocolEntity.getFrom(False))
228254
plaintext = sessionCipher.decryptMsg(whisperMessage)
229255

256+
if encMessageProtocolEntity.getVersion() == 2:
257+
plaintext = self.unpadV2Plaintext(plaintext)
258+
230259
bodyNode = ProtocolTreeNode("body", data = plaintext)
231260
node.addChild(bodyNode)
232261
self.toUpper(node)
262+
263+
def unpadV2Plaintext(self, v2plaintext):
264+
if len(v2plaintext) < 128:
265+
return v2plaintext[2:-1]
266+
else: # < 128 * 128
267+
return v2plaintext[3: -1]
268+
233269
####
234270

235271
### keys set and get
@@ -268,7 +304,9 @@ def persistKeys(self, registrationId, identityKeyPair, preKeys, signedPreKey, fr
268304
if currPercentage == prevPercentage:
269305
continue
270306
prevPercentage = currPercentage
271-
logger.debug("%s" % currPercentage + "%")
307+
#logger.debug("%s" % currPercentage + "%")
308+
sys.stdout.write("Storing prekeys %d%% \r" % (currPercentage))
309+
sys.stdout.flush()
272310

273311
if fresh:
274312
self.state = self.__class__._STATE_GENKEYS

yowsup/layers/axolotl/protocolentities/message_encrypted.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def getEncType(self):
3232
def getEncData(self):
3333
return self.encData
3434

35+
def getVersion(self):
36+
return self.encVersion
37+
3538
def toProtocolTreeNode(self):
3639
node = super(EncryptedMessageProtocolEntity, self).toProtocolTreeNode()
3740
encNode = ProtocolTreeNode("enc", data = self.encData)

0 commit comments

Comments
 (0)