|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2025 Estonian Information System Authority |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package eu.webeid.security.validator; |
| 24 | + |
| 25 | +import eu.webeid.security.authtoken.SupportedSignatureAlgorithm; |
| 26 | +import eu.webeid.security.authtoken.WebEidAuthToken; |
| 27 | +import eu.webeid.security.certificate.CertificateLoader; |
| 28 | +import eu.webeid.security.exceptions.AuthTokenException; |
| 29 | +import eu.webeid.security.exceptions.AuthTokenParseException; |
| 30 | +import eu.webeid.security.exceptions.CertificateDecodingException; |
| 31 | +import eu.webeid.security.validator.certvalidators.SubjectCertificateValidatorBatch; |
| 32 | +import eu.webeid.security.validator.ocsp.OcspClient; |
| 33 | +import eu.webeid.security.validator.ocsp.OcspServiceProvider; |
| 34 | +import org.bouncycastle.asn1.x500.X500Name; |
| 35 | +import org.bouncycastle.asn1.x500.style.RFC4519Style; |
| 36 | +import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; |
| 37 | +import org.bouncycastle.asn1.x509.Extension; |
| 38 | +import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; |
| 39 | + |
| 40 | +import javax.security.auth.x500.X500Principal; |
| 41 | +import java.security.cert.CertStore; |
| 42 | +import java.security.cert.TrustAnchor; |
| 43 | +import java.security.cert.X509Certificate; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Set; |
| 47 | + |
| 48 | +import static eu.webeid.security.util.Strings.isNullOrEmpty; |
| 49 | + |
| 50 | +class AuthTokenV11Validator extends AuthTokenV1Validator { |
| 51 | + |
| 52 | + private static final String SUPPORTED_PREFIX = "web-eid:1.1"; |
| 53 | + private static final Set<String> SUPPORTED_CRYPTO_ALGORITHMS = Set.of("ECC", "RSA"); |
| 54 | + private static final Set<String> SUPPORTED_PADDING_SCHEMES = Set.of("NONE", "PKCS1.5", "PSS"); |
| 55 | + private static final Set<String> SUPPORTED_HASH_FUNCTIONS = Set.of( |
| 56 | + "SHA-224", "SHA-256", "SHA-384", "SHA-512", |
| 57 | + "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" |
| 58 | + ); |
| 59 | + private static final int KEY_USAGE_NON_REPUDIATION = 1; |
| 60 | + |
| 61 | + public AuthTokenV11Validator( |
| 62 | + SubjectCertificateValidatorBatch simpleSubjectCertificateValidators, |
| 63 | + Set<TrustAnchor> trustedCACertificateAnchors, |
| 64 | + CertStore trustedCACertificateCertStore, |
| 65 | + AuthTokenSignatureValidator authTokenSignatureValidator, |
| 66 | + AuthTokenValidationConfiguration configuration, |
| 67 | + OcspClient ocspClient, |
| 68 | + OcspServiceProvider ocspServiceProvider |
| 69 | + ) { |
| 70 | + super( |
| 71 | + simpleSubjectCertificateValidators, |
| 72 | + trustedCACertificateAnchors, |
| 73 | + trustedCACertificateCertStore, |
| 74 | + authTokenSignatureValidator, |
| 75 | + configuration, |
| 76 | + ocspClient, |
| 77 | + ocspServiceProvider |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean supports(String format) { |
| 83 | + return format != null && format.startsWith(SUPPORTED_PREFIX); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException { |
| 88 | + final X509Certificate subjectCertificate = validateV1(token, currentChallengeNonce); |
| 89 | + |
| 90 | + validateFormat(token); |
| 91 | + final X509Certificate signingCertificate = validateSigningCertificateExists(token); |
| 92 | + validateSupportedSignatureAlgorithms(token.getSupportedSignatureAlgorithms()); |
| 93 | + validateSameSubject(subjectCertificate, signingCertificate); |
| 94 | + validateSameIssuer(subjectCertificate, signingCertificate); |
| 95 | + validateKeyUsage(signingCertificate); |
| 96 | + |
| 97 | + return subjectCertificate; |
| 98 | + } |
| 99 | + |
| 100 | + private static void validateSupportedSignatureAlgorithms(List<SupportedSignatureAlgorithm> algorithms) throws AuthTokenParseException { |
| 101 | + if (algorithms == null || algorithms.isEmpty()) { |
| 102 | + throw new AuthTokenParseException("'supportedSignatureAlgorithms' field is missing"); |
| 103 | + } |
| 104 | + |
| 105 | + boolean hasInvalid = algorithms.stream().anyMatch(supportedSignatureAlgorithm -> |
| 106 | + !SUPPORTED_CRYPTO_ALGORITHMS.contains(supportedSignatureAlgorithm.getCryptoAlgorithm()) || |
| 107 | + !SUPPORTED_HASH_FUNCTIONS.contains(supportedSignatureAlgorithm.getHashFunction()) || |
| 108 | + !SUPPORTED_PADDING_SCHEMES.contains(supportedSignatureAlgorithm.getPaddingScheme()) |
| 109 | + ); |
| 110 | + |
| 111 | + if (hasInvalid) { |
| 112 | + throw new AuthTokenParseException("Unsupported signature algorithm"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void validateFormat(WebEidAuthToken token) throws AuthTokenParseException { |
| 117 | + if (token.getFormat() == null || !token.getFormat().startsWith(SUPPORTED_PREFIX)) { |
| 118 | + throw new AuthTokenParseException("Only token format '" + SUPPORTED_PREFIX + "' is supported by this validator"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static X509Certificate validateSigningCertificateExists(WebEidAuthToken token) throws AuthTokenParseException, CertificateDecodingException { |
| 123 | + if (isNullOrEmpty(token.getUnverifiedSigningCertificate())) { |
| 124 | + throw new AuthTokenParseException("'unverifiedSigningCertificate' field is missing, null or empty for format 'web-eid:1.1'"); |
| 125 | + } |
| 126 | + return CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedSigningCertificate()); |
| 127 | + } |
| 128 | + |
| 129 | + private static void validateSameSubject(X509Certificate subjectCertificate, X509Certificate signingCertificate) |
| 130 | + throws AuthTokenParseException { |
| 131 | + if (!subjectAndSigningCertificateSubjectsMatch( |
| 132 | + subjectCertificate.getSubjectX500Principal(), |
| 133 | + signingCertificate.getSubjectX500Principal())) { |
| 134 | + throw new AuthTokenParseException("Signing certificate subject does not match authentication certificate subject"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static void validateSameIssuer(X509Certificate subjectCertificate, X509Certificate signingCertificate) |
| 139 | + throws AuthTokenParseException { |
| 140 | + byte[] subjectCertificateAuthorityKeyIdentifier = getAuthorityKeyIdentifier(subjectCertificate); |
| 141 | + byte[] signingCertificateAuthorityKeyIdentifier = getAuthorityKeyIdentifier(signingCertificate); |
| 142 | + |
| 143 | + if (subjectCertificateAuthorityKeyIdentifier.length == 0 |
| 144 | + || signingCertificateAuthorityKeyIdentifier.length == 0 |
| 145 | + || !Arrays.equals(subjectCertificateAuthorityKeyIdentifier, signingCertificateAuthorityKeyIdentifier)) { |
| 146 | + throw new AuthTokenParseException( |
| 147 | + "Signing certificate is not issued by the same issuing authority as the authentication certificate"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static void validateKeyUsage(X509Certificate signingCertificate) throws AuthTokenParseException { |
| 152 | + boolean[] keyUsage = signingCertificate.getKeyUsage(); |
| 153 | + if (keyUsage == null || keyUsage.length <= KEY_USAGE_NON_REPUDIATION || !keyUsage[KEY_USAGE_NON_REPUDIATION]) { |
| 154 | + throw new AuthTokenParseException("Signing certificate key usage extension missing or does not contain non-repudiation bit required for digital signatures"); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static boolean subjectAndSigningCertificateSubjectsMatch( |
| 159 | + X500Principal authenticationCertificateSubject, |
| 160 | + X500Principal signingCertificateSubject) { |
| 161 | + X500Name authName = X500Name.getInstance(RFC4519Style.INSTANCE, authenticationCertificateSubject.getEncoded()); |
| 162 | + X500Name signName = X500Name.getInstance(RFC4519Style.INSTANCE, signingCertificateSubject.getEncoded()); |
| 163 | + return authName.equals(signName); |
| 164 | + } |
| 165 | + |
| 166 | + private static byte[] getAuthorityKeyIdentifier(X509Certificate certificate) throws AuthTokenParseException { |
| 167 | + try { |
| 168 | + byte[] authorityKeyIdentifierExtension = certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId()); |
| 169 | + if (authorityKeyIdentifierExtension == null) { |
| 170 | + return new byte[0]; |
| 171 | + } |
| 172 | + AuthorityKeyIdentifier authorityKeyIdentifier = |
| 173 | + AuthorityKeyIdentifier.getInstance(JcaX509ExtensionUtils.parseExtensionValue(authorityKeyIdentifierExtension)); |
| 174 | + return authorityKeyIdentifier.getKeyIdentifier(); |
| 175 | + } catch (Exception e) { |
| 176 | + throw new AuthTokenParseException("Failed to parse Authority Key Identifier", e); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + protected X509Certificate validateV1(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException { |
| 181 | + return super.validate(token, currentChallengeNonce); |
| 182 | + } |
| 183 | +} |
0 commit comments