|
22 | 22 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
23 | 23 | import com.google.errorprone.annotations.CheckReturnValue; |
24 | 24 | import com.google.errorprone.annotations.concurrent.GuardedBy; |
| 25 | +import com.google.gson.JsonSyntaxException; |
25 | 26 | import com.google.protobuf.ByteString; |
26 | 27 | import dev.sigstore.bundle.Bundle; |
27 | 28 | import dev.sigstore.bundle.Bundle.MessageSignature; |
28 | 29 | import dev.sigstore.bundle.ImmutableBundle; |
| 30 | +import dev.sigstore.bundle.ImmutableDsseEnvelope; |
| 31 | +import dev.sigstore.bundle.ImmutableSignature; |
29 | 32 | import dev.sigstore.bundle.ImmutableTimestamp; |
| 33 | +import dev.sigstore.dsse.InTotoPayload; |
30 | 34 | import dev.sigstore.encryption.certificates.Certificates; |
31 | 35 | import dev.sigstore.encryption.signers.Signer; |
32 | 36 | import dev.sigstore.encryption.signers.Signers; |
|
42 | 46 | import dev.sigstore.oidc.client.OidcTokenMatcher; |
43 | 47 | import dev.sigstore.proto.ProtoMutators; |
44 | 48 | import dev.sigstore.proto.common.v1.X509Certificate; |
| 49 | +import dev.sigstore.proto.rekor.v2.DSSERequestV002; |
45 | 50 | import dev.sigstore.proto.rekor.v2.HashedRekordRequestV002; |
46 | 51 | import dev.sigstore.proto.rekor.v2.Signature; |
47 | 52 | import dev.sigstore.proto.rekor.v2.Verifier; |
|
65 | 70 | import dev.sigstore.trustroot.Service; |
66 | 71 | import dev.sigstore.trustroot.SigstoreConfigurationException; |
67 | 72 | import dev.sigstore.tuf.SigstoreTufClient; |
| 73 | +import io.intoto.EnvelopeOuterClass; |
68 | 74 | import java.io.IOException; |
69 | 75 | import java.nio.charset.StandardCharsets; |
70 | 76 | import java.nio.file.Path; |
@@ -102,6 +108,8 @@ public class KeylessSigner implements AutoCloseable { |
102 | 108 | */ |
103 | 109 | public static final Duration DEFAULT_MIN_SIGNING_CERTIFICATE_LIFETIME = Duration.ofMinutes(5); |
104 | 110 |
|
| 111 | + public static final String DEFAULT_INTOTO_PAYLOAD_TYPE = "https://in-toto.io/Statement/v1"; |
| 112 | + |
105 | 113 | private final FulcioClient fulcioClient; |
106 | 114 | private final FulcioVerifier fulcioVerifier; |
107 | 115 | private final RekorClient rekorClient; |
@@ -671,4 +679,163 @@ public Map<Path, Bundle> signFiles(List<Path> artifacts) throws KeylessSignerExc |
671 | 679 | public Bundle signFile(Path artifact) throws KeylessSignerException { |
672 | 680 | return signFiles(List.of(artifact)).get(artifact); |
673 | 681 | } |
| 682 | + |
| 683 | + public Bundle attest(String payload) throws KeylessSignerException { |
| 684 | + if (rekorV2Client != null) { // Using Rekor v2 and a TSA |
| 685 | + Preconditions.checkNotNull( |
| 686 | + timestampClient, "Timestamp client must be configured for Rekor v2"); |
| 687 | + Preconditions.checkNotNull( |
| 688 | + timestampVerifier, "Timestamp verifier must be configured for Rekor v2"); |
| 689 | + } else { |
| 690 | + throw new IllegalStateException("No rekor v2 client was configured."); |
| 691 | + } |
| 692 | + |
| 693 | + if (payload == null || payload.isEmpty()) { |
| 694 | + throw new IllegalArgumentException("Payload must be non-empty"); |
| 695 | + } |
| 696 | + |
| 697 | + InTotoPayload inTotoPayload; |
| 698 | + try { |
| 699 | + inTotoPayload = InTotoPayload.from(payload); |
| 700 | + } catch (JsonSyntaxException jse) { |
| 701 | + throw new IllegalArgumentException("Payload is not a valid in-toto statement"); |
| 702 | + } |
| 703 | + |
| 704 | + if (!inTotoPayload.getType().equals(DEFAULT_INTOTO_PAYLOAD_TYPE)) { |
| 705 | + throw new IllegalArgumentException( |
| 706 | + "Payload must be of type \"" |
| 707 | + + DEFAULT_INTOTO_PAYLOAD_TYPE |
| 708 | + + "\" but was \"" |
| 709 | + + inTotoPayload.getType() |
| 710 | + + "\""); |
| 711 | + } |
| 712 | + |
| 713 | + if (inTotoPayload.getSubject() == null || inTotoPayload.getSubject().isEmpty()) { |
| 714 | + throw new IllegalArgumentException("Payload must contain at least one subject"); |
| 715 | + } |
| 716 | + |
| 717 | + for (var subject : inTotoPayload.getSubject()) { |
| 718 | + if (subject.getName() != null && !subject.getName().isEmpty()) { |
| 719 | + continue; |
| 720 | + } |
| 721 | + throw new IllegalArgumentException("Payload must contain at least one non-empty subject"); |
| 722 | + } |
| 723 | + |
| 724 | + // Technically speaking, it is unlikely the certificate will expire between signing artifacts |
| 725 | + // However, files might be large, and it might take time to talk to Rekor |
| 726 | + // so we check the certificate expiration here. |
| 727 | + try { |
| 728 | + renewSigningCertificate(); |
| 729 | + } catch (FulcioVerificationException |
| 730 | + | UnsupportedAlgorithmException |
| 731 | + | OidcException |
| 732 | + | IOException |
| 733 | + | InterruptedException |
| 734 | + | InvalidKeyException |
| 735 | + | NoSuchAlgorithmException |
| 736 | + | SignatureException |
| 737 | + | CertificateException ex) { |
| 738 | + throw new KeylessSignerException("Failed to obtain signing certificate", ex); |
| 739 | + } |
| 740 | + |
| 741 | + CertPath signingCert; |
| 742 | + byte[] encodedCert; |
| 743 | + lock.readLock().lock(); |
| 744 | + try { |
| 745 | + signingCert = this.signingCert; |
| 746 | + encodedCert = this.encodedCert; |
| 747 | + if (signingCert == null) { |
| 748 | + throw new IllegalStateException("Signing certificate is null"); |
| 749 | + } |
| 750 | + } finally { |
| 751 | + lock.readLock().unlock(); |
| 752 | + } |
| 753 | + |
| 754 | + var bundleBuilder = ImmutableBundle.builder().certPath(signingCert); |
| 755 | + |
| 756 | + var dsse = |
| 757 | + ImmutableDsseEnvelope.builder() |
| 758 | + .payload(payload.getBytes(StandardCharsets.UTF_8)) |
| 759 | + .payloadType("application/vnd.in-toto+json") |
| 760 | + .build(); |
| 761 | + |
| 762 | + var pae = dsse.getPAE(); |
| 763 | + |
| 764 | + Bundle.DsseEnvelope dsseSigned; |
| 765 | + try { |
| 766 | + var sig = signer.sign(pae); |
| 767 | + dsseSigned = |
| 768 | + ImmutableDsseEnvelope.builder() |
| 769 | + .from(dsse) |
| 770 | + .addSignatures(ImmutableSignature.builder().sig(sig).build()) |
| 771 | + .build(); |
| 772 | + } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException ex) { |
| 773 | + throw new KeylessSignerException("Failed to sign artifact", ex); |
| 774 | + } |
| 775 | + |
| 776 | + var verifier = |
| 777 | + Verifier.newBuilder() |
| 778 | + .setX509Certificate( |
| 779 | + X509Certificate.newBuilder().setRawBytes(ByteString.copyFrom(encodedCert)).build()) |
| 780 | + .setKeyDetails(ProtoMutators.toPublicKeyDetails(signingAlgorithm)) |
| 781 | + .build(); |
| 782 | + |
| 783 | + var dsseRequest = |
| 784 | + DSSERequestV002.newBuilder() |
| 785 | + .setEnvelope( |
| 786 | + EnvelopeOuterClass.Envelope.newBuilder() |
| 787 | + .setPayload(ByteString.copyFrom(dsseSigned.getPayload())) |
| 788 | + .setPayloadType(dsseSigned.getPayloadType()) |
| 789 | + .addSignatures( |
| 790 | + EnvelopeOuterClass.Signature.newBuilder() |
| 791 | + .setSig(ByteString.copyFrom(dsseSigned.getSignature()))) |
| 792 | + .build()) |
| 793 | + .addVerifiers(verifier) |
| 794 | + .build(); |
| 795 | + |
| 796 | + var signatureDigest = Hashing.sha256().hashBytes(dsseSigned.getSignature()).asBytes(); |
| 797 | + |
| 798 | + var tsReq = |
| 799 | + ImmutableTimestampRequest.builder() |
| 800 | + .hashAlgorithm(dev.sigstore.timestamp.client.HashAlgorithm.SHA256) |
| 801 | + .hash(signatureDigest) |
| 802 | + .build(); |
| 803 | + |
| 804 | + TimestampResponse tsResp; |
| 805 | + try { |
| 806 | + tsResp = timestampClient.timestamp(tsReq); |
| 807 | + } catch (TimestampException ex) { |
| 808 | + throw new KeylessSignerException("Failed to generate timestamp", ex); |
| 809 | + } |
| 810 | + |
| 811 | + try { |
| 812 | + timestampVerifier.verify(tsResp, dsseSigned.getSignature()); |
| 813 | + } catch (TimestampVerificationException ex) { |
| 814 | + throw new KeylessSignerException("Returned timestamp was invalid", ex); |
| 815 | + } |
| 816 | + |
| 817 | + Bundle.Timestamp timestamp = |
| 818 | + ImmutableTimestamp.builder().rfc3161Timestamp(tsResp.getEncoded()).build(); |
| 819 | + |
| 820 | + bundleBuilder.addTimestamps(timestamp); |
| 821 | + |
| 822 | + RekorEntry entry; |
| 823 | + try { |
| 824 | + entry = rekorV2Client.putEntry(dsseRequest); |
| 825 | + } catch (IOException | RekorParseException ex) { |
| 826 | + throw new KeylessSignerException("Failed to put entry in rekor", ex); |
| 827 | + } |
| 828 | + |
| 829 | + try { |
| 830 | + rekorVerifier.verifyEntry(entry); |
| 831 | + } catch (RekorVerificationException ex) { |
| 832 | + throw new KeylessSignerException("Failed to validate rekor entry after signing", ex); |
| 833 | + } |
| 834 | + |
| 835 | + bundleBuilder.dsseEnvelope(dsseSigned); |
| 836 | + |
| 837 | + bundleBuilder.addEntries(entry); |
| 838 | + |
| 839 | + return bundleBuilder.build(); |
| 840 | + } |
674 | 841 | } |
0 commit comments