-
Notifications
You must be signed in to change notification settings - Fork 2
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
rework key attestation key comparison #11
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 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 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
jdk.version=17 | ||
artifactVersion=2.1.0 | ||
artifactVersion=2.1.1 | ||
androidAttestationVersion=1.6.0 |
Submodule warden-roboto
updated
from a7a5b3 to 0af7bd
This file contains 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 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 |
---|---|---|
|
@@ -4,6 +4,8 @@ import at.asitplus.attestation.AttestationException | |
import at.asitplus.attestation.IOSAttestationConfiguration.AppData | ||
import at.asitplus.attestation.android.* | ||
import at.asitplus.attestation.android.exceptions.AttestationValueException | ||
import at.asitplus.signum.indispensable.CryptoPublicKey | ||
import at.asitplus.signum.indispensable.fromJcaPublicKey | ||
import ch.veehait.devicecheck.appattest.assertion.Assertion | ||
import ch.veehait.devicecheck.appattest.attestation.ValidatedAttestation | ||
import com.google.android.attestation.AttestationApplicationId | ||
|
@@ -12,7 +14,6 @@ import net.swiftzer.semver.SemVer | |
import org.slf4j.LoggerFactory | ||
import java.security.PublicKey | ||
import java.security.cert.X509Certificate | ||
import java.security.interfaces.ECPublicKey | ||
import java.util.* | ||
import kotlin.jvm.optionals.getOrNull | ||
import at.asitplus.attestation.AttestationException as AttException | ||
|
@@ -256,46 +257,69 @@ abstract class AttestationService { | |
attestationProof: List<ByteArray>, | ||
expectedChallenge: ByteArray, | ||
keyToBeAttested: T | ||
): KeyAttestation<T> = | ||
when (val firstTry = verifyAttestation(attestationProof, expectedChallenge, keyToBeAttested.encoded)) { | ||
is AttestationResult.Android -> { | ||
if (keyToBeAttested.encoded contentEquals firstTry.attestationCertificate.publicKey.encoded) KeyAttestation( | ||
keyToBeAttested, | ||
firstTry | ||
) | ||
else { | ||
("Android attestation failed: keyToBeAttested (${keyToBeAttested.encoded.encodeBase64()}) does not match " + | ||
"key from attestation certificate: ${firstTry.attestationCertificate.publicKey.encoded.encodeBase64()}").let { | ||
KeyAttestation( | ||
null, | ||
AttestationResult.Error( | ||
explanation = it, | ||
cause = AttException.Content.Android( | ||
it, | ||
AttestationValueException( | ||
it, | ||
cause = null, | ||
reason = AttestationValueException.Reason.APP_UNEXPECTED | ||
) | ||
) | ||
) | ||
) | ||
): KeyAttestation<T> { | ||
when (val firstTry = verifyAttestation( | ||
attestationProof, | ||
expectedChallenge, | ||
keyToBeAttested.encoded | ||
)) { | ||
is AttestationResult.Android -> return processAndroidAttestationResult(keyToBeAttested, firstTry) | ||
|
||
is AttestationResult.Error -> { | ||
|
||
// try all different key encodings | ||
// not the most efficient way, but doing it like this won't involve any guesswork at all | ||
keyToBeAttested.transcodeToAllFormats().forEach { | ||
when (val secondTry = | ||
kotlin.runCatching { verifyAttestation(attestationProof, expectedChallenge, it) } | ||
.getOrElse { return KeyAttestation(null, firstTry) }) { | ||
|
||
is AttestationResult.Android -> | ||
throw logicalError(keyToBeAttested, attestationProof, expectedChallenge) | ||
|
||
is AttestationResult.Error -> {} //try again, IOS could have encoded it differently | ||
|
||
//if this works, perfect! | ||
is AttestationResult.IOS -> return KeyAttestation(keyToBeAttested, secondTry) | ||
} | ||
} | ||
//if no encoding works, then it should just fail | ||
return KeyAttestation(null, firstTry) | ||
} | ||
|
||
is AttestationResult.Error -> when (val secondTry = | ||
kotlin.runCatching { | ||
verifyAttestation(attestationProof, expectedChallenge, (keyToBeAttested as ECPublicKey).toAnsi()) | ||
}.getOrElse { return KeyAttestation(null, firstTry) }) { | ||
is AttestationResult.Android -> throw RuntimeException("WTF?") | ||
is AttestationResult.Error -> KeyAttestation(null, firstTry) | ||
is AttestationResult.IOS -> KeyAttestation(keyToBeAttested, secondTry) | ||
} | ||
is AttestationResult.IOS -> return KeyAttestation(keyToBeAttested, firstTry) | ||
} | ||
} | ||
|
||
is AttestationResult.IOS -> KeyAttestation(keyToBeAttested, firstTry) | ||
private fun <T : PublicKey> processAndroidAttestationResult( | ||
keyToBeAttested: T, | ||
firstTry: AttestationResult.Android | ||
): KeyAttestation<T> = | ||
if (CryptoPublicKey.fromJcaPublicKey(keyToBeAttested) == CryptoPublicKey.fromJcaPublicKey( | ||
firstTry.attestationCertificate.publicKey | ||
) | ||
) KeyAttestation(keyToBeAttested, firstTry) | ||
else { | ||
("Android attestation failed: keyToBeAttested (${keyToBeAttested.encoded.encodeBase64()}) does not match " + | ||
"key from attestation certificate: ${firstTry.attestationCertificate.publicKey.encoded.encodeBase64()}").let { | ||
KeyAttestation( | ||
null, | ||
AttestationResult.Error( | ||
explanation = it, | ||
cause = AttException.Content.Android( | ||
it, | ||
AttestationValueException( | ||
it, | ||
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. Much better, but this still seems off, because not every parameter is using the parameter name, and it takes up 14 lines to create the result |
||
cause = null, | ||
reason = AttestationValueException.Reason.APP_UNEXPECTED | ||
) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
|
||
/** Same as [verifyKeyAttestation], but taking an encoded (either ANSI X9.63 or DER) publix key as a byte array | ||
* @see verifyKeyAttestation | ||
*/ | ||
|
@@ -435,6 +459,22 @@ sealed class AttestationResult { | |
}" | ||
} | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Android) return false | ||
|
||
if (attestationCertificateChain.map { it.encoded.encodeBase64() } != other.attestationCertificateChain.map { it.encoded.encodeBase64() }) return false | ||
if (androidDetails != other.androidDetails) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = attestationCertificateChain.map { it.encoded.contentHashCode() }.hashCode() | ||
result = 31 * result + androidDetails.hashCode() | ||
return result | ||
} | ||
} | ||
|
||
|
||
|
@@ -466,6 +506,25 @@ sealed class AttestationResult { | |
override val iosDetails = "NOOP" | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is IOS) return false | ||
|
||
if (clientData != null) { | ||
if (other.clientData == null) return false | ||
if (!clientData.contentEquals(other.clientData)) return false | ||
} else if (other.clientData != null) return false | ||
if (iosDetails != other.iosDetails) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = clientData?.contentHashCode() ?: 0 | ||
result = 31 * result + iosDetails.hashCode() | ||
return result | ||
} | ||
|
||
/** | ||
* Represents an attestation verification failure. Always contains an [explanation] about what went wrong. | ||
*/ | ||
|
@@ -499,6 +558,24 @@ data class KeyAttestation<T : PublicKey> internal constructor( | |
else { | ||
onError(details as AttestationResult.Error) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is KeyAttestation<*>) return false | ||
|
||
if (!attestedPublicKey?.encoded.contentEquals(other.attestedPublicKey?.encoded)) return false | ||
if (details != other.details) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = attestedPublicKey?.encoded?.contentHashCode() ?: 0 | ||
result = 31 * result + details.hashCode() | ||
return result | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
|
This file contains 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 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 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 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.
would also refactor this, because it's not clear what's compared to what, and the
if
block has no curly braces, but theelse
block does, although it's also only one statement