Skip to content

Commit ae74a3c

Browse files
authored
Merge pull request #1021 from sigstore/rekor-entry-proto
Move TLogEntry-to-RekorEntry converter to ProtoMutators
2 parents a5794e8 + 8fb2eae commit ae74a3c

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

sigstore-java/src/main/java/dev/sigstore/proto/ProtoMutators.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@
2121
import dev.sigstore.encryption.certificates.Certificates;
2222
import dev.sigstore.proto.common.v1.HashAlgorithm;
2323
import dev.sigstore.proto.common.v1.X509Certificate;
24+
import dev.sigstore.proto.rekor.v1.InclusionProof;
25+
import dev.sigstore.proto.rekor.v1.TransparencyLogEntry;
26+
import dev.sigstore.rekor.client.ImmutableInclusionProof;
27+
import dev.sigstore.rekor.client.ImmutableRekorEntry;
28+
import dev.sigstore.rekor.client.ImmutableVerification;
29+
import dev.sigstore.rekor.client.RekorEntry;
30+
import dev.sigstore.rekor.client.RekorParseException;
2431
import java.security.cert.CertPath;
2532
import java.security.cert.Certificate;
2633
import java.security.cert.CertificateEncodingException;
2734
import java.security.cert.CertificateException;
2835
import java.security.cert.CertificateFactory;
2936
import java.time.Instant;
3037
import java.util.ArrayList;
38+
import java.util.Base64;
3139
import java.util.List;
40+
import org.bouncycastle.util.encoders.Hex;
3241

3342
public class ProtoMutators {
3443

@@ -59,4 +68,41 @@ public static HashAlgorithm from(Bundle.HashAlgorithm algorithm) {
5968
}
6069
throw new IllegalStateException("Unknown hash algorithm: " + algorithm);
6170
}
71+
72+
public static RekorEntry toRekorEntry(TransparencyLogEntry tle) throws RekorParseException {
73+
ImmutableRekorEntry.Builder builder = ImmutableRekorEntry.builder();
74+
75+
builder.logIndex(tle.getLogIndex());
76+
builder.logID(Hex.toHexString(tle.getLogId().getKeyId().toByteArray()));
77+
builder.integratedTime(tle.getIntegratedTime());
78+
79+
// The body of a RekorEntry is Base64 encoded
80+
builder.body(Base64.getEncoder().encodeToString(tle.getCanonicalizedBody().toByteArray()));
81+
82+
ImmutableVerification.Builder verificationBuilder = ImmutableVerification.builder();
83+
84+
// Rekor v2 entries won't have an InclusionPromise/SET
85+
if (tle.hasInclusionPromise()
86+
&& !tle.getInclusionPromise().getSignedEntryTimestamp().isEmpty()) {
87+
verificationBuilder.signedEntryTimestamp(
88+
Base64.getEncoder()
89+
.encodeToString(tle.getInclusionPromise().getSignedEntryTimestamp().toByteArray()));
90+
}
91+
92+
if (tle.hasInclusionProof()) {
93+
InclusionProof ipProto = tle.getInclusionProof();
94+
ImmutableInclusionProof.Builder ipBuilder = ImmutableInclusionProof.builder();
95+
ipBuilder.logIndex(ipProto.getLogIndex());
96+
ipBuilder.rootHash(Hex.toHexString(ipProto.getRootHash().toByteArray()));
97+
ipBuilder.treeSize(ipProto.getTreeSize());
98+
ipBuilder.checkpoint(ipProto.getCheckpoint().getEnvelope());
99+
ipProto
100+
.getHashesList()
101+
.forEach(hash -> ipBuilder.addHashes(Hex.toHexString(hash.toByteArray())));
102+
verificationBuilder.inclusionProof(ipBuilder.build());
103+
}
104+
builder.verification(verificationBuilder.build());
105+
106+
return builder.build();
107+
}
62108
}

