|
| 1 | +package eu.webeid.security.validator; |
| 2 | + |
| 3 | +import eu.webeid.security.authtoken.WebEidAuthToken; |
| 4 | +import eu.webeid.security.certificate.CertificateLoader; |
| 5 | +import eu.webeid.security.exceptions.AuthTokenException; |
| 6 | +import eu.webeid.security.exceptions.AuthTokenParseException; |
| 7 | +import eu.webeid.security.validator.certvalidators.SubjectCertificateNotRevokedValidator; |
| 8 | +import eu.webeid.security.validator.certvalidators.SubjectCertificateTrustedValidator; |
| 9 | +import eu.webeid.security.validator.certvalidators.SubjectCertificateValidatorBatch; |
| 10 | +import eu.webeid.security.validator.ocsp.OcspClient; |
| 11 | +import eu.webeid.security.validator.ocsp.OcspServiceProvider; |
| 12 | + |
| 13 | +import java.security.cert.CertStore; |
| 14 | +import java.security.cert.TrustAnchor; |
| 15 | +import java.security.cert.X509Certificate; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +public final class AuthTokenV1Validator implements AuthTokenValidator { |
| 19 | + |
| 20 | + private static final String SUPPORTED_TOKEN_FORMAT_VERSION = "web-eid:1"; |
| 21 | + |
| 22 | + private final SubjectCertificateValidatorBatch simpleSubjectCertificateValidators; |
| 23 | + private final Set<TrustAnchor> trustedCACertificateAnchors; |
| 24 | + private final CertStore trustedCACertificateCertStore; |
| 25 | + private final AuthTokenSignatureValidator authTokenSignatureValidator; |
| 26 | + private final AuthTokenValidationConfiguration configuration; |
| 27 | + private final OcspClient ocspClient; |
| 28 | + private final OcspServiceProvider ocspServiceProvider; |
| 29 | + |
| 30 | + public AuthTokenV1Validator( |
| 31 | + SubjectCertificateValidatorBatch simpleSubjectCertificateValidators, |
| 32 | + Set<TrustAnchor> trustedCACertificateAnchors, |
| 33 | + CertStore trustedCACertificateCertStore, |
| 34 | + AuthTokenSignatureValidator authTokenSignatureValidator, |
| 35 | + AuthTokenValidationConfiguration configuration, |
| 36 | + OcspClient ocspClient, |
| 37 | + OcspServiceProvider ocspServiceProvider |
| 38 | + ) { |
| 39 | + this.simpleSubjectCertificateValidators = simpleSubjectCertificateValidators; |
| 40 | + this.trustedCACertificateAnchors = trustedCACertificateAnchors; |
| 41 | + this.trustedCACertificateCertStore = trustedCACertificateCertStore; |
| 42 | + this.authTokenSignatureValidator = authTokenSignatureValidator; |
| 43 | + this.configuration = configuration; |
| 44 | + this.ocspClient = ocspClient; |
| 45 | + this.ocspServiceProvider = ocspServiceProvider; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean supports(String format) { |
| 50 | + return format != null && format.startsWith(SUPPORTED_TOKEN_FORMAT_VERSION); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public WebEidAuthToken parse(String authToken) throws AuthTokenException { |
| 55 | + throw new UnsupportedOperationException("Parsing is handled by AuthTokenValidatorImpl. " + this.getClass().getSimpleName() + " only supports validation."); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException { |
| 60 | + if (token.getFormat() == null || token.getFormat().isBlank()) { |
| 61 | + throw new AuthTokenParseException("'format' field is missing"); |
| 62 | + } |
| 63 | + if (!token.getFormat().startsWith(SUPPORTED_TOKEN_FORMAT_VERSION)) { |
| 64 | + throw new AuthTokenParseException("Only token format version '" + SUPPORTED_TOKEN_FORMAT_VERSION + "' is currently supported"); |
| 65 | + } |
| 66 | + |
| 67 | + if (token.getUnverifiedCertificate() == null || token.getUnverifiedCertificate().isEmpty()) { |
| 68 | + throw new AuthTokenParseException("'unverifiedCertificate' field is missing, null or empty"); |
| 69 | + } |
| 70 | + |
| 71 | + final X509Certificate subjectCertificate = CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedCertificate()); |
| 72 | + |
| 73 | + simpleSubjectCertificateValidators.executeFor(subjectCertificate); |
| 74 | + getCertTrustValidators().executeFor(subjectCertificate); |
| 75 | + |
| 76 | + // It is guaranteed that if the signature verification succeeds, then the origin and challenge |
| 77 | + // have been implicitly and correctly verified without the need to implement any additional checks. |
| 78 | + authTokenSignatureValidator.validate( |
| 79 | + token.getAlgorithm(), |
| 80 | + token.getSignature(), |
| 81 | + subjectCertificate.getPublicKey(), |
| 82 | + currentChallengeNonce |
| 83 | + ); |
| 84 | + |
| 85 | + return subjectCertificate; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Creates the certificate trust validators batch. |
| 90 | + * As SubjectCertificateTrustedValidator has mutable state that SubjectCertificateNotRevokedValidator depends on, |
| 91 | + * they cannot be reused/cached in an instance variable in a multi-threaded environment. Hence, they are |
| 92 | + * re-created for each validation run for thread safety. |
| 93 | + * |
| 94 | + * @return certificate trust validator batch |
| 95 | + */ |
| 96 | + private SubjectCertificateValidatorBatch getCertTrustValidators() { |
| 97 | + return createCertTrustValidators( |
| 98 | + configuration, |
| 99 | + trustedCACertificateAnchors, |
| 100 | + trustedCACertificateCertStore, |
| 101 | + ocspClient, |
| 102 | + ocspServiceProvider |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public static SubjectCertificateValidatorBatch createCertTrustValidators( |
| 107 | + AuthTokenValidationConfiguration configuration, |
| 108 | + Set<TrustAnchor> trustedCACertificateAnchors, |
| 109 | + CertStore trustedCACertificateCertStore, |
| 110 | + OcspClient ocspClient, |
| 111 | + OcspServiceProvider ocspServiceProvider |
| 112 | + ) { |
| 113 | + final SubjectCertificateTrustedValidator certTrustedValidator = |
| 114 | + new SubjectCertificateTrustedValidator(trustedCACertificateAnchors, trustedCACertificateCertStore); |
| 115 | + |
| 116 | + return SubjectCertificateValidatorBatch.createFrom( |
| 117 | + certTrustedValidator::validateCertificateTrusted |
| 118 | + ).addOptional(configuration.isUserCertificateRevocationCheckWithOcspEnabled(), |
| 119 | + new SubjectCertificateNotRevokedValidator( |
| 120 | + certTrustedValidator, |
| 121 | + ocspClient, ocspServiceProvider, |
| 122 | + configuration.getAllowedOcspResponseTimeSkew(), |
| 123 | + configuration.getMaxOcspResponseThisUpdateAge() |
| 124 | + )::validateCertificateNotRevoked |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments