Skip to content

Commit faca147

Browse files
committed
Support fragment encoding for signing data
forgot something forgot something add SignedCertificateChain cleanup fix
1 parent 941a297 commit faca147

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed
2.54 KB
Binary file not shown.

shared/global_values.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
APP_PROTOCOL_XSD = "../shared/xsd_files/latest_version/V2G_CI_AppProtocol.xsd"
2626
COMMON_MESSAGES_XSD = "../shared/xsd_files/latest_version/V2G_CI_CommonMessages.xsd"
2727
DC_MESSAGES_XSD = "../shared/xsd_files/latest_version/V2G_CI_DC.xsd"
28+
XMLDSIG_XSD = "../shared/xsd_files/latest_version/xmldsig-core-schema.xsd"
2829
APP_PROTOCOL_EXIG = "../shared/exig_files/latest_version/V2G_CI_AppProtocol.exig"
2930
COMMON_MESSAGES_EXIG = "../shared/exig_files/latest_version/V2G_CI_CommonMessages.exig"
3031
DC_MESSAGES_EXIG = "../shared/exig_files/latest_version/V2G_CI_DC.exig"
32+
XMLDSIG_EXIG = "../shared/exig_files/latest_version/xmldsig-core-schema.exig"
3133

3234
# Passphrase used to access private key. This parameter shall be stored in a secured directory.
3335
PASSPHRASE = "123456789abcdefgh"

shared/message_handling.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from shared.log import logger
2222
import lxml
2323
from shared.global_values import SDP_PAYLOAD_TYPES, MAX_PAYLOAD_LENGTH, APP_PROTOCOL_EXIG, COMMON_MESSAGES_EXIG, \
24-
DC_MESSAGES_EXIG, APP_PROTOCOL_XSD, COMMON_MESSAGES_XSD, DC_MESSAGES_XSD
24+
DC_MESSAGES_EXIG, APP_PROTOCOL_XSD, COMMON_MESSAGES_XSD, DC_MESSAGES_XSD, XMLDSIG_XSD, XMLDSIG_EXIG
2525

2626
import jpype
2727
import os
@@ -107,10 +107,14 @@ class MessageHandler(metaclass=Singleton):
107107
dc_schema = open_exi_schema(DC_MESSAGES_EXIG)
108108
dc_grammar_cache = GrammarCache(dc_schema, options)
109109

110+
xmldsig_schema = open_exi_schema(XMLDSIG_EXIG)
111+
xmldsig_grammar_cache = GrammarCache(xmldsig_schema, options)
112+
110113
def __init__(self):
111114
self.xml_SAP_validator = lxml.etree.XMLSchema(file=APP_PROTOCOL_XSD)
112115
self.xml_Common_validator = lxml.etree.XMLSchema(file=COMMON_MESSAGES_XSD)
113116
self.xml_DC_validator = lxml.etree.XMLSchema(file=DC_MESSAGES_XSD)
117+
self.xml_xmldsig_validator = lxml.etree.XMLSchema(file=XMLDSIG_XSD)
114118
self.parser = XmlParser(context=XmlContext())
115119
self.config = SerializerConfig(pretty_print=True)
116120
self.serializer = XmlSerializer(config=self.config)
@@ -170,7 +174,7 @@ def is_payload_length_correct(v2gtp_message: V2GTPMessage) -> bool:
170174
# return schema
171175

