-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specificially for CertificateAuthorities and TransparencyLogs We need to be able to query the trustroot for CAs and logs to initialize our signers and based on the material we are signing. This will eventually allow us to init a client directly from a trustroot Signed-off-by: Appu Goundan <[email protected]>
- Loading branch information
1 parent
9bebb37
commit 9b9fb4c
Showing
9 changed files
with
516 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
sigstore-java/src/main/java/dev/sigstore/trustroot/CertificateAuthorities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.trustroot; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.immutables.value.Value; | ||
import org.immutables.value.Value.Derived; | ||
import org.immutables.value.Value.Immutable; | ||
|
||
@Immutable | ||
@Value.Style( | ||
depluralize = true, | ||
depluralizeDictionary = {"certificateAuthority:certificateAuthorities"}) | ||
public abstract class CertificateAuthorities { | ||
|
||
public abstract List<CertificateAuthority> getCertificateAuthorities(); | ||
|
||
@Derived | ||
public int size() { | ||
return getCertificateAuthorities().size(); | ||
} | ||
|
||
@Derived | ||
public List<CertificateAuthority> all() { | ||
return getCertificateAuthorities(); | ||
} | ||
|
||
/** | ||
* Find a CA by validity time, users of this method will need to then compare the key in the leaf | ||
* to find the exact CA to validate against | ||
* | ||
* @param time the time the CA was expected to be valid (usually tlog entry time) | ||
* @return a list of CAs that were valid at {@code time} | ||
*/ | ||
public List<CertificateAuthority> find(Instant time) { | ||
return getCertificateAuthorities().stream() | ||
.filter(ca -> ca.getValidFor().getStart().compareTo(time) <= 0) | ||
.filter(ca -> ca.getValidFor().getEnd().orElse(Instant.now()).compareTo(time) >= 0) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Get the one an only current Certificate Authority | ||
* | ||
* @return the current active CA | ||
* @throws IllegalStateException if trust root does not contain exactly one active CA | ||
*/ | ||
public CertificateAuthority current() { | ||
var current = | ||
getCertificateAuthorities().stream() | ||
.filter(ca -> ca.getValidFor().getEnd().isEmpty()) | ||
.collect(Collectors.toList()); | ||
if (current.size() == 0) { | ||
throw new IllegalStateException("Trust root contains no current certificate authorities"); | ||
} | ||
if (current.size() > 1) { | ||
throw new IllegalStateException( | ||
"Trust root contains multiple current certificate authorities (" + current.size() + ")"); | ||
} | ||
return current.get(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sigstore-java/src/main/java/dev/sigstore/trustroot/TransparencyLogs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2023 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.trustroot; | ||
|
||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.immutables.value.Value; | ||
import org.immutables.value.Value.Derived; | ||
import org.immutables.value.Value.Immutable; | ||
|
||
@Immutable | ||
@Value.Style(depluralize = true) | ||
public abstract class TransparencyLogs { | ||
|
||
public abstract List<TransparencyLog> getTransparencyLogs(); | ||
|
||
@Derived | ||
public int size() { | ||
return getTransparencyLogs().size(); | ||
} | ||
|
||
@Derived | ||
public List<TransparencyLog> all() { | ||
return getTransparencyLogs(); | ||
} | ||
|
||
public TransparencyLog current() { | ||
var current = | ||
getTransparencyLogs().stream() | ||
.filter(tl -> tl.getPublicKey().getValidFor().getEnd().isEmpty()) | ||
.collect(Collectors.toList()); | ||
if (current.size() == 0) { | ||
throw new IllegalStateException("Trust root contains no current transparency logs"); | ||
} | ||
if (current.size() > 1) { | ||
throw new IllegalStateException( | ||
"Trust root contains multiple current transparency logs (" + current.size() + ")"); | ||
} | ||
return current.get(0); | ||
} | ||
|
||
public Optional<TransparencyLog> find(byte[] logId, Instant time) { | ||
return getTransparencyLogs().stream() | ||
.filter(tl -> Arrays.equals(tl.getLogId().getKeyId(), logId)) | ||
.filter(tl -> tl.getPublicKey().getValidFor().getStart().compareTo(time) <= 0) | ||
.filter( | ||
tl -> | ||
tl.getPublicKey().getValidFor().getEnd().orElse(Instant.now()).compareTo(time) >= 0) | ||
.findAny(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
sigstore-java/src/test/java/dev/sigstore/trustroot/CertificateAuthoritiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2023 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.trustroot; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.net.URI; | ||
import java.security.cert.CertPath; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
class CertificateAuthoritiesTest { | ||
@Test | ||
public void current_missing() { | ||
Assertions.assertThrows( | ||
IllegalStateException.class, | ||
() -> ImmutableCertificateAuthorities.builder().build().current()); | ||
} | ||
|
||
@Test | ||
public void current_tooMany() { | ||
var ca = | ||
ImmutableCertificateAuthority.builder() | ||
.certPath(Mockito.mock(CertPath.class)) | ||
.uri(URI.create("abc")) | ||
.subject(ImmutableSubject.builder().commonName("abc").organization("xyz").build()) | ||
.validFor( | ||
ImmutableValidFor.builder() | ||
.start(Instant.now().minus(10, ChronoUnit.SECONDS)) | ||
.build()) | ||
.build(); | ||
Assertions.assertThrows( | ||
IllegalStateException.class, | ||
() -> | ||
ImmutableCertificateAuthorities.builder() | ||
.addCertificateAuthorities(ca, ca) | ||
.build() | ||
.current()); | ||
} | ||
} |
Oops, something went wrong.