|
| 1 | +/* |
| 2 | + * Copyright 2024 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.bundle; |
| 17 | + |
| 18 | +import com.google.common.base.Preconditions; |
| 19 | +import dev.sigstore.KeylessSignature; |
| 20 | +import dev.sigstore.rekor.client.RekorEntry; |
| 21 | +import java.io.Reader; |
| 22 | +import java.security.cert.CertPath; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import org.immutables.value.Value; |
| 26 | +import org.immutables.value.Value.Immutable; |
| 27 | +import org.immutables.value.Value.Lazy; |
| 28 | + |
| 29 | +/** |
| 30 | + * A representation of sigstore signing materials. See <a |
| 31 | + * href="https://github.com/sigstore/protobuf-specs">protobuf-specs</a> |
| 32 | + */ |
| 33 | +@Immutable |
| 34 | +public abstract class Bundle { |
| 35 | + |
| 36 | + public enum HashAlgorithm { |
| 37 | + SHA2_256 |
| 38 | + } |
| 39 | + |
| 40 | + static final String BUNDLE_V_0_1 = "application/vnd.dev.sigstore.bundle+json;version=0.1"; |
| 41 | + static final String BUNDLE_V_0_2 = "application/vnd.dev.sigstore.bundle+json;version=0.2"; |
| 42 | + static final String BUNDLE_V_0_3 = "application/vnd.dev.sigstore.bundle+json;version=0.3"; |
| 43 | + // media_type format switch: https://github.com/sigstore/protobuf-specs/pull/279 |
| 44 | + static final String BUNDLE_V_0_3_1 = "application/vnd.dev.sigstore.bundle.v0.3+json"; |
| 45 | + static final List<String> SUPPORTED_MEDIA_TYPES = |
| 46 | + List.of(BUNDLE_V_0_1, BUNDLE_V_0_2, BUNDLE_V_0_3, BUNDLE_V_0_3_1); |
| 47 | + |
| 48 | + /** The bundle version */ |
| 49 | + public abstract String getMediaType(); |
| 50 | + |
| 51 | + /** A signature represented as a signature and digest */ |
| 52 | + public abstract Optional<MessageSignature> getMessageSignature(); |
| 53 | + |
| 54 | + /** A DSSE envelope signature type that may contain an arbitrary payload */ |
| 55 | + public abstract Optional<DSSESignature> getDSSESignature(); |
| 56 | + |
| 57 | + @Value.Check |
| 58 | + protected void checkOnlyOneSignature() { |
| 59 | + Preconditions.checkState( |
| 60 | + (getDSSESignature().isEmpty() && getMessageSignature().isPresent()) |
| 61 | + || (getDSSESignature().isPresent() && getMessageSignature().isEmpty())); |
| 62 | + } |
| 63 | + |
| 64 | + @Value.Check |
| 65 | + protected void checkAtLeastOneTimestamp() { |
| 66 | + for (var entry : getEntries()) { |
| 67 | + if (entry.getVerification().getSignedEntryTimestamp() != null) { |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + if (getTimestamps().size() > 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + throw new IllegalStateException("No timestamp verification (set, timestamp) was provided"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * The partial certificate chain provided by fulcio for the public key and identity used to sign |
| 79 | + * the artifact, this should NOT contain the trusted root or any trusted intermediates. But users |
| 80 | + * of this object should understand that older signatures may include the full chain. |
| 81 | + */ |
| 82 | + public abstract CertPath getCertPath(); |
| 83 | + |
| 84 | + /** |
| 85 | + * The entry in the rekor transparency log (represented as a list for future compatibility, but |
| 86 | + * currently only allow for at most one entry. |
| 87 | + */ |
| 88 | + public abstract List<RekorEntry> getEntries(); |
| 89 | + |
| 90 | + /** A list of timestamps to verify the time of signing. Currently, allows rfc3161 timestamps. */ |
| 91 | + public abstract List<Timestamp> getTimestamps(); |
| 92 | + |
| 93 | + @Immutable |
| 94 | + interface MessageSignature { |
| 95 | + |
| 96 | + /** |
| 97 | + * An optional message digest, this should not be used to verify signature validity. A digest |
| 98 | + * should be provided or computed by the system. |
| 99 | + */ |
| 100 | + Optional<MessageDigest> getMessageDigest(); |
| 101 | + |
| 102 | + /** Signature over an artifact. */ |
| 103 | + byte[] getSignature(); |
| 104 | + } |
| 105 | + |
| 106 | + @Immutable |
| 107 | + interface MessageDigest { |
| 108 | + |
| 109 | + /** The algorithm used to compute the digest. */ |
| 110 | + HashAlgorithm getHashAlgorithm(); |
| 111 | + |
| 112 | + /** |
| 113 | + * The raw bytes of the digest computer using the hashing algorithm described by {@link |
| 114 | + * #getHashAlgorithm()} |
| 115 | + */ |
| 116 | + byte[] getDigest(); |
| 117 | + } |
| 118 | + |
| 119 | + @Immutable |
| 120 | + interface DSSESignature { |
| 121 | + |
| 122 | + /** An arbitrary payload that does not need to be parsed to be validated */ |
| 123 | + String getPayload(); |
| 124 | + |
| 125 | + /** Information on how to interpret the payload */ |
| 126 | + String getPayloadType(); |
| 127 | + |
| 128 | + /** DSSE specific signature */ |
| 129 | + byte[] getSignature(); |
| 130 | + } |
| 131 | + |
| 132 | + @Immutable |
| 133 | + interface Timestamp { |
| 134 | + |
| 135 | + /** Raw bytes of an rfc31616 timestamp */ |
| 136 | + byte[] getRfc3161Timestamp(); |
| 137 | + } |
| 138 | + |
| 139 | + public static Bundle from(Reader bundleJson) throws BundleParseException { |
| 140 | + return BundleReader.readBundle(bundleJson); |
| 141 | + } |
| 142 | + |
| 143 | + @Lazy |
| 144 | + public String toJson() { |
| 145 | + return BundleWriter.writeBundle(this); |
| 146 | + } |
| 147 | + |
| 148 | + /** Compat method to convert from keyless signature. Don't use, will be removed. */ |
| 149 | + public static Bundle from(KeylessSignature keylessSignature) { |
| 150 | + var sig = |
| 151 | + ImmutableMessageSignature.builder() |
| 152 | + .messageDigest( |
| 153 | + ImmutableMessageDigest.builder() |
| 154 | + .hashAlgorithm(HashAlgorithm.SHA2_256) |
| 155 | + .digest(keylessSignature.getDigest()) |
| 156 | + .build()) |
| 157 | + .signature(keylessSignature.getSignature()) |
| 158 | + .build(); |
| 159 | + |
| 160 | + return ImmutableBundle.builder() |
| 161 | + .mediaType(BUNDLE_V_0_3_1) |
| 162 | + .messageSignature(sig) |
| 163 | + .addEntries(keylessSignature.getEntry().get()) |
| 164 | + .certPath(keylessSignature.getCertPath()) |
| 165 | + .build(); |
| 166 | + } |
| 167 | + |
| 168 | + /** Compat method to convert to keyless signature. Don't use, will be removed. */ |
| 169 | + @Lazy |
| 170 | + public KeylessSignature toKeylessSignature() { |
| 171 | + if (getDSSESignature().isPresent()) { |
| 172 | + throw new IllegalStateException("This client can't process bundles with DSSE signatures."); |
| 173 | + } |
| 174 | + if (getTimestamps().size() >= 1) { |
| 175 | + throw new IllegalStateException("This client can't process bundles with RFC3161 Timestamps"); |
| 176 | + } |
| 177 | + var builder = |
| 178 | + KeylessSignature.builder() |
| 179 | + .certPath(getCertPath()) |
| 180 | + .signature(getMessageSignature().get().getSignature()) |
| 181 | + .entry(getEntries().get(0)); |
| 182 | + if (getMessageSignature().get().getMessageDigest().isPresent()) { |
| 183 | + builder.digest(getMessageSignature().get().getMessageDigest().get().getDigest()); |
| 184 | + } else { |
| 185 | + builder.digest(); |
| 186 | + } |
| 187 | + return builder.build(); |
| 188 | + } |
| 189 | +} |
0 commit comments