172176
@staticmethod
173-
def encode(xml_contents: str, type_msg: str) -> str:
177+
def encode(xml_contents: str, type_msg: str, fragment: bool = False) -> str:
174178
"""Turns a human-readable string to an EXI-encoded string. Relies on Java classes.
175179
176180
:param xml_contents: The XML string to be encoded.
@@ -182,18 +186,21 @@ def encode(xml_contents: str, type_msg: str) -> str:
182186
output = None
183187
try:
184188
t = MessageHandler.transmogrifier
185-
input = ByteArrayInputStream(contents.getBytes(Charset.forName("ASCII")));
186-
output = ByteArrayOutputStream();
189+
t.setFragment(fragment)
190+
input = ByteArrayInputStream(contents.getBytes(Charset.forName("ASCII")))
191+
output = ByteArrayOutputStream()
187192
if type_msg == "SAP":
188-
t.setGrammarCache(MessageHandler.ap_grammar_cache);
193+
t.setGrammarCache(MessageHandler.ap_grammar_cache)
189194
elif type_msg == "Common":
190-
t.setGrammarCache(MessageHandler.common_grammar_cache);
195+
t.setGrammarCache(MessageHandler.common_grammar_cache)
191196
elif type_msg == "DC":
192-
t.setGrammarCache(MessageHandler.dc_grammar_cache);
197+
t.setGrammarCache(MessageHandler.dc_grammar_cache)
198+
elif type_msg == "xmldsig":
199+
t.setGrammarCache(MessageHandler.xmldsig_grammar_cache)
193200
else:
194201
raise Exception("Unknown message type")
195-
t.setOutputStream(output);
196-
t.encode(InputSource(input));
202+
t.setOutputStream(output)
203+
t.encode(InputSource(input))
197204
result = output.toByteArray()
198205
finally:
199206
if input:
@@ -203,7 +210,7 @@ def encode(xml_contents: str, type_msg: str) -> str:
203210
return result
204211

205212
@staticmethod
206-
def decode(exi_contents: bytes, type_msg: str) -> str:
213+
def decode(exi_contents: bytes, type_msg: str, fragment: bool = False) -> str:
207214
"""Turns encoded EXI bytes to human-readable string. Relies on Java classes.
208215
209216
:param exi_contents: The EXI encoded contents.
@@ -217,13 +224,16 @@ def decode(exi_contents: bytes, type_msg: str) -> str:
217224
try:
218225
input = ByteArrayInputStream(exi_contents)
219226
r = MessageHandler.reader
227+
r.setFragment(fragment)
220228
tf_handler = MessageHandler.transformer_handler
221229
if type_msg == "SAP":
222-
r.setGrammarCache(MessageHandler.ap_grammar_cache);
230+
r.setGrammarCache(MessageHandler.ap_grammar_cache)
223231
elif type_msg == "Common":
224-
r.setGrammarCache(MessageHandler.common_grammar_cache);
232+
r.setGrammarCache(MessageHandler.common_grammar_cache)
225233
elif type_msg == "DC":
226-
r.setGrammarCache(MessageHandler.dc_grammar_cache);
234+
r.setGrammarCache(MessageHandler.dc_grammar_cache)
235+
elif type_msg == "xmldsig":
236+
r.setGrammarCache(MessageHandler.xmldsig_grammar_cache)
227237
else:
228238
raise Exception("Unknown message type")
229239

@@ -309,6 +319,8 @@ def is_xml_valid(self, xml, msg_type):
309319
validator = self.xml_Common_validator
310320
elif msg_type == 'DC':
311321
validator = self.xml_DC_validator
322+
elif msg_type == 'xmldsig':
323+
validator = self.xml_xmldsig_validator
312324
try:
313325
validator.assertValid(xml_file)
314326
is_valid = True

shared/xml_classes/common_messages/v2_g_ci_common_messages.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ class SignedCertificateChainType:
948948
}
949949
)
950950

951+
@dataclass
952+
class SignedCertificateChain(SignedCertificateChainType):
953+
class Meta:
954+
name = "SignedCertificateChain"
955+
namespace = "urn:iso:std:iso:15118:-20:CommonMessages"
951956

952957
@dataclass
953958
class SignedMeteringDataType:
@@ -1463,6 +1468,11 @@ class Meta:
14631468
}
14641469
)
14651470

1471+
@dataclass
1472+
class PnCAreqAuthorizationMode(PnCAreqAuthorizationModeType):
1473+
class Meta:
1474+
name = "PnC_AReqAuthorizationMode"
1475+
namespace = "urn:iso:std:iso:15118:-20:CommonMessages"
14661476

14671477
@dataclass
14681478
class PowerDeliveryRes(PowerDeliveryResType):

0 commit comments

Comments
 (0)