19
19
persist_temp_file
20
20
)
21
21
from securesystemslib .storage import StorageBackendInterface
22
- from securesystemslib .keys import create_signature , verify_signature
22
+ from securesystemslib .keys import verify_signature
23
+ from securesystemslib .signer import Signer , Signature
23
24
24
25
import tuf .formats
25
26
import tuf .exceptions
@@ -92,12 +93,17 @@ class also that has a 'from_dict' factory method. (Currently this is
92
93
else :
93
94
raise ValueError (f'unrecognized metadata type "{ _type } "' )
94
95
96
+ signatures = []
97
+ for signature in metadata ['signatures' ]:
98
+ new_signature = Signature (signature ['keyid' ], signature ['sig' ])
99
+ signatures .append (new_signature )
100
+
95
101
# NOTE: If Signature becomes a class, we should iterate over
96
102
# metadata['signatures'], call Signature.from_dict for each item, and
97
103
# pass a list of Signature objects to the Metadata constructor intead.
98
104
return cls (
99
105
signed = inner_cls .from_dict (metadata ['signed' ]),
100
- signatures = metadata [ ' signatures' ] )
106
+ signatures = signatures )
101
107
102
108
103
109
@classmethod
@@ -146,8 +152,13 @@ def from_json_file(
146
152
# Serialization.
147
153
def to_dict (self ) -> JsonDict :
148
154
"""Returns the JSON-serializable dictionary representation of self. """
155
+
156
+ signatures_arr = []
157
+ for sig in self .signatures :
158
+ signatures_arr .append (sig .to_dict ())
159
+
149
160
return {
150
- 'signatures' : self . signatures ,
161
+ 'signatures' : signatures_arr ,
151
162
'signed' : self .signed .to_dict ()
152
163
}
153
164
@@ -184,11 +195,12 @@ def to_json_file(
184
195
185
196
186
197
# Signatures.
187
- def sign (self , key : JsonDict , append : bool = False ) -> JsonDict :
198
+ def sign (self , signer : Signer , append : bool = False ) -> JsonDict :
188
199
"""Creates signature over 'signed' and assigns it to 'signatures'.
189
200
190
201
Arguments:
191
- key: A securesystemslib-style private key object used for signing.
202
+ singer: An object implementing the securesystemslib.signer.Signer
203
+ interface.
192
204
append: A boolean indicating if the signature should be appended to
193
205
the list of signatures or replace any existing signatures. The
194
206
default behavior is to replace signatures.
@@ -203,7 +215,7 @@ def sign(self, key: JsonDict, append: bool = False) -> JsonDict:
203
215
A securesystemslib-style signature object.
204
216
205
217
"""
206
- signature = create_signature ( key , self .signed .to_canonical_bytes ())
218
+ signature = signer . sign ( self .signed .to_canonical_bytes ())
207
219
208
220
if append :
209
221
self .signatures .append (signature )
@@ -232,7 +244,7 @@ def verify(self, key: JsonDict) -> bool:
232
244
233
245
"""
234
246
signatures_for_keyid = list (filter (
235
- lambda sig : sig [ ' keyid' ] == key ['keyid' ], self .signatures ))
247
+ lambda sig : sig . keyid == key ['keyid' ], self .signatures ))
236
248
237
249
if not signatures_for_keyid :
238
250
raise tuf .exceptions .Error (
@@ -244,7 +256,7 @@ def verify(self, key: JsonDict) -> bool:
244
256
f'{ key ["keyid" ]} , not sure which one to verify.' )
245
257
246
258
return verify_signature (
247
- key , signatures_for_keyid [0 ],
259
+ key , signatures_for_keyid [0 ]. to_dict () ,
248
260
self .signed .to_canonical_bytes ())
249
261
250
262
0 commit comments