|
| 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.RekorParseException; |
| 27 | +import dev.sigstore.rekor.client.RekorVerificationException; |
| 28 | +import dev.sigstore.trustroot.SigstoreTrustedRoot; |
| 29 | +import dev.sigstore.trustroot.TransparencyLog; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.security.InvalidKeyException; |
| 32 | +import java.security.NoSuchAlgorithmException; |
| 33 | +import java.security.SignatureException; |
| 34 | +import java.security.spec.InvalidKeySpecException; |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Base64; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +/* Verifier for rekor v2 entries. */ |
| 42 | +public class RekorV2Verifier { |
| 43 | + private final List<TransparencyLog> tlogs; |
| 44 | + |
| 45 | + public static RekorV2Verifier newRekorV2Verifier(SigstoreTrustedRoot trustRoot) { |
| 46 | + return newRekorV2Verifier(trustRoot.getTLogs()); |
| 47 | + } |
| 48 | + |
| 49 | + public static RekorV2Verifier newRekorV2Verifier(List<TransparencyLog> tlogs) { |
| 50 | + return new RekorV2Verifier(tlogs); |
| 51 | + } |
| 52 | + |
| 53 | + private RekorV2Verifier(List<TransparencyLog> tlogs) { |
| 54 | + this.tlogs = tlogs; |
| 55 | + } |
| 56 | + |
| 57 | + public void verifyEntry(TransparencyLogEntry entry, Instant timestamp) |
| 58 | + throws RekorVerificationException { |
| 59 | + if (entry.getInclusionProof() == null) { |
| 60 | + throw new RekorVerificationException("No inclusion proof in entry."); |
| 61 | + } |
| 62 | + |
| 63 | + var tlog = |
| 64 | + TransparencyLog.find(tlogs, entry.getLogId().getKeyId().toByteArray(), timestamp) |
| 65 | + .orElseThrow( |
| 66 | + () -> |
| 67 | + new RekorVerificationException( |
| 68 | + "Log entry (logid, timestamp) does not match any provided transparency logs.")); |
| 69 | + |
| 70 | + // verify inclusion proof |
| 71 | + verifyInclusionProof(entry); |
| 72 | + verifyCheckpoint(entry, tlog); |
| 73 | + } |
| 74 | + |
| 75 | + /** Verify that a Rekor Entry is in the log by checking inclusion proof. */ |
| 76 | + private void verifyInclusionProof(TransparencyLogEntry entry) throws RekorVerificationException { |
| 77 | + var inclusionProof = entry.getInclusionProof(); |
| 78 | + |
| 79 | + var leafHash = |
| 80 | + Hashing.sha256() |
| 81 | + .hashBytes( |
| 82 | + InclusionProofVerifier.combineBytes( |
| 83 | + new byte[] {0x00}, entry.getCanonicalizedBody().toByteArray())) |
| 84 | + .asBytes(); |
| 85 | + |
| 86 | + List<byte[]> hashes = new ArrayList<>(); |
| 87 | + for (ByteString hash : inclusionProof.getHashesList()) { |
| 88 | + hashes.add(hash.toByteArray()); |
| 89 | + } |
| 90 | + |
| 91 | + byte[] expectedRootHash = inclusionProof.getRootHash().toByteArray(); |
| 92 | + |
| 93 | + try { |
| 94 | + InclusionProofVerifier.verify( |
| 95 | + leafHash, |
| 96 | + inclusionProof.getLogIndex(), |
| 97 | + inclusionProof.getTreeSize(), |
| 98 | + hashes, |
| 99 | + expectedRootHash); |
| 100 | + } catch (InclusionProofVerificationException e) { |
| 101 | + throw new RekorVerificationException("Inclusion proof verification failed", e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void verifyCheckpoint(TransparencyLogEntry entry, TransparencyLog tlog) |
| 106 | + throws RekorVerificationException { |
| 107 | + var checkpoint = entry.getInclusionProof().getCheckpoint(); |
| 108 | + Checkpoint parsedCheckpoint; |
| 109 | + try { |
| 110 | + parsedCheckpoint = Checkpoints.from(checkpoint.getEnvelope()); |
| 111 | + } catch (RekorParseException ex) { |
| 112 | + throw new RekorVerificationException("Could not parse checkpoint from envelope", ex); |
| 113 | + } |
| 114 | + |
| 115 | + byte[] inclusionRootHash = entry.getInclusionProof().getRootHash().toByteArray(); |
| 116 | + byte[] checkpointRootHash = Base64.getDecoder().decode(parsedCheckpoint.getBase64Hash()); |
| 117 | + |
| 118 | + if (!Arrays.equals(inclusionRootHash, checkpointRootHash)) { |
| 119 | + throw new RekorVerificationException( |
| 120 | + "Checkpoint root hash does not match root hash provided in inclusion proof"); |
| 121 | + } |
| 122 | + var keyHash = tlog.getLogId().getKeyId(); |
| 123 | + // checkpoint 0 is always the log, not any of the cross signing verifiers/monitors |
| 124 | + var sig = parsedCheckpoint.getSignatures().get(0); |
| 125 | + for (int i = 0; i < 4; i++) { |
| 126 | + if (sig.getKeyHint()[i] != keyHash[i]) { |
| 127 | + throw new RekorVerificationException( |
| 128 | + "Checkpoint key hint did not match provided log public key"); |
| 129 | + } |
| 130 | + } |
| 131 | + var signedData = parsedCheckpoint.getSignedData(); |
| 132 | + |
| 133 | + try { |
| 134 | + if (!Verifiers.newVerifier(tlog.getPublicKey().toJavaPublicKey()) |
| 135 | + .verify(signedData.getBytes(StandardCharsets.UTF_8), sig.getSignature())) { |
| 136 | + throw new RekorVerificationException("Checkpoint signature was invalid"); |
| 137 | + } |
| 138 | + } catch (NoSuchAlgorithmException |
| 139 | + | InvalidKeySpecException |
| 140 | + | SignatureException |
| 141 | + | InvalidKeyException ex) { |
| 142 | + throw new RekorVerificationException("Could not verify checkpoint signature", ex); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments