Skip to content

Commit 76df05a

Browse files
NFC-46 Add web-eid-1.1 token support
1 parent 2d8b399 commit 76df05a

18 files changed

+1206
-126
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.authtoken;
24+
25+
public class SupportedSignatureAlgorithm {
26+
private String cryptoAlgorithm;
27+
private String hashFunction;
28+
private String paddingScheme;
29+
30+
public String getCryptoAlgorithm() {
31+
return cryptoAlgorithm;
32+
}
33+
34+
public void setCryptoAlgorithm(String cryptoAlgorithm) {
35+
this.cryptoAlgorithm = cryptoAlgorithm;
36+
}
37+
38+
public String getHashFunction() {
39+
return hashFunction;
40+
}
41+
42+
public void setHashFunction(String hashFunction) {
43+
this.hashFunction = hashFunction;
44+
}
45+
46+
public String getPaddingScheme() {
47+
return paddingScheme;
48+
}
49+
50+
public void setPaddingScheme(String paddingScheme) {
51+
this.paddingScheme = paddingScheme;
52+
}
53+
}

src/main/java/eu/webeid/security/authtoken/WebEidAuthToken.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2626

27+
import java.util.List;
28+
2729
@JsonIgnoreProperties(ignoreUnknown = true)
2830
public class WebEidAuthToken {
2931

@@ -32,6 +34,9 @@ public class WebEidAuthToken {
3234
private String algorithm;
3335
private String format;
3436

37+
private String unverifiedSigningCertificate;
38+
private List<SupportedSignatureAlgorithm> supportedSignatureAlgorithms;
39+
3540
public String getUnverifiedCertificate() {
3641
return unverifiedCertificate;
3742
}
@@ -64,4 +69,19 @@ public void setFormat(String format) {
6469
this.format = format;
6570
}
6671

72+
public String getUnverifiedSigningCertificate() {
73+
return unverifiedSigningCertificate;
74+
}
75+
76+
public void setUnverifiedSigningCertificate(String unverifiedSigningCertificate) {
77+
this.unverifiedSigningCertificate = unverifiedSigningCertificate;
78+
}
79+
80+
public List<SupportedSignatureAlgorithm> getSupportedSignatureAlgorithms() {
81+
return supportedSignatureAlgorithms;
82+
}
83+
84+
public void setSupportedSignatureAlgorithms(List<SupportedSignatureAlgorithm> supportedSignatureAlgorithms) {
85+
this.supportedSignatureAlgorithms = supportedSignatureAlgorithms;
86+
}
6787
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.WebEidAuthToken;
26+
import eu.webeid.security.certificate.CertificateLoader;
27+
import eu.webeid.security.exceptions.AuthTokenException;
28+
import eu.webeid.security.exceptions.AuthTokenParseException;
29+
import eu.webeid.security.validator.certvalidators.SubjectCertificateValidatorBatch;
30+
import eu.webeid.security.validator.ocsp.OcspClient;
31+
import eu.webeid.security.validator.ocsp.OcspServiceProvider;
32+
33+
import java.security.cert.CertStore;
34+
import java.security.cert.TrustAnchor;
35+
import java.security.cert.X509Certificate;
36+
import java.util.Set;
37+
38+
class AuthTokenV1Validator implements AuthTokenValidator {
39+
40+
private static final String SUPPORTED_TOKEN_FORMAT_VERSION = "web-eid:1";
41+
42+
private final SubjectCertificateValidatorBatch simpleSubjectCertificateValidators;
43+
private final Set<TrustAnchor> trustedCACertificateAnchors;
44+
private final CertStore trustedCACertificateCertStore;
45+
private final AuthTokenSignatureValidator authTokenSignatureValidator;
46+
private final AuthTokenValidationConfiguration configuration;
47+
private final OcspClient ocspClient;
48+
private final OcspServiceProvider ocspServiceProvider;
49+
50+
public AuthTokenV1Validator(
51+
SubjectCertificateValidatorBatch simpleSubjectCertificateValidators,
52+
Set<TrustAnchor> trustedCACertificateAnchors,
53+
CertStore trustedCACertificateCertStore,
54+
AuthTokenSignatureValidator authTokenSignatureValidator,
55+
AuthTokenValidationConfiguration configuration,
56+
OcspClient ocspClient,
57+
OcspServiceProvider ocspServiceProvider
58+
) {
59+
this.simpleSubjectCertificateValidators = simpleSubjectCertificateValidators;
60+
this.trustedCACertificateAnchors = trustedCACertificateAnchors;
61+
this.trustedCACertificateCertStore = trustedCACertificateCertStore;
62+
this.authTokenSignatureValidator = authTokenSignatureValidator;
63+
this.configuration = configuration;
64+
this.ocspClient = ocspClient;
65+
this.ocspServiceProvider = ocspServiceProvider;
66+
}
67+
68+
@Override
69+
public boolean supports(String format) {
70+
return format != null && format.startsWith(SUPPORTED_TOKEN_FORMAT_VERSION);
71+
}
72+
73+
@Override
74+
public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException {
75+
if (token.getFormat() == null || token.getFormat().isBlank()) {
76+
throw new AuthTokenParseException("'format' field is missing");
77+
}
78+
if (!token.getFormat().startsWith(SUPPORTED_TOKEN_FORMAT_VERSION)) {
79+
throw new AuthTokenParseException("Only token format version '" + SUPPORTED_TOKEN_FORMAT_VERSION + "' is currently supported");
80+
}
81+
82+
if (token.getUnverifiedCertificate() == null || token.getUnverifiedCertificate().isEmpty()) {
83+
throw new AuthTokenParseException("'unverifiedCertificate' field is missing, null or empty");
84+
}
85+
86+
final X509Certificate subjectCertificate = CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedCertificate());
87+
88+
simpleSubjectCertificateValidators.executeFor(subjectCertificate);
89+
90+
SubjectCertificateValidatorBatch.forTrustValidation(
91+
configuration,
92+
trustedCACertificateAnchors,
93+
trustedCACertificateCertStore,
94+
ocspClient,
95+
ocspServiceProvider
96+
).executeFor(subjectCertificate);
97+
98+
// It is guaranteed that if the signature verification succeeds, then the origin and challenge
99+
// have been implicitly and correctly verified without the need to implement any additional checks.
100+
authTokenSignatureValidator.validate(
101+
token.getAlgorithm(),
102+
token.getSignature(),
103+
subjectCertificate.getPublicKey(),
104+
currentChallengeNonce
105+
);
106+
107+
return subjectCertificate;
108+
}
109+
}

src/main/java/eu/webeid/security/validator/AuthTokenValidationConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Collection<X509Certificate> getTrustedCACertificates() {
8787
return trustedCACertificates;
8888
}
8989

90-
boolean isUserCertificateRevocationCheckWithOcspEnabled() {
90+
public boolean isUserCertificateRevocationCheckWithOcspEnabled() {
9191
return isUserCertificateRevocationCheckWithOcspEnabled;
9292
}
9393

0 commit comments

Comments
 (0)