Skip to content

Commit f1d12dc

Browse files
committed
do not unpack a upp before verification
1 parent f72cecc commit f1d12dc

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

examples/example-client.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,25 @@ def __init__(self, uuid: UUID, auth: str, env: str = DEFAULT_UBIRCH_ENV):
100100

101101
# a variable to store the current upp
102102
self.currentUPP = None
103+
self.currentSig = None
103104

104105
def run(self, data: dict):
105106
""" create and send a ubirch protocol message; verify the response """
106107
# create the upp
107108
self.currentUPP = self.createUPP(data)
109+
_, self.currentSig = self.protocol.upp_msgpack_split_signature(self.currentUPP)
108110

109111
logging.info("Created UPP: %s" % str(self.currentUPP.hex()))
110112

111113
# send the upp and handle the response
112114
resp = self.sendUPP(self.currentUPP)
115+
113116
self.handleBackendResponse(resp)
114-
117+
115118
# the handle function is expected to sys.exit() on any kind of error - assume success
116119
logging.info("Successfully sent the UPP and verified the response!")
117120

118-
# save last signature
121+
# save last signatures
119122
self.protocol.persist(self.uuid)
120123

121124
def checkRegisterPubkey(self):
@@ -134,7 +137,8 @@ def checkRegisterPubkey(self):
134137
logger.info("{}: public key registered".format(self.uuid))
135138
else:
136139
logger.error("{}: registration failed".format(self.uuid))
137-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
140+
141+
raise Exception("Failed to register the public key!")
138142

139143
def createUPP(self, message: dict) -> bytes:
140144
""" creates an UPP from a given message """
@@ -153,7 +157,7 @@ def sendUPP(self, upp: bytes) -> Response:
153157
# send chained protocol message to UBIRCH authentication service
154158
return self.api.send(self.uuid, upp)
155159

156-
def handleBackendResponse(self, response: Response):
160+
def handleBackendResponse(self, response: Response) -> bool:
157161
""" handles the response object returned by sendUPP """
158162
# check the http status code
159163
#
@@ -162,53 +166,30 @@ def handleBackendResponse(self, response: Response):
162166
if response.status_code != codes.ok:
163167
logger.error("Sending UPP failed! response: ({}) {}".format(response.status_code,
164168
binascii.hexlify(response.content).decode()))
165-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
169+
170+
raise(Exception("Exiting due to failure sending the UPP to the backend!"))
166171

167172
logger.info("UPP successfully sent. response: {}".format(binascii.hexlify(response.content).decode()))
168173

169-
# FIXME do not unpack before signature is verified
170-
# unpack the UPP
171-
try:
172-
unpackedUPP = self.protocol.unpack_upp(response.content)
173-
except Exception as e:
174-
logger.error("Error unpacking the response UPP: '%s'" % str(response.content))
175-
logger.exception(e)
176-
177-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
178-
179-
# get the index of the signature and previous signature
180-
sigIndex = self.protocol.get_unpacked_index(unpackedUPP[0], ubirch.ubirch_protocol.UNPACKED_UPP_FIELD_SIG)
181-
prevSigIndex = self.protocol.get_unpacked_index(unpackedUPP[0],
182-
ubirch.ubirch_protocol.UNPACKED_UPP_FIELD_PREV_SIG)
183-
184-
# check if a valid index for the signature was returned
185-
if sigIndex == -1:
186-
logger.error("The message returned by the backend doesn't contain a signature!")
187-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
188-
189174
# verify that the response came from the backend
190-
try:
191-
self.protocol.verfiy_signature(response.content)
175+
if self.protocol.verfiy_signature(UBIRCH_UUIDS[self.env], response.content) == True:
192176
logger.info("Backend response signature successfully verified!")
193-
except Exception as e:
177+
else:
194178
logger.error("Backend response signature verification FAILED!")
195-
logger.exception(e)
196-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
197-
198-
# check if a valid index for the previous signature was returned
199-
if prevSigIndex == -1:
200-
logger.error("The message returned by the backend doesn't contain a previous signature!")
201-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle in main()
179+
180+
raise(Exception("Exiting due to failed signature verification!"))
202181

203-
# unpack the previously sent upp; assume that it is a valid chained upp
204-
unpackedPrevUpp = self.protocol.unpack_upp(self.currentUPP)
182+
# unpack the received upp to get its previous signature
183+
unpacked = self.protocol.unpack_upp(response.content)
184+
prevSig = unpacked[self.protocol.get_unpacked_index(unpacked[0], ubirch.ubirch_protocol.UNPACKED_UPP_FIELD_PREV_SIG)]
205185

206186
# verfiy that the response contains the signature of our upp
207-
if unpackedPrevUpp[sigIndex] != unpackedUPP[prevSigIndex]:
187+
if self.currentSig != prevSig:
208188
logger.error("The previous signature in the response UPP doesn't match the signature of our UPP!")
209-
logger.error("Previous signature in the response UPP: %s" % str(unpackedUPP[prevSigIndex].hex()))
210-
logger.error("Actual signature of our UPP: %s" % str(unpackedPrevUpp[prevSigIndex].hex()))
211-
sys.exit(1) # FIXME do not exit from client methods. throw exception and handle/log in main()
189+
logger.error("Previous signature in the response UPP: %s" % str(prevSig.hex()))
190+
logger.error("Actual signature of our UPP: %s" % str(self.currentSig.hex()))
191+
192+
raise(Exception("Exiting due to a non-matching signature in the response UPP!"))
212193
else:
213194
logger.info("Matching previous signature!")
214195

@@ -245,4 +226,9 @@ def get_message(uuid: UUID) -> dict:
245226

246227
# todo >> send data message to data service / cloud / customer backend here <<
247228

248-
client.run(data)
229+
try:
230+
client.run(data)
231+
except Exception as e:
232+
logger.exception(e)
233+
234+
sys.exit(1)

0 commit comments

Comments
 (0)