Skip to content

Commit 6b3f058

Browse files
committed
Add test test_wrong_master_sae_id
1 parent dc1f90a commit 6b3f058

5 files changed

Lines changed: 104 additions & 2 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"RINT",
5454
"Rivest",
5555
"rtype",
56+
"SAEID",
5657
"Satoshi",
5758
"setaf",
5859
"Shamir",

common/exceptions.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,39 @@ def __init__(self, client_name: str):
382382
"client_name": client_name,
383383
},
384384
)
385+
386+
387+
class WrongMasterSAEIDError(DSKEException):
388+
"""
389+
Exception raised when the master SAE ID in a Get key with key IDs request does not match
390+
the master SAE ID used in the original Get key request.
391+
"""
392+
393+
def __init__(self, client_name: str, master_sae_id: str, key_id: str):
394+
super().__init__(
395+
status_code=status.HTTP_400_BAD_REQUEST,
396+
message="Master SAE ID does not match the one used in the original Get key request.",
397+
details={
398+
"client_name": client_name,
399+
"master_sae_id": master_sae_id,
400+
"key_id": key_id,
401+
},
402+
)
403+
404+
405+
class WrongSlaveSAEIDError(DSKEException):
406+
"""
407+
Exception raised when the slave SAE ID in a Get key with key IDs request does not match
408+
the slave SAE ID used in the original Get key request.
409+
"""
410+
411+
def __init__(self, client_name: str, slave_sae_id: str, key_id: str):
412+
super().__init__(
413+
status_code=status.HTTP_400_BAD_REQUEST,
414+
message="Slave SAE ID does not match the one used in the original Get key request.",
415+
details={
416+
"client_name": client_name,
417+
"slave_sae_id": slave_sae_id,
418+
"key_id": key_id,
419+
},
420+
)

hub/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,22 @@ async def post_key_share(
129129
async def get_key_share(
130130
client_name: str,
131131
key_id: str,
132+
master_sae_id: str,
133+
slave_sae_id: str,
132134
raw_request: fastapi.Request,
133135
headers_temp_response: fastapi.Response,
134136
) -> APIGetShareResponse:
135137
"""
136138
DSKE API: Get key share.
137139
"""
140+
# pylint: disable=too-many-function-args
138141
headers_temp_response = await _HUB.get_share_requested_by_client(
139-
client_name, key_id, raw_request, headers_temp_response
142+
client_name,
143+
key_id,
144+
master_sae_id,
145+
slave_sae_id,
146+
raw_request,
147+
headers_temp_response,
140148
)
141149
return headers_temp_response
142150

hub/hub.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
from common.allocation import Allocation
1414
from common.block import Block
1515
from common.encryption_key import EncryptionKey
16-
from common.exceptions import EncryptorNotRegisteredForClientError
16+
from common.exceptions import (
17+
EncryptorNotRegisteredForClientError,
18+
WrongMasterSAEIDError,
19+
)
1720
from common.logging import LOGGER
1821
from common.pool import Pool
1922
from common.share import Share
@@ -144,6 +147,8 @@ async def get_share_requested_by_client(
144147
self,
145148
client_name: str,
146149
key_id_str: str,
150+
master_sae_id: str,
151+
slave_sae_id: str,
147152
raw_request: fastapi.Request,
148153
headers_temp_response: fastapi.Response,
149154
) -> APIGetShareResponse:
@@ -168,6 +173,22 @@ async def get_share_requested_by_client(
168173
except KeyError as exc:
169174
LOGGER.warning(f"No share for key ID {key_id_str}")
170175
raise exceptions.UnknownKeyIDError(key_id) from exc
176+
# Check that master and slave SAE IDs in the Get key with key IDs request match those in the
177+
# original Get key request.
178+
if master_sae_id != share.master_sae_id:
179+
LOGGER.warning(
180+
f"Requested master SAE ID {master_sae_id} does not match stored master SAE ID "
181+
f"{share.master_sae_id} for key ID {key_id_str}"
182+
)
183+
raise exceptions.WrongMasterSAEIDError(
184+
client_name, master_sae_id, key_id_str
185+
)
186+
if slave_sae_id != share.slave_sae_id:
187+
LOGGER.warning(
188+
f"Requested slave SAE ID {slave_sae_id} does not match stored slave SAE ID "
189+
f"{share.slave_sae_id} for key ID {key_id_str}"
190+
)
191+
raise WrongMasterSAEIDError(client_name, slave_sae_id, key_id_str)
171192
# Encrypt the share value
172193
encryption_key = EncryptionKey.from_pool(peer_client.local_pool, share.size)
173194
encrypted_share_value = encryption_key.encrypt(share.value)

system_tests/test_etsi_qkd.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import uuid
6+
import httpx
67
import pytest
78
from . import system_test_common
89

@@ -57,3 +58,38 @@ def test_wrong_key_id():
5758
system_test_common.get_key_with_key_ids(
5859
"sam", "sofia", wrong_uuid, expected_status_code=400
5960
)
61+
62+
63+
def test_wrong_master_sae_id():
64+
"""
65+
ETSI QKD Get ey with key IDs, using a master SAE ID that does not match the master SAE ID
66+
that was used in the Get key call (expect error).
67+
"""
68+
key_id = system_test_common.get_key("sam", "sofia")
69+
assert key_id is not None
70+
connie_port = 8108
71+
url = (
72+
f"http://127.0.0.1:{connie_port}"
73+
f"/client/connie/etsi/api/v1/keys/wrong_master_sae_id/dec_keys?"
74+
f"key_ID={key_id}"
75+
)
76+
result = httpx.get(url, headers={"Authorization": "sofia"})
77+
assert result.status_code == 400
78+
assert "Master SAE ID does not match" in result.text
79+
80+
81+
# def test_wrong_slave_sae_id():
82+
# """
83+
# ETSI QKD Get ey with key IDs, using a master SAE ID that does not match the master SAE ID
84+
# that was used in the Get key call (expect error).
85+
# """
86+
# key_id = system_test_common.get_key("sam", "sofia")
87+
# assert key_id is not None
88+
# connie_port = 8108
89+
# url = (
90+
# f"http://127.0.0.1:{connie_port}/client/connie/etsi/api/v1/keys/sunny/dec_keys?"
91+
# f"key_ID={key_id}"
92+
# )
93+
# result = httpx.get(url)
94+
# assert result.status_code == 400
95+
# assert "Master SAE ID does not match" in result.text

0 commit comments

Comments
 (0)