|
| 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.validator.certvalidators.SubjectCertificateValidatorBatch; |
| 31 | + |
| 32 | +import java.security.cert.X509Certificate; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +import static eu.webeid.security.util.Strings.isNullOrEmpty; |
| 37 | + |
| 38 | +public class NfcAuthTokenValidator { |
| 39 | + |
| 40 | + private final SubjectCertificateValidatorBatch simpleSubjectCertificateValidators; |
| 41 | + private final SubjectCertificateValidatorBatch certTrustValidators; |
| 42 | + |
| 43 | + NfcAuthTokenValidator( |
| 44 | + SubjectCertificateValidatorBatch simpleSubjectCertificateValidators, |
| 45 | + SubjectCertificateValidatorBatch certTrustValidators |
| 46 | + ) { |
| 47 | + this.simpleSubjectCertificateValidators = simpleSubjectCertificateValidators; |
| 48 | + this.certTrustValidators = certTrustValidators; |
| 49 | + } |
| 50 | + |
| 51 | + void validate(WebEidAuthToken token, X509Certificate subjectCertificate) throws AuthTokenException { |
| 52 | + if (isNullOrEmpty(token.getUnverifiedSigningCertificate())) { |
| 53 | + throw new AuthTokenParseException("'unverifiedSigningCertificate' field is missing, null or empty for format 'web-eid:1.1'"); |
| 54 | + } |
| 55 | + |
| 56 | + if (token.getSupportedSignatureAlgorithms() == null || token.getSupportedSignatureAlgorithms().isEmpty()) { |
| 57 | + throw new AuthTokenParseException("'supportedSignatureAlgorithms' field is missing"); |
| 58 | + } |
| 59 | + |
| 60 | + validateSupportedSignatureAlgorithms(token.getSupportedSignatureAlgorithms()); |
| 61 | + |
| 62 | + final X509Certificate signingCertificate = CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedSigningCertificate()); |
| 63 | + |
| 64 | + if (!subjectCertificate.getSubjectX500Principal().equals(signingCertificate.getSubjectX500Principal())) { |
| 65 | + throw new AuthTokenParseException("Signing certificate subject does not match authentication certificate subject"); |
| 66 | + } |
| 67 | + |
| 68 | + simpleSubjectCertificateValidators.executeFor(signingCertificate); |
| 69 | + certTrustValidators.executeFor(signingCertificate); |
| 70 | + } |
| 71 | + |
| 72 | + private static void validateSupportedSignatureAlgorithms(List<SupportedSignatureAlgorithm> algorithms) throws AuthTokenParseException { |
| 73 | + boolean hasInvalid = algorithms.stream().anyMatch(supportedSignatureAlgorithm -> |
| 74 | + !isValidCryptoAlgorithm(supportedSignatureAlgorithm.getCryptoAlgorithm()) |
| 75 | + || !isValidHashFunction(supportedSignatureAlgorithm.getHashFunction()) |
| 76 | + || !isValidPaddingScheme(supportedSignatureAlgorithm.getPaddingScheme()) |
| 77 | + ); |
| 78 | + |
| 79 | + if (hasInvalid) { |
| 80 | + throw new AuthTokenParseException("Unsupported signature algorithm"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean isValidCryptoAlgorithm(String value) { |
| 85 | + return "RSA".equals(value) || "ECC".equals(value); |
| 86 | + } |
| 87 | + |
| 88 | + private static boolean isValidHashFunction(String value) { |
| 89 | + return Set.of( |
| 90 | + "SHA-224", "SHA-256", "SHA-384", "SHA-512", |
| 91 | + "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" |
| 92 | + ).contains(value); |
| 93 | + } |
| 94 | + |
| 95 | + private static boolean isValidPaddingScheme(String value) { |
| 96 | + return "NONE".equals(value) || "PKCS1.5".equals(value) || "PSS".equals(value); |
| 97 | + } |
| 98 | +} |
0 commit comments