Skip to content

Commit ff6a0cf

Browse files
NFC-46 Add web-eid-1.1 token support
Signed-off-by: Sander Kondratjev <sander.kondratjev@nortal.com>
1 parent 2d8b399 commit ff6a0cf

21 files changed

+1336
-208
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: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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.CertificateExpiredException;
43+
import java.security.cert.CertificateNotYetValidException;
44+
import java.security.cert.TrustAnchor;
45+
import java.security.cert.X509Certificate;
46+
import java.util.Arrays;
47+
import java.util.List;
48+
import java.util.Set;
49+
50+
import static eu.webeid.security.util.Strings.isNullOrEmpty;
51+
52+
class AuthTokenV11Validator extends AuthTokenV1Validator implements AuthTokenVersionValidator {
53+
54+
private static final String V11_SUPPORTED_TOKEN_FORMAT_PREFIX = "web-eid:1.1";
55+
private static final Set<String> SUPPORTED_SIGNING_CRYPTO_ALGORITHMS = Set.of("ECC", "RSA");
56+
private static final Set<String> SUPPORTED_SIGNING_PADDING_SCHEMES = Set.of("NONE", "PKCS1.5", "PSS");
57+
private static final Set<String> SUPPORTED_SIGNING_HASH_FUNCTIONS = Set.of(
58+
"SHA-224", "SHA-256", "SHA-384", "SHA-512",
59+
"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"
60+
);
61+
private static final int KEY_USAGE_NON_REPUDIATION = 1;
62+
63+
public AuthTokenV11Validator(
64+
SubjectCertificateValidatorBatch simpleSubjectCertificateValidators,
65+
Set<TrustAnchor> trustedCACertificateAnchors,
66+
CertStore trustedCACertificateCertStore,
67+
AuthTokenSignatureValidator authTokenSignatureValidator,
68+
AuthTokenValidationConfiguration configuration,
69+
OcspClient ocspClient,
70+
OcspServiceProvider ocspServiceProvider
71+
) {
72+
super(
73+
simpleSubjectCertificateValidators,
74+
trustedCACertificateAnchors,
75+
trustedCACertificateCertStore,
76+
authTokenSignatureValidator,
77+
configuration,
78+
ocspClient,
79+
ocspServiceProvider
80+
);
81+
}
82+
83+
@Override
84+
public boolean supports(String format) {
85+
return format != null && format.startsWith(getSupportedFormatPrefix());
86+
}
87+
88+
@Override
89+
protected String getSupportedFormatPrefix() {
90+
return V11_SUPPORTED_TOKEN_FORMAT_PREFIX;
91+
}
92+
93+
@Override
94+
public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException {
95+
final X509Certificate subjectCertificate = validateV1(token, currentChallengeNonce);
96+
final X509Certificate signingCertificate = validateSigningCertificateExists(token);
97+
validateSupportedSignatureAlgorithms(token.getSupportedSignatureAlgorithms());
98+
validateSameSubject(subjectCertificate, signingCertificate);
99+
validateSameIssuer(subjectCertificate, signingCertificate);
100+
validateKeyUsage(signingCertificate);
101+
102+
return subjectCertificate;
103+
}
104+
105+
private static void validateSupportedSignatureAlgorithms(List<SupportedSignatureAlgorithm> algorithms) throws AuthTokenParseException {
106+
if (algorithms == null || algorithms.isEmpty()) {
107+
throw new AuthTokenParseException("'supportedSignatureAlgorithms' field is missing");
108+
}
109+
110+
boolean hasInvalid = algorithms.stream().anyMatch(supportedSignatureAlgorithm ->
111+
!SUPPORTED_SIGNING_CRYPTO_ALGORITHMS.contains(supportedSignatureAlgorithm.getCryptoAlgorithm()) ||
112+
!SUPPORTED_SIGNING_HASH_FUNCTIONS.contains(supportedSignatureAlgorithm.getHashFunction()) ||
113+
!SUPPORTED_SIGNING_PADDING_SCHEMES.contains(supportedSignatureAlgorithm.getPaddingScheme())
114+
);
115+
116+
if (hasInvalid) {
117+
throw new AuthTokenParseException("Unsupported signature algorithm");
118+
}
119+
}
120+
121+
private static X509Certificate validateSigningCertificateExists(WebEidAuthToken token) throws AuthTokenParseException, CertificateDecodingException {
122+
if (isNullOrEmpty(token.getUnverifiedSigningCertificate())) {
123+
throw new AuthTokenParseException("'unverifiedSigningCertificate' field is missing, null or empty for format 'web-eid:1.1'");
124+
}
125+
return CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedSigningCertificate());
126+
}
127+
128+
private static void validateSameSubject(X509Certificate subjectCertificate, X509Certificate signingCertificate)
129+
throws AuthTokenParseException {
130+
if (!subjectAndSigningCertificateSubjectsMatch(
131+
subjectCertificate.getSubjectX500Principal(),
132+
signingCertificate.getSubjectX500Principal())) {
133+
throw new AuthTokenParseException("Signing certificate subject does not match authentication certificate subject");
134+
}
135+
}
136+
137+
private static void validateSameIssuer(X509Certificate subjectCertificate, X509Certificate signingCertificate)
138+
throws AuthTokenParseException {
139+
byte[] subjectCertificateAuthorityKeyIdentifier = getAuthorityKeyIdentifier(subjectCertificate);
140+
byte[] signingCertificateAuthorityKeyIdentifier = getAuthorityKeyIdentifier(signingCertificate);
141+
142+
if (subjectCertificateAuthorityKeyIdentifier.length == 0
143+
|| signingCertificateAuthorityKeyIdentifier.length == 0
144+
|| !Arrays.equals(subjectCertificateAuthorityKeyIdentifier, signingCertificateAuthorityKeyIdentifier)) {
145+
throw new AuthTokenParseException(
146+
"Signing certificate is not issued by the same issuing authority as the authentication certificate");
147+
}
148+
}
149+
150+
private static void validateKeyUsage(X509Certificate signingCertificate) throws AuthTokenParseException {
151+
try {
152+
signingCertificate.checkValidity();
153+
} catch (CertificateExpiredException | CertificateNotYetValidException e) {
154+
throw new AuthTokenParseException("Signing certificate is not valid: " + e.getMessage(), e);
155+
}
156+
157+
boolean[] keyUsage = signingCertificate.getKeyUsage();
158+
if (keyUsage == null || keyUsage.length <= KEY_USAGE_NON_REPUDIATION || !keyUsage[KEY_USAGE_NON_REPUDIATION]) {
159+
throw new AuthTokenParseException("Signing certificate key usage extension missing or does not contain non-repudiation bit required for digital signatures");
160+
}
161+
}
162+
163+
private static boolean subjectAndSigningCertificateSubjectsMatch(
164+
X500Principal authenticationCertificateSubject,
165+
X500Principal signingCertificateSubject) {
166+
X500Name authName = X500Name.getInstance(RFC4519Style.INSTANCE, authenticationCertificateSubject.getEncoded());
167+
X500Name signName = X500Name.getInstance(RFC4519Style.INSTANCE, signingCertificateSubject.getEncoded());
168+
return authName.equals(signName);
169+
}
170+
171+
private static byte[] getAuthorityKeyIdentifier(X509Certificate certificate) throws AuthTokenParseException {
172+
try {
173+
byte[] authorityKeyIdentifierExtension = certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId());
174+
if (authorityKeyIdentifierExtension == null) {
175+
return new byte[0];
176+
}
177+
AuthorityKeyIdentifier authorityKeyIdentifier =
178+
AuthorityKeyIdentifier.getInstance(JcaX509ExtensionUtils.parseExtensionValue(authorityKeyIdentifierExtension));
179+
return authorityKeyIdentifier.getKeyIdentifier();
180+
} catch (Exception e) {
181+
throw new AuthTokenParseException("Failed to parse Authority Key Identifier", e);
182+
}
183+
}
184+
185+
protected X509Certificate validateV1(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException {
186+
return super.validate(token, currentChallengeNonce);
187+
}
188+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 AuthTokenVersionValidator {
39+
40+
protected static final String V1_SUPPORTED_TOKEN_FORMAT_PREFIX = "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(getSupportedFormatPrefix());
71+
}
72+
73+
protected String getSupportedFormatPrefix() {
74+
return V1_SUPPORTED_TOKEN_FORMAT_PREFIX;
75+
}
76+
77+
@Override
78+
public X509Certificate validate(WebEidAuthToken token, String currentChallengeNonce) throws AuthTokenException {
79+
if (token.getFormat() == null || token.getFormat().isBlank()) {
80+
throw new AuthTokenParseException("'format' field is missing");
81+
}
82+
83+
if (token.getUnverifiedCertificate() == null || token.getUnverifiedCertificate().isEmpty()) {
84+
throw new AuthTokenParseException("'unverifiedCertificate' field is missing, null or empty");
85+
}
86+
87+
final X509Certificate subjectCertificate = CertificateLoader.decodeCertificateFromBase64(token.getUnverifiedCertificate());
88+
89+
simpleSubjectCertificateValidators.executeFor(subjectCertificate);
90+
91+
SubjectCertificateValidatorBatch.forTrustValidation(
92+
configuration,
93+
trustedCACertificateAnchors,
94+
trustedCACertificateCertStore,
95+
ocspClient,
96+
ocspServiceProvider
97+
).executeFor(subjectCertificate);
98+
99+
// It is guaranteed that if the signature verification succeeds, then the origin and challenge
100+
// have been implicitly and correctly verified without the need to implement any additional checks.
101+
authTokenSignatureValidator.validate(
102+
token.getAlgorithm(),
103+
token.getSignature(),
104+
subjectCertificate.getPublicKey(),
105+
currentChallengeNonce
106+
);
107+
108+
return subjectCertificate;
109+
}
110+
}

0 commit comments

Comments
 (0)