-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[OID4VCI] Harden key attestation x5c certificate validation #51042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
919c8e9
Harden OID4VCI key attestation x5c validation
forkimenjeckayang ad1f76a
address copilot reviews
forkimenjeckayang bdb915e
address copilot reviews
forkimenjeckayang 1698ea4
address copilot reviews
forkimenjeckayang fe1d19c
address review comments from Marek
forkimenjeckayang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
server-spi-private/src/main/java/org/keycloak/broker/provider/X509TrustMaterial.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.broker.provider; | ||
|
|
||
| import java.security.cert.X509Certificate; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * X.509 trust material and validation policy exposed by a trust-material identity provider. | ||
| * | ||
| * @param trustAnchors self-signed CA roots for one trust domain | ||
| * @param allowedExtendedKeyUsages extended-key-usage OIDs accepted for end-entity certificates | ||
| */ | ||
| public record X509TrustMaterial(Set<X509Certificate> trustAnchors, | ||
| List<String> allowedExtendedKeyUsages) { | ||
|
|
||
| public X509TrustMaterial { | ||
| trustAnchors = Set.copyOf(trustAnchors); | ||
| allowedExtendedKeyUsages = List.copyOf(allowedExtendedKeyUsages); | ||
| if (trustAnchors.isEmpty()) { | ||
| throw new IllegalArgumentException("At least one X.509 trust anchor is required"); | ||
| } | ||
| if (allowedExtendedKeyUsages.isEmpty()) { | ||
| throw new IllegalArgumentException("At least one attestation extended key usage is required"); | ||
| } | ||
| trustAnchors.forEach(X509TrustMaterial::validateTrustAnchor); | ||
| } | ||
|
|
||
| private static void validateTrustAnchor(X509Certificate certificate) { | ||
| if (certificate.getBasicConstraints() < 0) { | ||
| throw new IllegalArgumentException("X.509 trust anchors must be CA certificates"); | ||
| } | ||
|
|
||
| boolean[] keyUsage = certificate.getKeyUsage(); | ||
| if (keyUsage != null && (keyUsage.length <= 5 || !keyUsage[5])) { | ||
| throw new IllegalArgumentException("X.509 trust anchors must be valid for certificate signing"); | ||
| } | ||
|
|
||
| if (!certificate.getSubjectX500Principal().equals(certificate.getIssuerX500Principal())) { | ||
| throw new IllegalArgumentException("X.509 trust anchors must be self-issued root certificates"); | ||
| } | ||
|
|
||
| try { | ||
| certificate.verify(certificate.getPublicKey()); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("X.509 trust anchors must be self-signed root certificates", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When checking this, I am thinking about this corner-case scenario:
subject=my-ca, issuer=my-ca, which is self-signed by ca-keypair1x5celement with something like this (single leaf certificate, root certificate is omitted):subject=my-leaf, issuer=my-ca, which is signed by ca-keypair2In other words, the issuer from
x5cis same valuemy-caas the trusted CA certificate from identity provider configuration. However thex5cis signed by incorrect keypairca-keypair2(not by theca-keypair1from IDP config).Just thinking about possible attack, when attacker knows the name of configured CA, however attacker creates his own CA certificate of same name, but signed by his own keypair (ca-keypair2), which he would use to sign his own root certificate and leaf certificate attached.
Do you think that such scenario would be rejected? Might be good to have automated test for this though?