20
20
21
21
import tempfile
22
22
23
+ from securesystemslib .keys import verify_signature
23
24
from securesystemslib .util import persist_temp_file
25
+ from securesystemslib .signer import Signer , Signature
24
26
from securesystemslib .storage import (StorageBackendInterface ,
25
27
FilesystemBackend )
26
- from securesystemslib .keys import create_signature , verify_signature
27
28
28
29
from tuf .api .serialization import (MetadataSerializer , MetadataDeserializer ,
29
30
SignedSerializer )
@@ -90,12 +91,14 @@ def from_dict(cls, metadata: Mapping[str, Any]) -> 'Metadata':
90
91
else :
91
92
raise ValueError (f'unrecognized metadata type "{ _type } "' )
92
93
93
- # NOTE: If Signature becomes a class, we should iterate over
94
- # metadata['signatures'], call Signature.from_dict for each item, and
95
- # pass a list of Signature objects to the Metadata constructor instead.
94
+ signatures = []
95
+ for signature in metadata .pop ('signatures' ):
96
+ signature_obj = Signature .from_dict (signature )
97
+ signatures .append (signature_obj )
98
+
96
99
return cls (
97
100
signed = inner_cls .from_dict (metadata .pop ('signed' )),
98
- signatures = metadata . pop ( ' signatures' ) )
101
+ signatures = signatures )
99
102
100
103
@classmethod
101
104
def from_file (
@@ -139,8 +142,13 @@ def from_file(
139
142
140
143
def to_dict (self ) -> Dict [str , Any ]:
141
144
"""Returns the dict representation of self. """
145
+
146
+ signatures = []
147
+ for sig in self .signatures :
148
+ signatures .append (sig .to_dict ())
149
+
142
150
return {
143
- 'signatures' : self . signatures ,
151
+ 'signatures' : signatures ,
144
152
'signed' : self .signed .to_dict ()
145
153
}
146
154
@@ -178,13 +186,14 @@ def to_file(
178
186
179
187
# Signatures.
180
188
def sign (
181
- self , key : Mapping [ str , Any ] , append : bool = False ,
189
+ self , signer : Signer , append : bool = False ,
182
190
signed_serializer : Optional [SignedSerializer ] = None
183
191
) -> Dict [str , Any ]:
184
192
"""Creates signature over 'signed' and assigns it to 'signatures'.
185
193
186
194
Arguments:
187
- key: A securesystemslib-style private key object used for signing.
195
+ signer: An object implementing the securesystemslib.signer.Signer
196
+ interface.
188
197
append: A boolean indicating if the signature should be appended to
189
198
the list of signatures or replace any existing signatures. The
190
199
default behavior is to replace signatures.
@@ -209,8 +218,7 @@ def sign(
209
218
from tuf .api .serialization .json import CanonicalJSONSerializer
210
219
signed_serializer = CanonicalJSONSerializer ()
211
220
212
- signature = create_signature (key ,
213
- signed_serializer .serialize (self .signed ))
221
+ signature = signer .sign (signed_serializer .serialize (self .signed ))
214
222
215
223
if append :
216
224
self .signatures .append (signature )
@@ -244,7 +252,7 @@ def verify(self, key: Mapping[str, Any],
244
252
245
253
"""
246
254
signatures_for_keyid = list (filter (
247
- lambda sig : sig [ ' keyid' ] == key ['keyid' ], self .signatures ))
255
+ lambda sig : sig . keyid == key ['keyid' ], self .signatures ))
248
256
249
257
if not signatures_for_keyid :
250
258
raise tuf .exceptions .Error (
@@ -262,7 +270,7 @@ def verify(self, key: Mapping[str, Any],
262
270
signed_serializer = CanonicalJSONSerializer ()
263
271
264
272
return verify_signature (
265
- key , signatures_for_keyid [0 ],
273
+ key , signatures_for_keyid [0 ]. to_dict () ,
266
274
signed_serializer .serialize (self .signed ))
267
275
268
276
0 commit comments