|
| 1 | +package com.oviva.telematik.vau.epa4all.client.authz.internal; |
| 2 | + |
| 3 | +import com.nimbusds.jose.JOSEException; |
| 4 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 5 | +import com.nimbusds.jwt.SignedJWT; |
| 6 | +import com.nimbusds.jwt.proc.BadJWTException; |
| 7 | +import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier; |
| 8 | +import com.oviva.telematik.vau.epa4all.client.authz.AuthorizationException; |
| 9 | +import com.oviva.telematik.vau.epa4all.client.authz.internal.jose.BrainpoolJwsVerifier; |
| 10 | +import com.oviva.telematik.vau.epa4all.client.authz.internal.jose.CertificateUtil; |
| 11 | +import java.security.*; |
| 12 | +import java.security.cert.*; |
| 13 | +import java.security.interfaces.ECPublicKey; |
| 14 | +import java.text.ParseException; |
| 15 | +import java.util.Set; |
| 16 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 17 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +public class OidcDiscoveryValidatorImpl implements OidcClient.DiscoveryValidator { |
| 22 | + |
| 23 | + // https://gemspec.gematik.de/docs/gemSpec/gemSpec_OID/gemSpec_OID_V3.23.0/#GS-A_4446-17 |
| 24 | + private static final ASN1ObjectIdentifier OID_IDPD = |
| 25 | + new ASN1ObjectIdentifier("1.2.276.0.76.4.260"); |
| 26 | + |
| 27 | + private static final Logger logger = LoggerFactory.getLogger(OidcDiscoveryValidatorImpl.class); |
| 28 | + |
| 29 | + private final KeyStore trustStore; |
| 30 | + |
| 31 | + public OidcDiscoveryValidatorImpl(KeyStore trustStore) { |
| 32 | + this.trustStore = trustStore; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void validate(SignedJWT jwt) { |
| 37 | + |
| 38 | + verifySignature(jwt); |
| 39 | + verifyClaims(jwt); |
| 40 | + } |
| 41 | + |
| 42 | + private void verifySignature(SignedJWT jwt) { |
| 43 | + |
| 44 | + var cert = signingCertificcate(jwt); |
| 45 | + verifyTrustChainAgainstRoot(cert); |
| 46 | + verifyRole(cert); |
| 47 | + verifySignature(jwt, cert); |
| 48 | + } |
| 49 | + |
| 50 | + private X509Certificate signingCertificcate(SignedJWT jwt) { |
| 51 | + |
| 52 | + var x5cRaw = |
| 53 | + jwt.getHeader().getX509CertChain().stream() |
| 54 | + // the first one MUST be the one used for signing |
| 55 | + // https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6 |
| 56 | + .findFirst() |
| 57 | + .orElseThrow( |
| 58 | + () -> |
| 59 | + new AuthorizationException( |
| 60 | + "OIDC discovery document 'x5c' header claim is missing")); |
| 61 | + try { |
| 62 | + return CertificateUtil.parseDer(x5cRaw.decode()); |
| 63 | + } catch (CertificateException e) { |
| 64 | + throw new AuthorizationException( |
| 65 | + "Failed to parse OIDC discovery document x509 header claim: " + e.getMessage(), e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void verifyTrustChainAgainstRoot(X509Certificate endUserCertificate) { |
| 70 | + |
| 71 | + try { |
| 72 | + |
| 73 | + var target = new X509CertSelector(); |
| 74 | + target.setCertificate(endUserCertificate); |
| 75 | + |
| 76 | + var params = new PKIXBuilderParameters(trustStore, target); |
| 77 | + |
| 78 | + // there are no CRLs to be found |
| 79 | + params.setRevocationEnabled(false); |
| 80 | + |
| 81 | + var builder = CertPathBuilder.getInstance("PKIX", BouncyCastleProvider.PROVIDER_NAME); |
| 82 | + |
| 83 | + var result = (PKIXCertPathBuilderResult) builder.build(params); |
| 84 | + logger.atDebug().log( |
| 85 | + "certificate '{}' verified with trust anchor: '{}'", |
| 86 | + endUserCertificate.getSubjectX500Principal().getName(), |
| 87 | + result.getTrustAnchor().getTrustedCert().getSubjectX500Principal().getName()); |
| 88 | + |
| 89 | + } catch (CertPathBuilderException |
| 90 | + | NoSuchAlgorithmException |
| 91 | + | InvalidAlgorithmParameterException e) { |
| 92 | + var name = endUserCertificate.getSubjectX500Principal().getName(); |
| 93 | + throw new AuthorizationException( |
| 94 | + "failed to validate IDP discovery document signing certificate, bad certificate: " + name, |
| 95 | + e); |
| 96 | + } catch (NoSuchProviderException | KeyStoreException e) { |
| 97 | + throw new AuthorizationException("unexpected crypto exception", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void verifyRole(X509Certificate trustedSigningCertificate) { |
| 102 | + var oid = |
| 103 | + CertificateUtil.getProfessionOid(trustedSigningCertificate) |
| 104 | + .orElseThrow( |
| 105 | + () -> new AuthorizationException("missing profession OID for IDP certificate")); |
| 106 | + if (!oid.equals(OID_IDPD)) { |
| 107 | + throw new AuthorizationException("expected OID %s, got %s".formatted(OID_IDPD, oid)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void verifySignature(SignedJWT jwt, X509Certificate trustedSigningCertificate) { |
| 112 | + var verifier = new BrainpoolJwsVerifier((ECPublicKey) trustedSigningCertificate.getPublicKey()); |
| 113 | + try { |
| 114 | + if (!verifier.verify(jwt.getHeader(), jwt.getSigningInput(), jwt.getSignature())) { |
| 115 | + throw new AuthorizationException("bad signature"); |
| 116 | + } |
| 117 | + } catch (JOSEException e) { |
| 118 | + throw new AuthorizationException("failed to verify signature", e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private void verifyClaims(SignedJWT jwt) { |
| 123 | + |
| 124 | + var claims = parseClaims(jwt); |
| 125 | + var claimsVerifier = |
| 126 | + new DefaultJWTClaimsVerifier<>( |
| 127 | + null, Set.of("iat", "exp", "issuer", "uri_puk_idp_enc", "uri_puk_idp_sig", "jwks_uri")); |
| 128 | + try { |
| 129 | + claimsVerifier.verify(claims, null); |
| 130 | + } catch (BadJWTException e) { |
| 131 | + throw new AuthorizationException("Bad discovery document", e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private JWTClaimsSet parseClaims(SignedJWT jwt) { |
| 136 | + try { |
| 137 | + return jwt.getJWTClaimsSet(); |
| 138 | + } catch (ParseException e) { |
| 139 | + throw new AuthorizationException("Failed to parse JWT claims", e); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments