|
| 1 | +/* |
| 2 | + * Copyright 2025 The Sigstore Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package dev.sigstore.rekor.v2.client; |
| 17 | + |
| 18 | +import com.google.common.hash.Hashing; |
| 19 | +import com.google.protobuf.ByteString; |
| 20 | +import dev.sigstore.encryption.signers.Verifiers; |
| 21 | +import dev.sigstore.merkle.InclusionProofVerificationException; |
| 22 | +import dev.sigstore.merkle.InclusionProofVerifier; |
| 23 | +import dev.sigstore.proto.rekor.v1.TransparencyLogEntry; |
| 24 | +import dev.sigstore.rekor.client.Checkpoints; |
| 25 | +import dev.sigstore.rekor.client.RekorEntry.Checkpoint; |
| 26 | +import dev.sigstore.rekor.client.RekorEntry.CheckpointSignature; |
| 27 | +import dev.sigstore.rekor.client.RekorParseException; |
| 28 | +import dev.sigstore.rekor.client.RekorVerificationException; |
| 29 | +import dev.sigstore.trustroot.SigstoreTrustedRoot; |
| 30 | +import dev.sigstore.trustroot.TransparencyLog; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.security.InvalidKeyException; |
| 33 | +import java.security.NoSuchAlgorithmException; |
| 34 | +import java.security.SignatureException; |
| 35 | +import java.security.spec.InvalidKeySpecException; |
| 36 | +import java.time.Instant; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Base64; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Optional; |
| 42 | + |
| 43 | +/* Verifier for rekor v2 entries. */ |
| 44 | +public class RekorV2Verifier { |
| 45 | + private final List<TransparencyLog> tlogs; |
| 46 | + |
| 47 | + public static RekorV2Verifier newRekorV2Verifier(SigstoreTrustedRoot trustRoot) { |
| 48 | + return newRekorV2Verifier(trustRoot.getTLogs()); |
| 49 | + } |
| 50 | + |
| 51 | + public static RekorV2Verifier newRekorV2Verifier(List<TransparencyLog> tlogs) { |
| 52 | + return new RekorV2Verifier(tlogs); |
| 53 | + } |
| 54 | + |
| 55 | + private RekorV2Verifier(List<TransparencyLog> tlogs) { |
| 56 | + this.tlogs = tlogs; |
| 57 | + } |
| 58 | + |
| 59 | + public void verifyEntry(TransparencyLogEntry entry, Instant timestamp) |
| 60 | + throws RekorVerificationException { |
| 61 | + if (entry.getInclusionProof() == null) { |
| 62 | + throw new RekorVerificationException("No inclusion proof in entry."); |
| 63 | + } |
| 64 | + |
| 65 | + var tlog = |
| 66 | + TransparencyLog.find(tlogs, entry.getLogId().getKeyId().toByteArray(), timestamp) |
| 67 | + .orElseThrow( |
| 68 | + () -> |
| 69 | + new RekorVerificationException( |
| 70 | + "Log entry (logid, timestamp) does not match any provided transparency logs.")); |
| 71 | + |
| 72 | + // verify inclusion proof |
| 73 | + verifyInclusionProof(entry); |
| 74 | + verifyCheckpoint(entry, tlog); |
| 75 | + } |
| 76 | + |
| 77 | + /** Verify that a Rekor Entry is in the log by checking inclusion proof. */ |
| 78 | + private void verifyInclusionProof(TransparencyLogEntry entry) throws RekorVerificationException { |
| 79 | + var inclusionProof = entry.getInclusionProof(); |
| 80 | + |
| 81 | + var leafHash = |
| 82 | + Hashing.sha256() |
| 83 | + .newHasher() |
| 84 | + .putByte((byte) 0x00) |
| 85 | + .putBytes(entry.getCanonicalizedBody().toByteArray()) |
| 86 | + .hash() |
| 87 | + .asBytes(); |
| 88 | + |
| 89 | + List<byte[]> hashes = new ArrayList<>(); |
| 90 | + for (ByteString hash : inclusionProof.getHashesList()) { |
| 91 | + hashes.add(hash.toByteArray()); |
| 92 | + } |
| 93 | + |
| 94 | + byte[] expectedRootHash = inclusionProof.getRootHash().toByteArray(); |
| 95 | + |
| 96 | + try { |
| 97 | + InclusionProofVerifier.verify( |
| 98 | + leafHash, |
| 99 | + inclusionProof.getLogIndex(), |
| 100 | + inclusionProof.getTreeSize(), |
| 101 | + hashes, |
| 102 | + expectedRootHash); |
| 103 | + } catch (InclusionProofVerificationException e) { |
| 104 | + throw new RekorVerificationException("Inclusion proof verification failed", e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void verifyCheckpoint(TransparencyLogEntry entry, TransparencyLog tlog) |
| 109 | + throws RekorVerificationException { |
| 110 | + var checkpoint = entry.getInclusionProof().getCheckpoint(); |
| 111 | + Checkpoint parsedCheckpoint; |
| 112 | + try { |
| 113 | + parsedCheckpoint = Checkpoints.from(checkpoint.getEnvelope()); |
| 114 | + } catch (RekorParseException ex) { |
| 115 | + throw new RekorVerificationException("Could not parse checkpoint from envelope", ex); |
| 116 | + } |
| 117 | + |
| 118 | + final int MAX_CHECKPOINT_SIGNATURES = 20; |
| 119 | + if (parsedCheckpoint.getSignatures().size() > MAX_CHECKPOINT_SIGNATURES) { |
| 120 | + throw new RekorVerificationException( |
| 121 | + "Checkpoint contains an excessive number of signatures (" |
| 122 | + + parsedCheckpoint.getSignatures().size() |
| 123 | + + "), exceeding the maximum allowed of " |
| 124 | + + MAX_CHECKPOINT_SIGNATURES); |
| 125 | + } |
| 126 | + |
| 127 | + byte[] inclusionRootHash = entry.getInclusionProof().getRootHash().toByteArray(); |
| 128 | + byte[] checkpointRootHash = Base64.getDecoder().decode(parsedCheckpoint.getBase64Hash()); |
| 129 | + |
| 130 | + if (!Arrays.equals(inclusionRootHash, checkpointRootHash)) { |
| 131 | + throw new RekorVerificationException( |
| 132 | + "Checkpoint root hash does not match root hash provided in inclusion proof"); |
| 133 | + } |
| 134 | + |
| 135 | + Optional<CheckpointSignature> matchingSig = |
| 136 | + parsedCheckpoint.getSignatures().stream() |
| 137 | + .filter(sig -> sig.getIdentity().equals(tlog.getBaseUrl().getHost())) |
| 138 | + .findFirst(); |
| 139 | + |
| 140 | + if (!matchingSig.isPresent()) { |
| 141 | + throw new RekorVerificationException( |
| 142 | + "No matching checkpoint signature found for transparency log: " |
| 143 | + + tlog.getBaseUrl().getHost()); |
| 144 | + } |
| 145 | + |
| 146 | + var signedData = parsedCheckpoint.getSignedData(); |
| 147 | + |
| 148 | + try { |
| 149 | + if (!Verifiers.newVerifier(tlog.getPublicKey().toJavaPublicKey()) |
| 150 | + .verify(signedData.getBytes(StandardCharsets.UTF_8), matchingSig.get().getSignature())) { |
| 151 | + throw new RekorVerificationException("Checkpoint signature was invalid"); |
| 152 | + } |
| 153 | + } catch (NoSuchAlgorithmException |
| 154 | + | InvalidKeySpecException |
| 155 | + | SignatureException |
| 156 | + | InvalidKeyException ex) { |
| 157 | + throw new RekorVerificationException("Could not verify checkpoint signature", ex); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments