Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates before applying tuf to fulcio client #477

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.sigstore.encryption.certificates;

import com.google.api.client.util.PemReader;
import com.google.common.collect.ImmutableList;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -135,4 +136,16 @@ public static CertPath toCertPath(Certificate certificate) throws CertificateExc
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return cf.generateCertPath(Collections.singletonList(certificate));
}

/** Appends an X509Certificate to a {@link CertPath} as a leaf. */
public static CertPath appendCertPath(CertPath root, Certificate certificate)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is so we can recreate cert paths from provided certificates

throws CertificateException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
List<Certificate> certs =
ImmutableList.<Certificate>builder()
.add(certificate)
.addAll(root.getCertificates())
.build();
return cf.generateCertPath(certs);
}
}
19 changes: 19 additions & 0 deletions sigstore-java/src/main/java/dev/sigstore/http/GrpcChannels.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,23 @@ public static ManagedChannel newManagedChannel(URI serverUrl, HttpParams httpPar
}
return channelBuilder.build();
}

/**
* Create a new managed channel, this may be reused across multiple requests to a host, and must
* be closed when finished.
*
* @param serverUrl the host to connect to
* @param httpParams the http configuration
* @return a reusable grpc channel
*/
public static ManagedChannel newManagedChannel(String serverUrl, HttpParams httpParams) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we extract the "authority" from a uri (Strip the "http...") it returns a string, so we're going to greate managed channels with a string.

var channelBuilder =
ManagedChannelBuilder.forTarget(serverUrl)
.userAgent(httpParams.getUserAgent())
.keepAliveTimeout(httpParams.getTimeout(), TimeUnit.SECONDS);
if (httpParams.getAllowInsecureConnections()) {
channelBuilder.usePlaintext();
}
return channelBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.sigstore.trustroot;

