1313from common .allocation import Allocation
1414from common .block import Block
1515from common .encryption_key import EncryptionKey
16+ from common .exceptions import EncryptorNotRegisteredForClientError
17+ from common .logging import LOGGER
1618from common .pool import Pool
1719from common .share import Share
1820from common .share_api import APIGetShareResponse , APIPostShareRequest
@@ -75,6 +77,7 @@ def generate_block_for_client(
7577 Generate a block of PSRD for a peer client.
7678 """
7779 if client_name not in self ._peer_clients :
80+ LOGGER .warning (f"Peer client '{ client_name } ' not found" )
7881 raise exceptions .ClientNotRegisteredError (client_name )
7982 peer_client = self ._peer_clients [client_name ]
8083 match pool_owner_str .lower ():
@@ -83,6 +86,9 @@ def generate_block_for_client(
8386 case "hub" :
8487 pool_owner = Pool .Owner .LOCAL
8588 case _:
89+ LOGGER .warning (
90+ f"Invalid pool owner { pool_owner_str } for peer client { client_name } "
91+ )
8692 raise exceptions .InvalidPoolOwnerError (pool_owner_str )
8793 block = peer_client .create_random_block (pool_owner , size )
8894 return block
@@ -96,11 +102,22 @@ async def store_share_received_from_client(
96102 """
97103 Store a key share posted by a client.
98104 """
105+ # Lookup the peer client
99106 client_name = api_post_share_request .master_client_name
100107 if client_name not in self ._peer_clients :
108+ LOGGER .warning (f"Peer client { client_name } not found" )
101109 raise exceptions .ClientNotRegisteredError (client_name )
102110 peer_client = self ._peer_clients [client_name ]
111+ # Verify the request signature
103112 await peer_client .check_request_signature (raw_request )
113+ # Check that the master encryptor (SAE) is one that was registered for the client
114+ master_sae_id = api_post_share_request .master_sae_id
115+ if master_sae_id not in peer_client .encryptor_names :
116+ LOGGER .warning (
117+ f"Encryptor { master_sae_id } not registered for client { client_name } "
118+ )
119+ raise EncryptorNotRegisteredForClientError (client_name , master_sae_id )
120+ # Decrypt the share value
104121 encryption_key_allocation = Allocation .from_api (
105122 api_post_share_request .encryption_key_allocation , peer_client .peer_pool
106123 )
@@ -120,6 +137,7 @@ async def store_share_received_from_client(
120137 # TODO: Check if the key UUID is already present, and if so, do something sensible
121138 self ._shares [share .user_key_id ] = share
122139 peer_client .add_dske_signing_key_header_to_response (headers_temp_response )
140+ # Clean up fully used blocks
123141 peer_client .delete_fully_used_blocks ()
124142
125143 async def get_share_requested_by_client (
@@ -132,25 +150,35 @@ async def get_share_requested_by_client(
132150 """
133151 Get a key share.
134152 """
153+ # Lookup the peer client
154+ if client_name not in self ._peer_clients :
155+ LOGGER .warning (f"Peer client { client_name } not found" )
156+ raise exceptions .ClientNotRegisteredError (client_name )
157+ peer_client = self ._peer_clients [client_name ]
158+ # Verify the request signature
159+ await peer_client .check_request_signature (raw_request )
160+ # Lookup the share
135161 try :
136162 key_id = UUID (key_id_str )
137163 except ValueError as exc :
164+ LOGGER .warning (f"Invalid key ID { key_id_str } " )
138165 raise exceptions .InvalidKeyIDError (key_id_str ) from exc
139- # TODO: Error handling: share is not in the store
140166 try :
141167 share = self ._shares [key_id ]
142168 except KeyError as exc :
169+ LOGGER .warning (f"No share for key ID { key_id_str } " )
143170 raise exceptions .UnknownKeyIDError (key_id ) from exc
144- peer_client = self ._peer_clients [client_name ]
145- await peer_client .check_request_signature (raw_request )
171+ # Encrypt the share value
146172 encryption_key = EncryptionKey .from_pool (peer_client .local_pool , share .size )
147173 encrypted_share_value = encryption_key .encrypt (share .value )
174+ # Prepare the response
148175 response = APIGetShareResponse (
149176 share_index = share .share_index ,
150177 encryption_key_allocation = encryption_key .allocation .to_api (),
151178 encrypted_share_value = bytes_to_str (encrypted_share_value ),
152179 )
153180 peer_client .add_dske_signing_key_header_to_response (headers_temp_response )
181+ # Clean up fully used blocks
154182 peer_client .delete_fully_used_blocks ()
155183 return response
156184
0 commit comments