From 35585a5d15b936a0f345acf7376dc028ebeadcb3 Mon Sep 17 00:00:00 2001 From: Ralph Rakers Date: Tue, 27 Jun 2023 10:14:37 +0200 Subject: [PATCH 1/4] - fixed invalid xml (open/close tag different) due to wrong direction - fixed typo --- Gurux.DLMS.python/gurux_dlms/_GXAPDU.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gurux.DLMS.python/gurux_dlms/_GXAPDU.py b/Gurux.DLMS.python/gurux_dlms/_GXAPDU.py index eea999b..c80cf04 100644 --- a/Gurux.DLMS.python/gurux_dlms/_GXAPDU.py +++ b/Gurux.DLMS.python/gurux_dlms/_GXAPDU.py @@ -404,7 +404,7 @@ def parse(cls, initiateRequest, settings, cipher, data, xml, tag2): raise GXDLMSConfirmedServiceError(ConfirmedServiceError(data.getUInt8()), ServiceError(data.getUInt8()), data.getUInt8()) else: if xml: - xml.appendComment("Error: Failed to descypt data.") + xml.appendComment("Error: Failed to decrypt data.") data.position = len(data) return raise ValueError("Invalid tag.") @@ -433,7 +433,7 @@ def parse(cls, initiateRequest, settings, cipher, data, xml, tag2): # The number of unused bits in the bit string. tag = data.getUInt8() v = cls.getConformanceToArray(data) - if settings.isServer: + if not response: settings.negotiatedConformance = v & settings.proposedConformance if xml: xml.appendStartTag(TranslatorGeneralTags.PROPOSED_CONFORMANCE) From 99d136316f9c2f89823e02619f033805a4b113d5 Mon Sep 17 00:00:00 2001 From: Ralph Rakers Date: Tue, 27 Jun 2023 10:16:09 +0200 Subject: [PATCH 2/4] fixed a bug where setXmlLength function was called on a 'None' reference --- Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py index fa2755a..a0d51be 100644 --- a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py +++ b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py @@ -816,7 +816,7 @@ def _pduToXml2(self, xml, value, omitDeclaration, omitNameSpace, allowUnknownCom xml.endComment() except Exception: # It's OK if this fails. Ciphering settings are not correct. - xml.xml.setXmlLength(len_) + xml.setXmlLength(len_) value.position = originalPosition cnt = _GXCommon.getObjectCount(value) if cnt != len(value) - value.position: From 6fc1ebb31257cd4a5d5fdcffbfb6ef125883e21c Mon Sep 17 00:00:00 2001 From: Ralph Rakers Date: Tue, 27 Jun 2023 10:17:36 +0200 Subject: [PATCH 3/4] added a function that prevents comments in comments and therefore invalid XML --- .../gurux_dlms/GXDLMSTranslatorStructure.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslatorStructure.py b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslatorStructure.py index 0c6f7ed..9c46f00 100644 --- a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslatorStructure.py +++ b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslatorStructure.py @@ -63,6 +63,7 @@ def __init__(self, type_, numericsAshex, numericshex, isHex, addComments, list_) # Amount of spaces. self.offset = 0 self.outputType = type_ + self.comment_started = False numericsAshex = numericshex self.showNumericsAsHex = numericsAshex self.showStringAsHex = isHex @@ -131,9 +132,11 @@ def appendLine(self, tag, name=None, value=None): def appendComment(self, comment): if self.comments: self.appendSpaces(2 * self.offset) - self.sb += "" + if not self.comment_started: + self.sb += "-->" self.sb += '\r' self.sb += '\n' @@ -151,6 +154,7 @@ def startComment(self, comment): self.sb += '\r' self.sb += '\n' self.offset += 1 + self.comment_started = True # # End comment section. @@ -162,6 +166,7 @@ def endComment(self): self.sb += "-->" self.sb += '\r' self.sb += '\n' + self.comment_started = False def append(self, tag, start=None): if isinstance(tag, str): From 6fb363220afccfe3d34f123b7d958147d1c8a681 Mon Sep 17 00:00:00 2001 From: Ralph Rakers Date: Tue, 27 Jun 2023 10:36:57 +0200 Subject: [PATCH 4/4] fixed a bug where the encrypted content was embedded into the comment --- Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py index a0d51be..e791110 100644 --- a/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py +++ b/Gurux.DLMS.python/gurux_dlms/GXDLMSTranslator.py @@ -811,9 +811,8 @@ def _pduToXml2(self, xml, value, omitDeclaration, omitNameSpace, allowUnknownCom p = AesGcmParameter(0, st, settings.cipher.blockCipherKey, settings.cipher.authenticationKey) if p.blockCipherKey: data2 = GXByteBuffer(GXCiphering.decrypt(settings.cipher, p, value)) - xml.startComment("Decrypt data: " + str(data2)) + xml.appendComment("Decrypt data: " + str(data2)) self._pduToXml2(xml, data2, omitDeclaration, omitNameSpace, False) - xml.endComment() except Exception: # It's OK if this fails. Ciphering settings are not correct. xml.setXmlLength(len_) @@ -833,9 +832,8 @@ def _pduToXml2(self, xml, value, omitDeclaration, omitNameSpace, allowUnknownCom p.xml = xml tmp = GXByteBuffer(GXCiphering.decrypt(settings.cipher, p, tmp)) len_ = xml.getXmlLength() - xml.startComment("Decrypt data: " + str(tmp)) + xml.appendComment("Decrypt data: " + str(tmp)) self._pduToXml2(xml, tmp, omitDeclaration, omitNameSpace, False) - xml.endComment() except Exception: # It's OK if this fails. Ciphering settings are not # correct.