import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.immutables.value.Value;
Expand All @@ -26,7 +27,7 @@
@Value.Style(
depluralize = true,
depluralizeDictionary = {"certificateAuthority:certificateAuthorities"})
public abstract class CertificateAuthorities {
public abstract class CertificateAuthorities implements Iterable<CertificateAuthority> {

public abstract List<CertificateAuthority> getCertificateAuthorities();

Expand Down Expand Up @@ -74,4 +75,9 @@ public CertificateAuthority current() {
}
return current.get(0);
}

@Override
public Iterator<CertificateAuthority> iterator() {
return getCertificateAuthorities().iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@

import dev.sigstore.proto.ProtoMutators;
import java.net.URI;
import java.security.InvalidAlgorithmParameterException;
import java.security.cert.CertPath;
import java.security.cert.CertificateException;
import java.security.cert.PKIXParameters;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.Collections;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Lazy;

@Immutable
public abstract class CertificateAuthority {
Expand All @@ -36,6 +42,19 @@ public boolean isCurrent() {
return getValidFor().contains(Instant.now());
}

@Lazy
public TrustAnchor asTrustAnchor()
throws CertificateException, InvalidAlgorithmParameterException {
var certs = getCertPath().getCertificates();
X509Certificate fulcioRootObj = (X509Certificate) certs.get(certs.size() - 1);
TrustAnchor fulcioRootTrustAnchor = new TrustAnchor(fulcioRootObj, null);

// validate the certificate can be a trust anchor
new PKIXParameters(Collections.singleton(fulcioRootTrustAnchor));

return fulcioRootTrustAnchor;
}

public static CertificateAuthority from(
dev.sigstore.proto.trustroot.v1.CertificateAuthority proto) throws CertificateException {
return ImmutableCertificateAuthority.builder()
Expand Down
28 changes: 15 additions & 13 deletions sigstore-java/src/main/java/dev/sigstore/trustroot/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Lazy;

@Immutable
public interface PublicKey {
byte[] getRawBytes();
public abstract class PublicKey {
public abstract byte[] getRawBytes();

String getKeyDetails();
public abstract String getKeyDetails();

ValidFor getValidFor();
public abstract ValidFor getValidFor();

static PublicKey from(dev.sigstore.proto.common.v1.PublicKey proto) {
@Lazy
public java.security.PublicKey toJavaPublicKey()
throws InvalidKeySpecException, NoSuchAlgorithmException {
if (!getKeyDetails().equals("PKIX_ECDSA_P256_SHA_256")) {
throw new InvalidKeySpecException("Unsupported key algorithm: " + getKeyDetails());
}
return Keys.parsePkixPublicKey(getRawBytes(), "EC");
}

public static PublicKey from(dev.sigstore.proto.common.v1.PublicKey proto) {
return ImmutablePublicKey.builder()
.rawBytes(proto.getRawBytes().toByteArray())
.keyDetails(proto.getKeyDetails().name())
.validFor(ValidFor.from(proto.getValidFor()))
.build();
}

static java.security.PublicKey toJavaPublicKey(PublicKey publicKey)
throws InvalidKeySpecException, NoSuchAlgorithmException {
if (!publicKey.getKeyDetails().equals("PKIX_ECDSA_P256_SHA_256")) {
throw new InvalidKeySpecException("Unsupported key algorithm: " + publicKey.getKeyDetails());
}
return Keys.parsePkixPublicKey(publicKey.getRawBytes(), "EC");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.immutables.value.Value.Immutable;

@Immutable
interface Subject {
public interface Subject {
String getOrganization();

String getCommonName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.time.Instant;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -26,7 +27,7 @@

@Immutable
@Value.Style(depluralize = true)
public abstract class TransparencyLogs {
public abstract class TransparencyLogs implements Iterable<TransparencyLog> {

public abstract List<TransparencyLog> getTransparencyLogs();

Expand Down Expand Up @@ -64,4 +65,9 @@ public Optional<TransparencyLog> find(byte[] logId, Instant time) {
tl.getPublicKey().getValidFor().getEnd().orElse(Instant.now()).compareTo(time) >= 0)
.findAny();
}

@Override
public Iterator<TransparencyLog> iterator() {
return getTransparencyLogs().iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class CertificatesTest {
static final String CERT_CHAIN = "dev/sigstore/samples/certs/cert.pem";
static final String CERT = "dev/sigstore/samples/certs/cert-single.pem";
static final String CERT_GH = "dev/sigstore/samples/certs/cert-githuboidc.pem";

@Test
public void testCertificateTranslation() throws IOException, CertificateException {
Expand Down Expand Up @@ -130,4 +131,19 @@ public void toCertPath() throws Exception {
Assertions.assertEquals(1, certPath.getCertificates().size());
Assertions.assertEquals(cert, certPath.getCertificates().get(0));
}

@Test
public void appendCertPath() throws Exception {
var certPath =
Certificates.fromPemChain(Resources.toByteArray(Resources.getResource(CERT_CHAIN)));
var cert = Certificates.fromPem(Resources.toByteArray(Resources.getResource(CERT_GH)));

Assertions.assertEquals(2, certPath.getCertificates().size());
var appended = Certificates.appendCertPath(certPath, cert);

Assertions.assertEquals(3, appended.getCertificates().size());
Assertions.assertEquals(cert, appended.getCertificates().get(0));
Assertions.assertEquals(certPath.getCertificates().get(0), appended.getCertificates().get(1));
Assertions.assertEquals(certPath.getCertificates().get(1), appended.getCertificates().get(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void from_checkFields() throws Exception {
Base64.toBase64String(tlog.getLogId().getKeyId()));
assertEquals(
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE2G2Y+2tabdTV5BcGiBIx0a9fAFwrkBbmLSGtks4L3qX6yYY0zufBnhC8Ur/iy55GhWP/9A/bY2LhC30M9+RYtw==",
Base64.toBase64String(PublicKey.toJavaPublicKey(tlog.getPublicKey()).getEncoded()));
Base64.toBase64String(tlog.getPublicKey().toJavaPublicKey().getEncoded()));

var oldCTLog = trustRoot.getCTLogs().all().get(0);
assertEquals("https://ctfe.sigstore.dev/test", oldCTLog.getBaseUrl().toString());
Expand All @@ -116,7 +116,7 @@ void from_checkFields() throws Exception {
Base64.toBase64String(oldCTLog.getLogId().getKeyId()));
assertEquals(
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEbfwR+RJudXscgRBRpKX1XFDy3PyudDxz/SfnRi1fT8ekpfBd2O1uoz7jr3Z8nKzxA69EUQ+eFCFI3zeubPWU7w==",
Base64.toBase64String(PublicKey.toJavaPublicKey(oldCTLog.getPublicKey()).getEncoded()));
Base64.toBase64String(oldCTLog.getPublicKey().toJavaPublicKey().getEncoded()));

var currCTLog = trustRoot.getCTLogs().all().get(1);
assertEquals("https://ctfe.sigstore.dev/2022", currCTLog.getBaseUrl().toString());
Expand All @@ -130,7 +130,7 @@ void from_checkFields() throws Exception {
Base64.toBase64String(currCTLog.getLogId().getKeyId()));
assertEquals(
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiPSlFi0CmFTfEjCUqF9HuCEcYXNKAaYalIJmBZ8yyezPjTqhxrKBpMnaocVtLJBI1eM3uXnQzQGAJdJ4gs9Fyw==",
Base64.toBase64String(PublicKey.toJavaPublicKey(currCTLog.getPublicKey()).getEncoded()));
Base64.toBase64String(currCTLog.getPublicKey().toJavaPublicKey().getEncoded()));
}

@Test
Expand Down