Skip to content
Snippets Groups Projects
Commit aac8465d authored by ptangu01's avatar ptangu01
Browse files

add autodoc comments and clean code encode/decode method

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1336 b32b6428-25c9-4566-ad07-03861ab6144f
parent 33c756ec
No related branches found
No related tags found
No related merge requests found
......@@ -69,7 +69,12 @@ class MessageFactory():
# Message Serialization: Json encoding / decoding
#####################################################
def encodeMSG(self,msg):
""" Apply security layer and return encode MSG in Json """
"""Apply security layer and return encode MSG in Json
:param msg: xAAL msg instance
:type msg: Message
:return: return an xAAL msg ciphered and serialized in json
:rtype: json
"""
result={}
# Format data msg to send
......@@ -79,24 +84,29 @@ class MessageFactory():
# Format payload before ciphering
if msg.getBody():
payloadBeforeCiph=json.dumps({"header":msg.getHeader(),"body":msg.getBody()})
payload = json.dumps({"header":msg.getHeader(),"body":msg.getBody()})
else:
payloadBeforeCiph=json.dumps({"header":msg.getHeader()})
payload = json.dumps({"header":msg.getHeader()})
# Payload Ciphering
# Payload Ciphering: ciph
ad = json.dumps(msg.getTargets()) # Additionnal Data == json serialization of the targets array
nonce = buildNonce(msg.getTimestamp())
key = self.getCipherKey()
payloadCiph=pysodium.crypto_aead_chacha20poly1305_encrypt(payloadBeforeCiph,ad,nonce,key)
ciph = pysodium.crypto_aead_chacha20poly1305_encrypt(payload,ad,nonce,key)
# Add payload: base64 encoded of payloadCiph to data msg to send
result['payload'] = base64.b64encode(payloadCiph)
# Add payload: base64 encoded of payload cipher
result['payload'] = base64.b64encode(ciph)
# Json serialization
return json.dumps(result)
def decodeMSG(self,data):
""" decode incoming Json data and De-Ciphering """
"""Decode incoming Json data and De-Ciphering
:param data: data received from the multicast bus
:type data: json
:return: xAAL msg
:rtype: Message
"""
# Decode json incoming data
try:
dataRx = json.loads(data)
......@@ -105,6 +115,7 @@ class MessageFactory():
# Instanciate Message
msg=Message()
if 'targets'in dataRx:
targets = json.loads(dataRx['targets'])
msg.setTargets(targets)
......@@ -118,6 +129,7 @@ class MessageFactory():
# Replay attack, window fixed to CIPHER_WINDOW in seconds
now = buildTimestamp()[0] # test done only on seconds ...
msgTime = dataRx['timestamp'][0]
if msgTime < (now - CIPHER_WINDOW):
raise MessageFactoryParserError("Potential replay attack, message too old")
......@@ -128,19 +140,20 @@ class MessageFactory():
ad = dataRx['targets'].encode("utf8") # Additional Data
nonce = buildNonce(dataRx['timestamp'])
key = self.getCipherKey()
if 'payload' in dataRx:
payloadCiph=base64.b64decode(dataRx['payload'])
ciph = base64.b64decode(dataRx['payload'])
else:
raise MessageFactoryParserError("Bad Message, no payload found!")
try:
payloadDeCiph=pysodium.crypto_aead_chacha20poly1305_decrypt(payloadCiph,ad,nonce,key)
pJson=pysodium.crypto_aead_chacha20poly1305_decrypt(ciph,ad,nonce,key)
except:
raise MessageFactoryParserError("Unable to decrypt msg")
# Decode Json after deciphering
try:
payload=json.loads(payloadDeCiph)
payload = json.loads(pJson)
except:
raise MessageFactoryParserError("Unable to parse JSON data in payload after decrypt")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment