This repository was archived by the owner on Nov 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Correct handling of ATTEST_KEYs. #33
Open
divegeek
wants to merge
2
commits into
google:master
Choose a base branch
from
divegeek:handle_attest_key
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import static com.google.android.attestation.Constants.KEYMASTER_SECURITY_LEVEL_INDEX; | ||
| import static com.google.android.attestation.Constants.KEYMASTER_VERSION_INDEX; | ||
| import static com.google.android.attestation.Constants.KEY_DESCRIPTION_OID; | ||
| import static com.google.android.attestation.Constants.KM_KEY_PURPOSE_ATTEST_KEY; | ||
| import static com.google.android.attestation.Constants.KM_SECURITY_LEVEL_SOFTWARE; | ||
| import static com.google.android.attestation.Constants.KM_SECURITY_LEVEL_STRONG_BOX; | ||
| import static com.google.android.attestation.Constants.KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT; | ||
|
|
@@ -30,7 +31,10 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.bouncycastle.asn1.ASN1Encodable; | ||
| import org.bouncycastle.asn1.ASN1Enumerated; | ||
| import org.bouncycastle.asn1.ASN1InputStream; | ||
|
|
@@ -100,20 +104,51 @@ private ParsedAttestationRecord( | |
| public static ParsedAttestationRecord createParsedAttestationRecord(List<X509Certificate> certs) | ||
| throws IOException { | ||
|
|
||
| // Parse the attestation record that is closest to the root. This prevents an adversary from | ||
| // attesting an attestation record of their choice with an otherwise trusted chain using the | ||
| // following attack: | ||
| // 1) having the TEE attest a key under the adversary's control, | ||
| // 2) using that key to sign a new leaf certificate with an attestation extension that has their chosen attestation record, then | ||
| // 3) appending that certificate to the original certificate chain. | ||
| for (int i = certs.size() - 1; i >= 0; i--) { | ||
| byte[] attestationExtensionBytes = certs.get(i).getExtensionValue(KEY_DESCRIPTION_OID); | ||
| if (attestationExtensionBytes != null && attestationExtensionBytes.length != 0) { | ||
| return new ParsedAttestationRecord(extractAttestationSequence(attestationExtensionBytes)); | ||
| if (certs.isEmpty()) { | ||
| throw new IllegalArgumentException("Empty certificate list, couldn't get attestation data."); | ||
| } | ||
|
|
||
| // Leaf certificate should contain the attestation record we're looking for. | ||
| final ParsedAttestationRecord retval = extractParsedAttestationRecord(certs.get(0)); | ||
| if (retval == null) { | ||
| throw new IllegalArgumentException("Leaf certificate doesn't contain attestation data."); | ||
| } | ||
|
|
||
| // Make sure the rest of the chain is correct. After the leaf we can have zero or more | ||
| // attestations of ATTEST_KEYs, then the remaining certificates must not be attestations. So if | ||
| // we find any attestations after the first non-attestation, or if we find any attestations that | ||
| // aren't for ATTEST_KEYs, throw. | ||
| boolean foundNonAttestationCert = false; | ||
| for (X509Certificate cert : certs.stream().skip(1).collect(Collectors.toList())) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not certs.listIterator(1)? |
||
| final ParsedAttestationRecord record = extractParsedAttestationRecord(cert); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "record" to "intermediateAttestationRecord" |
||
| if (record == null) { | ||
| foundNonAttestationCert = true; | ||
| } else { | ||
| if (foundNonAttestationCert) { | ||
| throw new IllegalArgumentException( | ||
| "Found an attestation certificate after non-attestation cert(s). This should be impossible."); | ||
| } | ||
| if (!isAttestKeyAttestation(record)) { | ||
| throw new IllegalArgumentException("Found non-ATTEST_KEY attestation after leaf."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new IllegalArgumentException("Couldn't find the keystore attestation extension data."); | ||
| return retval; | ||
| } | ||
|
|
||
| private static ParsedAttestationRecord extractParsedAttestationRecord(X509Certificate cert) | ||
| throws IOException { | ||
| byte[] attestationExtensionBytes = cert.getExtensionValue(KEY_DESCRIPTION_OID); | ||
| if (attestationExtensionBytes == null) { | ||
| return null; | ||
| } | ||
| return new ParsedAttestationRecord(extractAttestationSequence(attestationExtensionBytes)); | ||
| } | ||
|
|
||
| private static boolean isAttestKeyAttestation(ParsedAttestationRecord record) { | ||
| Set<Integer> purposes = record.teeEnforced.purpose.orElse(Collections.emptySet()); | ||
| return purposes.size() == 1 && purposes.contains(KM_KEY_PURPOSE_ATTEST_KEY); | ||
| } | ||
|
|
||
| public static ParsedAttestationRecord create(ASN1Sequence extensionData) { | ||
|
|
||
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.
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.
Change variable from "retval" to "attestationRecord"