Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class CertificateValidator
- Object
- CertificateValidator
Validates a certificate chain against trust anchors the application names.
CertificateValidator validator = new CertificateValidator(
Certificate.listFromPem(KeyFiles.read("/etc/app/partner-ca.pem")))
.forHost("partner.example.com");
List<Certificate> path = validator.validate(Certificate.listFromPem(presented));
There is no default trust store: nothing is trusted unless it is given. A path is built from the first certificate, the leaf, through the other certificates given (in any order) to one of the anchors, and every link is checked:
- each certificate is signed by the next, within the signature profile of
Certificate.getSignatureAlgorithm; - each certificate, the leaf and the intermediates, is inside its validity period at the validation time;
- each issuer below the anchor is a CA by its basic constraints, has
keyCertSignwhen it carries a key usage, and its path length is not exceeded; - no certificate carries a critical extension this validator does not understand (name constraints and certificate policies among them);
- when
forHostorforPurposeis set, the leaf matches.
An anchor is trusted as given: its signature, validity and constraints are not checked, as RFC 5280 treats an anchor as a name and a key. Revocation (CRL, OCSP) is not checked.
Constructors
public CertificateValidator(List<Certificate> anchors) | A validator that trusts anchors and nothing else. |
Methods
public CertificateValidator at(long millis) | Validates at millis instead of the current time. |
public CertificateValidator forHost(String host) | Requires the leaf to be for host, by Certificate.matchesHost, and to allow Certificate.SERVER_AUTH when it restricts its purposes. |
public CertificateValidator forPurpose(String extendedKeyUsage) | Requires the leaf to allow an extended key usage, such as Certificate.CLIENT_AUTH for a client certificate. |
public List<Certificate> validate(List<Certificate> chain)
throws IOException | Validates chain, the leaf first. |
Inherited methods
Constructor details
CertificateValidator
public CertificateValidator(List<Certificate> anchors)A validator that trusts
anchors and nothing else.Method details
at
public CertificateValidator at(long millis)Validates at
millis instead of the current time.forHost
public CertificateValidator forHost(String host)Requires the leaf to be for
host, by Certificate.matchesHost, and to
allow Certificate.SERVER_AUTH when it restricts its purposes.forPurpose
public CertificateValidator forPurpose(String extendedKeyUsage)Requires the leaf to allow an extended key usage, such as
Certificate.CLIENT_AUTH for a client certificate. A leaf with no
extended key usage extension allows every purpose.validate
public List<Certificate> validate(List<Certificate> chain)
throws IOExceptionValidates
chain, the leaf first.Returns
the path from the leaf to the anchor, both included
Throws
CertificateException- naming the certificate that failed and why
IOException