|
25 | 25 | import eu.webeid.security.authtoken.SupportedSignatureAlgorithm; |
26 | 26 | import eu.webeid.security.authtoken.WebEidAuthToken; |
27 | 27 | import eu.webeid.security.certificate.CertificateLoader; |
28 | | -import eu.webeid.security.certificate.UnverifiedSigningCertificate; |
| 28 | +import eu.webeid.security.authtoken.UnverifiedSigningCertificate; |
29 | 29 | import eu.webeid.security.exceptions.AuthTokenException; |
30 | 30 | import eu.webeid.security.exceptions.AuthTokenParseException; |
31 | 31 | import eu.webeid.security.exceptions.CertificateDecodingException; |
|
46 | 46 | import java.security.cert.CertificateNotYetValidException; |
47 | 47 | import java.security.cert.TrustAnchor; |
48 | 48 | import java.security.cert.X509Certificate; |
| 49 | +import java.util.ArrayList; |
49 | 50 | import java.util.Arrays; |
| 51 | +import java.util.Iterator; |
50 | 52 | import java.util.List; |
51 | 53 | import java.util.Set; |
52 | 54 |
|
@@ -91,50 +93,57 @@ protected String getSupportedFormatPrefix() { |
91 | 93 | @Override |
92 | 94 | public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException { |
93 | 95 | final X509Certificate subjectCertificate = validateV1(token, currentChallengeNonce); |
94 | | - final X509Certificate signingCertificate = validateSigningCertificatesExist(token); |
95 | | - validateSupportedSignatureAlgorithms(token.getUnverifiedSigningCertificates()); |
96 | | - validateSameSubject(subjectCertificate, signingCertificate); |
97 | | - validateSameIssuer(subjectCertificate, signingCertificate); |
98 | | - validateSigningCertificateValidity(signingCertificate); |
99 | | - validateKeyUsage(signingCertificate); |
| 96 | + final List<X509Certificate> signingCertificates = validateSigningCertificatesExist(token); |
| 97 | + final List<UnverifiedSigningCertificate> unverifiedSigningCertificates = token.getUnverifiedSigningCertificates(); |
| 98 | + Iterator<UnverifiedSigningCertificate> unverifiedIterator = unverifiedSigningCertificates.iterator(); |
| 99 | + for(X509Certificate signingCertificate : signingCertificates) { |
| 100 | + UnverifiedSigningCertificate unverifiedSigningCertificate = unverifiedIterator.next(); |
| 101 | + validateSupportedSignatureAlgorithms(unverifiedSigningCertificate); |
| 102 | + validateSameSubject(subjectCertificate, signingCertificate); |
| 103 | + validateSameIssuer(subjectCertificate, signingCertificate); |
| 104 | + validateSigningCertificateValidity(signingCertificate); |
| 105 | + validateKeyUsage(signingCertificate); |
| 106 | + } |
100 | 107 |
|
101 | 108 | return subjectCertificate; |
102 | 109 | } |
103 | 110 |
|
104 | | - private static void validateSupportedSignatureAlgorithms(List<UnverifiedSigningCertificate> unverifiedSigningCertificates) throws AuthTokenParseException { |
105 | | - for (UnverifiedSigningCertificate cert : unverifiedSigningCertificates) { |
106 | | - List<SupportedSignatureAlgorithm> algorithms = cert.getSupportedSignatureAlgorithms(); |
| 111 | + private static void validateSupportedSignatureAlgorithms(UnverifiedSigningCertificate cert) throws AuthTokenParseException { |
| 112 | + List<SupportedSignatureAlgorithm> algorithms = cert.getSupportedSignatureAlgorithms(); |
107 | 113 |
|
108 | | - if (algorithms == null || algorithms.isEmpty()) { |
109 | | - throw new AuthTokenParseException("'supportedSignatureAlgorithms' field is missing"); |
110 | | - } |
| 114 | + if (algorithms == null || algorithms.isEmpty()) { |
| 115 | + throw new AuthTokenParseException("'supportedSignatureAlgorithms' field is missing"); |
| 116 | + } |
111 | 117 |
|
112 | | - boolean hasInvalid = algorithms.stream().anyMatch(algorithm -> |
113 | | - !SUPPORTED_SIGNING_CRYPTO_ALGORITHMS.contains(algorithm.getCryptoAlgorithm()) || |
114 | | - !SUPPORTED_SIGNING_HASH_FUNCTIONS.contains(algorithm.getHashFunction()) || |
115 | | - !SUPPORTED_SIGNING_PADDING_SCHEMES.contains(algorithm.getPaddingScheme()) |
116 | | - ); |
| 118 | + boolean hasInvalid = algorithms.stream().anyMatch(algorithm -> |
| 119 | + !SUPPORTED_SIGNING_CRYPTO_ALGORITHMS.contains(algorithm.getCryptoAlgorithm()) || |
| 120 | + !SUPPORTED_SIGNING_HASH_FUNCTIONS.contains(algorithm.getHashFunction()) || |
| 121 | + !SUPPORTED_SIGNING_PADDING_SCHEMES.contains(algorithm.getPaddingScheme()) |
| 122 | + ); |
117 | 123 |
|
118 | | - if (hasInvalid) { |
119 | | - throw new AuthTokenParseException("Unsupported signature algorithm"); |
120 | | - } |
| 124 | + if (hasInvalid) { |
| 125 | + throw new AuthTokenParseException("Unsupported signature algorithm"); |
121 | 126 | } |
122 | 127 | } |
123 | 128 |
|
124 | | - private static X509Certificate validateSigningCertificatesExist(WebEidAuthToken token) throws AuthTokenParseException, CertificateDecodingException { |
| 129 | + private static List<X509Certificate> validateSigningCertificatesExist(WebEidAuthToken token) throws AuthTokenParseException, CertificateDecodingException { |
125 | 130 | List<UnverifiedSigningCertificate> signingCertificates = token.getUnverifiedSigningCertificates(); |
126 | 131 |
|
127 | 132 | if (signingCertificates == null || signingCertificates.isEmpty()) { |
128 | 133 | throw new AuthTokenParseException("'unverifiedSigningCertificates' field is missing, null or empty for format 'web-eid:1.1'"); |
129 | 134 | } |
130 | 135 |
|
131 | | - UnverifiedSigningCertificate signingCertificate = signingCertificates.get(0); |
| 136 | + List<X509Certificate> result = new ArrayList<>(); |
132 | 137 |
|
133 | | - if (signingCertificate == null || isNullOrEmpty(signingCertificate.getUnverifiedSigningCertificate())) { |
134 | | - throw new AuthTokenParseException("'unverifiedSigningCertificates' field is missing, null or empty for format 'web-eid:1.1'"); |
| 138 | + for (UnverifiedSigningCertificate certificate : signingCertificates) { |
| 139 | + if (certificate == null || isNullOrEmpty(certificate.getUnverifiedSigningCertificate())) { |
| 140 | + throw new AuthTokenParseException("'unverifiedSigningCertificates' field is missing, null or empty for format 'web-eid:1.1'"); |
| 141 | + } |
| 142 | + |
| 143 | + result.add(CertificateLoader.decodeCertificateFromBase64(certificate.getUnverifiedSigningCertificate())); |
135 | 144 | } |
136 | 145 |
|
137 | | - return CertificateLoader.decodeCertificateFromBase64(signingCertificate.getUnverifiedSigningCertificate()); |
| 146 | + return result; |
138 | 147 | } |
139 | 148 |
|
140 | 149 | private static void validateSameSubject(X509Certificate subjectCertificate, X509Certificate signingCertificate) |
|
0 commit comments