22
22
import dev .sigstore .encryption .Keys ;
23
23
import dev .sigstore .encryption .signers .Verifiers ;
24
24
import dev .sigstore .tuf .model .*;
25
+ import dev .sigstore .tuf .model .TargetMeta .TargetData ;
25
26
import java .io .IOException ;
26
27
import java .nio .charset .StandardCharsets ;
27
28
import java .security .InvalidKeyException ;
@@ -97,7 +98,7 @@ public void update()
97
98
downloadTargets (trustedMetaStore .getTargets ());
98
99
}
99
100
100
- void updateMeta () throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
101
+ public void updateMeta () throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
101
102
updateRoot ();
102
103
var oldTimestamp = trustedMetaStore .findTimestamp ();
103
104
updateTimestamp ();
@@ -112,6 +113,15 @@ void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKeySpecEx
112
113
updateTargets ();
113
114
}
114
115
116
+ public void downloadTarget (String targetName )
117
+ throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
118
+ var targetData = trustedMetaStore .getTargets ().getSignedMeta ().getTargets ().get (targetName );
119
+ if (targetData == null ) {
120
+ throw new TargetMetadataMissingException (targetName );
121
+ }
122
+ downloadTarget (targetName , targetData );
123
+ }
124
+
115
125
// https://theupdateframework.github.io/specification/latest/#detailed-client-workflow
116
126
void updateRoot ()
117
127
throws IOException , RoleExpiredException , NoSuchAlgorithmException , InvalidKeySpecException ,
@@ -304,7 +314,6 @@ void updateTimestamp()
304
314
localTimestamp .getSignedMeta ().getVersion (), timestamp .getSignedMeta ().getVersion ());
305
315
}
306
316
if (localTimestamp .getSignedMeta ().getVersion () == timestamp .getSignedMeta ().getVersion ()) {
307
- trustedMetaStore .setTimestamp (localTimestamp );
308
317
return ;
309
318
}
310
319
}
@@ -459,24 +468,28 @@ void downloadTargets(Targets targets)
459
468
throw new TargetMetadataMissingException (targetName );
460
469
}
461
470
TargetMeta .TargetData targetData = entry .getValue ();
462
- // 9) Download target up to length specified in bytes. verify against hash.
463
- String versionedTargetName ;
464
- if (targetData .getHashes ().getSha512 () != null ) {
465
- versionedTargetName = targetData .getHashes ().getSha512 () + "." + targetName ;
466
- } else {
467
- versionedTargetName = targetData .getHashes ().getSha256 () + "." + targetName ;
468
- }
471
+ downloadTarget (targetName , targetData );
472
+ }
473
+ }
469
474
470
- var targetBytes = targetFetcher .fetchResource (versionedTargetName , targetData .getLength ());
471
- if (targetBytes == null ) {
472
- throw new FileNotFoundException (targetName , targetFetcher .getSource ());
473
- }
474
- verifyHashes (entry .getKey (), targetBytes , targetData .getHashes ());
475
+ void downloadTarget (String targetName , TargetData targetData ) throws IOException {
476
+ // 9) Download target up to length specified in bytes. verify against hash.
477
+ String versionedTargetName ;
478
+ if (targetData .getHashes ().getSha512 () != null ) {
479
+ versionedTargetName = targetData .getHashes ().getSha512 () + "." + targetName ;
480
+ } else {
481
+ versionedTargetName = targetData .getHashes ().getSha256 () + "." + targetName ;
482
+ }
475
483
476
- // when persisting targets use the targetname without sha512 prefix
477
- // https://theupdateframework.github.io/specification/latest/index.html#fetch-target
478
- targetStore . writeTarget (targetName , targetBytes );
484
+ var targetBytes = targetFetcher . fetchResource ( versionedTargetName , targetData . getLength ());
485
+ if ( targetBytes == null ) {
486
+ throw new FileNotFoundException (targetName , targetFetcher . getSource () );
479
487
}
488
+ verifyHashes (targetName , targetBytes , targetData .getHashes ());
489
+
490
+ // when persisting targets use the targetname without sha512 prefix
491
+ // https://theupdateframework.github.io/specification/latest/index.html#fetch-target
492
+ targetStore .writeTarget (targetName , targetBytes );
480
493
}
481
494
482
495
@ VisibleForTesting
0 commit comments