sigstore-java/src/main/java/dev/sigstore/rekor/client/RekorEntry.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
import com.google.protobuf.InvalidProtocolBufferException;
2222
import dev.sigstore.json.ProtoJson;
23+
import dev.sigstore.proto.ProtoMutators;
2324
import dev.sigstore.proto.rekor.v1.TransparencyLogEntry;
2425
import java.io.IOException;
2526
import java.time.Instant;
2627
import java.util.*;
2728
import javax.annotation.Nullable;
28-
import org.bouncycastle.util.encoders.Hex;
2929
import org.erdtman.jcs.JsonCanonicalizer;
3030
import org.immutables.gson.Gson;
3131
import org.immutables.value.Value;
@@ -173,49 +173,9 @@ static RekorEntry fromTLogEntryJson(String json) throws RekorParseException {
173173
try {
174174
TransparencyLogEntry.Builder builder = TransparencyLogEntry.newBuilder();
175175
ProtoJson.parser().ignoringUnknownFields().merge(json, builder);
176-
return RekorEntry.fromTLogEntry(builder.build());
176+
return ProtoMutators.toRekorEntry(builder.build());
177177
} catch (InvalidProtocolBufferException e) {
178178
throw new RekorParseException("Failed to parse Rekor response JSON", e);
179179
}
180180
}
181-
182-
/** Returns a RekorEntry from a TransparencyLogEntry */
183-
public static RekorEntry fromTLogEntry(TransparencyLogEntry tle) throws RekorParseException {
184-
ImmutableRekorEntry.Builder builder = ImmutableRekorEntry.builder();
185-
186-
builder.logIndex(tle.getLogIndex());
187-
builder.logID(Hex.toHexString(tle.getLogId().getKeyId().toByteArray()));
188-
builder.integratedTime(tle.getIntegratedTime());
189-
190-
// The body of a RekorEntry is Base64 encoded
191-
builder.body(
192-
java.util.Base64.getEncoder().encodeToString(tle.getCanonicalizedBody().toByteArray()));
193-
194-
ImmutableVerification.Builder verificationBuilder = ImmutableVerification.builder();
195-
196-
// Rekor v2 entries won't have an InclusionPromise/SET
197-
if (tle.hasInclusionPromise()
198-
&& !tle.getInclusionPromise().getSignedEntryTimestamp().isEmpty()) {
199-
verificationBuilder.signedEntryTimestamp(
200-
java.util.Base64.getEncoder()
201-
.encodeToString(tle.getInclusionPromise().getSignedEntryTimestamp().toByteArray()));
202-
}
203-
204-
if (tle.hasInclusionProof()) {
205-
dev.sigstore.proto.rekor.v1.InclusionProof ipProto = tle.getInclusionProof();
206-
ImmutableInclusionProof.Builder ipBuilder = ImmutableInclusionProof.builder();
207-
ipBuilder.logIndex(ipProto.getLogIndex());
208-
ipBuilder.rootHash(
209-
org.bouncycastle.util.encoders.Hex.toHexString(ipProto.getRootHash().toByteArray()));
210-
ipBuilder.treeSize(ipProto.getTreeSize());
211-
ipBuilder.checkpoint(ipProto.getCheckpoint().getEnvelope());
212-
ipProto
213-
.getHashesList()
214-
.forEach(hash -> ipBuilder.addHashes(Hex.toHexString(hash.toByteArray())));
215-
verificationBuilder.inclusionProof(ipBuilder.build());
216-
}
217-
builder.verification(verificationBuilder.build());
218-
219-
return builder.build();
220-
}
221181
}

sigstore-java/src/test/java/dev/sigstore/rekor/client/RekorEntryTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.protobuf.ByteString;
1919
import com.google.protobuf.util.JsonFormat;
20+
import dev.sigstore.proto.ProtoMutators;
2021
import dev.sigstore.proto.common.v1.LogId;
2122
import dev.sigstore.proto.rekor.v1.Checkpoint;
2223
import dev.sigstore.proto.rekor.v1.InclusionPromise;
@@ -60,7 +61,7 @@ public void fromTLogEntry_full() throws Exception {
6061
.addHashes(ByteString.fromHex("02")))
6162
.build();
6263

63-
var entry = RekorEntry.fromTLogEntry(tle);
64+
var entry = ProtoMutators.toRekorEntry(tle);
6465

6566
Assertions.assertEquals(123, entry.getLogIndex());
6667
Assertions.assertEquals("abcdef", entry.getLogID());
@@ -94,7 +95,7 @@ public void fromTLogEntry_minimal() throws Exception {
9495
.setCanonicalizedBody(MOCK_BODY_BYTESTRING)
9596
.build();
9697

97-
var entry = RekorEntry.fromTLogEntry(tle);
98+
var entry = ProtoMutators.toRekorEntry(tle);
9899
Assertions.assertEquals(123, entry.getLogIndex());
99100
Assertions.assertEquals("abcdef", entry.getLogID());
100101
Assertions.assertEquals(456, entry.getIntegratedTime());

sigstore-java/src/test/java/dev/sigstore/rekor/client/RekorVerifierTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.protobuf.InvalidProtocolBufferException;
2222
import dev.sigstore.json.GsonSupplier;
2323
import dev.sigstore.json.ProtoJson;
24+
import dev.sigstore.proto.ProtoMutators;
2425
import dev.sigstore.proto.rekor.v1.TransparencyLogEntry;
2526
import dev.sigstore.trustroot.SigstoreTrustedRoot;
2627
import java.io.IOException;
@@ -280,7 +281,7 @@ private RekorEntry getV2RekorEntry(String json)
280281
throws InvalidProtocolBufferException, RekorParseException {
281282
var transparencyLogEntryBuilder = TransparencyLogEntry.newBuilder();
282283
ProtoJson.parser().merge(json, transparencyLogEntryBuilder);
283-
return RekorEntry.fromTLogEntry(transparencyLogEntryBuilder.build());
284+
return ProtoMutators.toRekorEntry(transparencyLogEntryBuilder.build());
284285
}
285286

286287
private RekorEntry getV1RekorEntry(String json) throws Exception {

0 commit comments

Comments
 (0)