|
| 1 | +package org.webeid.security.certificate; |
| 2 | + |
| 3 | +import org.webeid.security.exceptions.CertificateNotTrustedException; |
| 4 | +import org.webeid.security.exceptions.JceException; |
| 5 | +import org.webeid.security.exceptions.UserCertificateExpiredException; |
| 6 | +import org.webeid.security.exceptions.UserCertificateNotYetValidException; |
| 7 | + |
| 8 | +import java.security.GeneralSecurityException; |
| 9 | +import java.security.InvalidAlgorithmParameterException; |
| 10 | +import java.security.NoSuchAlgorithmException; |
| 11 | +import java.security.cert.CertPathBuilder; |
| 12 | +import java.security.cert.CertPathBuilderException; |
| 13 | +import java.security.cert.CertStore; |
| 14 | +import java.security.cert.CertificateExpiredException; |
| 15 | +import java.security.cert.CertificateNotYetValidException; |
| 16 | +import java.security.cert.CollectionCertStoreParameters; |
| 17 | +import java.security.cert.PKIXBuilderParameters; |
| 18 | +import java.security.cert.PKIXCertPathBuilderResult; |
| 19 | +import java.security.cert.TrustAnchor; |
| 20 | +import java.security.cert.X509CertSelector; |
| 21 | +import java.security.cert.X509Certificate; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Date; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +public final class CertificateValidator { |
| 29 | + |
| 30 | + /** |
| 31 | + * Checks whether the certificate was valid on the given date. |
| 32 | + */ |
| 33 | + public static void certificateIsValidOnDate(X509Certificate cert, Date date) throws UserCertificateNotYetValidException, UserCertificateExpiredException { |
| 34 | + try { |
| 35 | + cert.checkValidity(date); |
| 36 | + } catch (CertificateNotYetValidException e) { |
| 37 | + throw new UserCertificateNotYetValidException(e); |
| 38 | + } catch (CertificateExpiredException e) { |
| 39 | + throw new UserCertificateExpiredException(e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static X509Certificate validateIsSignedByTrustedCA(X509Certificate certificate, |
| 44 | + Set<TrustAnchor> trustedCACertificateAnchors, |
| 45 | + CertStore trustedCACertificateCertStore) throws CertificateNotTrustedException, JceException { |
| 46 | + final X509CertSelector selector = new X509CertSelector(); |
| 47 | + selector.setCertificate(certificate); |
| 48 | + |
| 49 | + try { |
| 50 | + final PKIXBuilderParameters pkixBuilderParameters = new PKIXBuilderParameters(trustedCACertificateAnchors, selector); |
| 51 | + pkixBuilderParameters.setRevocationEnabled(false); |
| 52 | + pkixBuilderParameters.addCertStore(trustedCACertificateCertStore); |
| 53 | + |
| 54 | + // See the comment in buildCertStoreFromCertificates() below why we use the default JCE provider. |
| 55 | + final CertPathBuilder certPathBuilder = CertPathBuilder.getInstance(CertPathBuilder.getDefaultType()); |
| 56 | + final PKIXCertPathBuilderResult result = (PKIXCertPathBuilderResult) certPathBuilder.build(pkixBuilderParameters); |
| 57 | + |
| 58 | + return result.getTrustAnchor().getTrustedCert(); |
| 59 | + |
| 60 | + } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException e) { |
| 61 | + throw new JceException(e); |
| 62 | + } catch (CertPathBuilderException e) { |
| 63 | + throw new CertificateNotTrustedException(certificate, e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static Set<TrustAnchor> buildTrustAnchorsFromCertificates(Collection<X509Certificate> certificates) { |
| 68 | + return certificates.stream() |
| 69 | + .map(cert -> new TrustAnchor(cert, null)) |
| 70 | + .collect(Collectors.toSet()); |
| 71 | + } |
| 72 | + |
| 73 | + public static Set<TrustAnchor> buildTrustAnchorsFromCertificate(X509Certificate certificate) { |
| 74 | + return buildTrustAnchorsFromCertificates(Collections.singleton(certificate)); |
| 75 | + } |
| 76 | + |
| 77 | + public static CertStore buildCertStoreFromCertificates(Collection<X509Certificate> certificates) throws JceException { |
| 78 | + // We use the default JCE provider as there is no reason to use Bouncy Castle, moreover BC requires |
| 79 | + // the validated certificate to be in the certificate store which breaks the clean immutable usage of |
| 80 | + // trustedCACertificateCertStore in SubjectCertificateTrustedValidator. |
| 81 | + try { |
| 82 | + return CertStore.getInstance("Collection", new CollectionCertStoreParameters(certificates)); |
| 83 | + } catch (GeneralSecurityException e) { |
| 84 | + throw new JceException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static CertStore buildCertStoreFromCertificate(X509Certificate certificate) throws JceException { |
| 89 | + return buildCertStoreFromCertificates(Collections.singleton(certificate)); |
| 90 | + } |
| 91 | + |
| 92 | + private CertificateValidator() { |
| 93 | + throw new IllegalStateException("Utility class"); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments