-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Rename CertificateResponse to SigningCertificate - Add more testing around parsing - Add example fulcio/ctfe public keys and sct/cert examples for testing - Exceptions are still kinda just passed along Signed-off-by: Appu Goundan <[email protected]>
- Loading branch information
1 parent
4aa2b19
commit 8bc998d
Showing
16 changed files
with
448 additions
and
93 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
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
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
33 changes: 0 additions & 33 deletions
33
src/main/java/dev/sigstore/fulcio/client/CertificateRequests.java
This file was deleted.
Oops, something went wrong.
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
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
120 changes: 120 additions & 0 deletions
120
src/main/java/dev/sigstore/fulcio/client/FulcioValidator.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,120 @@ | ||
/* | ||
* Copyright 2022 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.fulcio.client; | ||
|
||
import com.google.api.client.util.PemReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PublicKey; | ||
import java.security.cert.*; | ||
import java.security.spec.EncodedKeySpec; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import org.conscrypt.ct.CTLogInfo; | ||
import org.conscrypt.ct.CertificateEntry; | ||
import org.conscrypt.ct.VerifiedSCT; | ||
|
||
public class FulcioValidator { | ||
private final CTLogInfo ctLogInfo; | ||
private final TrustAnchor fulcioRoot; | ||
|
||
public static FulcioValidator NewFulcioValidator(byte[] ctfePublicKey, byte[] fulcioRoot) | ||
throws InvalidKeySpecException, NoSuchAlgorithmException, CertificateException, IOException, | ||
InvalidAlgorithmParameterException { | ||
KeyFactory keyFactory = KeyFactory.getInstance("EC"); | ||
PemReader pemReader = | ||
new PemReader(new InputStreamReader(new ByteArrayInputStream(ctfePublicKey))); | ||
PemReader.Section section = pemReader.readNextSection(); | ||
if (pemReader.readNextSection() != null) { | ||
throw new InvalidKeySpecException( | ||
"ctfe public key must be only a single PEM encoded public key"); | ||
} | ||
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(section.getBase64DecodedBytes()); | ||
PublicKey ctfePublicKeyObj = keyFactory.generatePublic(publicKeySpec); | ||
|
||
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
X509Certificate fulcioRootObj = | ||
(X509Certificate) | ||
certificateFactory.generateCertificate(new ByteArrayInputStream(fulcioRoot)); | ||
|
||
TrustAnchor fulcioRootTrustAnchor = new TrustAnchor(fulcioRootObj, null); | ||
// this should throw the InvalidAlgorithmException a bit earlier | ||
new PKIXParameters(Collections.singleton(fulcioRootTrustAnchor)); | ||
|
||
// TODO: this ctloginfo probably needs to be appropriately filled out | ||
CTLogInfo ctLogInfo = | ||
new CTLogInfo(ctfePublicKeyObj, "fulcio ct log", "intentionally-nonsense"); | ||
return new FulcioValidator(ctLogInfo, fulcioRootTrustAnchor); | ||
} | ||
|
||
private FulcioValidator(CTLogInfo ctLogInfo, TrustAnchor fulcioRoot) { | ||
this.ctLogInfo = ctLogInfo; | ||
this.fulcioRoot = fulcioRoot; | ||
} | ||
|
||
public void validateSct(SigningCertificate sc) | ||
throws CertificateEncodingException, FulcioValidationException { | ||
if (!(sc.getLeafCertificate() instanceof X509Certificate)) { | ||
throw new RuntimeException("Encountered non X509 Certificate when validating SCT"); | ||
} | ||
CertificateEntry ce = | ||
CertificateEntry.createForX509Certificate((X509Certificate) sc.getLeafCertificate()); | ||
|
||
// a result is returned here, but I don't know what to do with it yet | ||
VerifiedSCT.Status status = ctLogInfo.verifySingleSCT(sc.getSct(), ce); | ||
if (status != VerifiedSCT.Status.VALID) { | ||
throw new FulcioValidationException("SCT could not be verified because " + status.toString()); | ||
} | ||
} | ||
|
||
public void validateCertChain(SigningCertificate sc) throws FulcioValidationException { | ||
CertPathValidator cpv; | ||
try { | ||
cpv = CertPathValidator.getInstance("PKIX"); | ||
} catch (NoSuchAlgorithmException e) { | ||
// no PKIX, we probably shouldn't be here, but this seems to be a system library error | ||
// not a program control flow issue | ||
throw new RuntimeException(e); | ||
} | ||
|
||
PKIXParameters pkixParams; | ||
try { | ||
pkixParams = new PKIXParameters(Collections.singleton(fulcioRoot)); | ||
} catch (InvalidAlgorithmParameterException e) { | ||
// this should have been checked when generating a validator instance | ||
throw new RuntimeException(e); | ||
} | ||
pkixParams.setRevocationEnabled(false); | ||
|
||
// these certs are only valid for 15 minutes, so find a time in the validity period | ||
Date dateInValidityPeriod = | ||
new Date(((X509Certificate) sc.getLeafCertificate()).getNotBefore().getTime() + 1000); | ||
pkixParams.setDate(dateInValidityPeriod); | ||
|
||
try { | ||
// a result is returned here, but I don't know what to do with it yet | ||
cpv.validate(sc.getCertPath(), pkixParams); | ||
} catch (CertPathValidatorException | InvalidAlgorithmParameterException ve) { | ||
throw new FulcioValidationException(ve); | ||
} | ||
} | ||
} |
Oops, something went wrong.