Class DefaultCertificateChainValidator
- java.lang.Object
-
- com.microej.util.appverifier.DefaultCertificateChainValidator
-
- All Implemented Interfaces:
CertificateChainValidator
public class DefaultCertificateChainValidator extends Object implements CertificateChainValidator
This implementation validates the first certificate of a chain when this certificate has been signed by a known and trusted certificate, named a trusted CA.The trusted CAs are provided at construction time. They can be loaded from any source (for example a
KeyStoreor a secure storage) and passed toDefaultCertificateChainValidator(X509Certificate[]), or loaded from an Application resource file with thefromResource()factory.A chain root is trusted when it has been signed by one of the trusted CAs. A self-signed root (its signature verifies with its own public key) is its own trust anchor and is trusted only when it is byte-identical to one of the trusted certificates.
- Since:
- 1.0.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDefaultCertificateChainValidator()Constructs a validator with no trusted CA.DefaultCertificateChainValidator(X509Certificate[] trustedCAs)Constructs a validator that anchors trust to the given trusted CAs.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DefaultCertificateChainValidatorfromResource()Creates a validator whose trusted CAs are loaded from the Application resource file identified by the "appverifier.trustedCAs.path" BON constant (default/trustedCAs.der).protected static booleanisSignedBy(Certificate certificate, PublicKey publicKey)Tells whether the given certificate has been signed by the given public key.booleanvalidateCertificate(Certificate previousCert, Certificate cert, boolean isLast, boolean isAlreadyAuthorized)Asks this validator to check the given certificate, in the context of a certificate chain.protected booleanvalidateRoot(X509Certificate x509cert)Validates the root of the certificate chain against this validator's trust anchor and decides whether the chain is authorized.
-
-
-
Constructor Detail
-
DefaultCertificateChainValidator
public DefaultCertificateChainValidator(X509Certificate[] trustedCAs)
Constructs a validator that anchors trust to the given trusted CAs.- Parameters:
trustedCAs- the trusted CA certificates, in no particular order.
-
DefaultCertificateChainValidator
protected DefaultCertificateChainValidator()
Constructs a validator with no trusted CA. This constructor is reserved for subclasses that fully overridevalidateRoot(X509Certificate)to anchor trust differently.
-
-
Method Detail
-
fromResource
public static DefaultCertificateChainValidator fromResource() throws CertificateException
Creates a validator whose trusted CAs are loaded from the Application resource file identified by the "appverifier.trustedCAs.path" BON constant (default/trustedCAs.der). The file contains the trusted CA certificates concatenated one after another, in no particular order.- Returns:
- a validator anchored to the trusted CAs read from the resource file.
- Throws:
CertificateException- if the resource file is missing or contains no certificate.- Since:
- 1.0.0
-
validateCertificate
public boolean validateCertificate(Certificate previousCert, Certificate cert, boolean isLast, boolean isAlreadyAuthorized) throws CertificateException
Description copied from interface:CertificateChainValidatorAsks this validator to check the given certificate, in the context of a certificate chain.For the first certificate in the chain (i.e. when
previousCertisnull), the validator will typically lookup for a Trust Anchor (known certificate that can be trusted) matching the issuer X500 DN embedded in the certificate to validate.- Specified by:
validateCertificatein interfaceCertificateChainValidator- Parameters:
previousCert- the previous certificate in the chain, if any, ornull.cert- the certificate to validate.isLast-trueif this is the last certificate to validate in the chain.isAlreadyAuthorized-trueif a previous certificate in the chain was already authorized.- Returns:
- a boolean indicating if the subject of this certificate is trusted enough to authorize the Application.
Returning
truemeans that this subject is authorized and that the rest of the chain will be checked for validity only, not for authorization. - Throws:
CertificateException- if there was a technical problem validating this certificate. Check the cause to know more about the actual issue.
-
validateRoot
protected boolean validateRoot(X509Certificate x509cert) throws CertificateException
Validates the root of the certificate chain against this validator's trust anchor and decides whether the chain is authorized.This implementation anchors trust to the trusted CAs of this validator: the given root is trusted when it has been signed by one of the trusted CAs, or - for a self-signed root - when it is byte-identical to one of them.
This method is the extension point of this validator. Subclasses may override it to anchor trust differently without altering the certificate-chaining logic of
validateCertificate(Certificate, Certificate, boolean, boolean). UseisSignedBy(Certificate, PublicKey)to perform signature checks from such an override.- Parameters:
x509cert- the root certificate of the chain to validate.- Returns:
trueif the chain is authorized once the root has been validated.- Throws:
CertificateException- if the given root is not anchored to a trusted certificate, or if no trusted certificates are available.- Since:
- 1.0.0
-
isSignedBy
protected static boolean isSignedBy(Certificate certificate, PublicKey publicKey)
Tells whether the given certificate has been signed by the given public key.This signature primitive is exposed to subclasses so that an alternative trust anchor can be implemented in
validateRoot(X509Certificate)without access to the package-private signature helpers of this library.- Parameters:
certificate- the certificate whose signature is checked.publicKey- the public key expected to have signed the given certificate.- Returns:
trueif the given certificate has been signed by the given public key.- Since:
- 1.0.0
-
-