diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverter.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverter.java new file mode 100644 index 000000000..ed84307de --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverter.java @@ -0,0 +1,84 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ +package org.whispersystems.textsecuregcm.storage.devicecheck; + +import com.webauthn4j.appattest.DeviceCheckManager; +import com.webauthn4j.appattest.data.DCAssertion; +import com.webauthn4j.appattest.data.DCAssertionData; +import com.webauthn4j.appattest.data.DCAssertionRequest; +import com.webauthn4j.converter.AuthenticatorDataConverter; +import com.webauthn4j.converter.exception.DataConversionException; +import com.webauthn4j.converter.util.ObjectConverter; +import com.webauthn4j.data.attestation.authenticator.AuthenticatorData; +import com.webauthn4j.data.extension.authenticator.AuthenticationExtensionAuthenticatorOutput; +import tools.jackson.core.JacksonException; +import java.util.Arrays; + +/// Parses Apple App Attest assertions +/// +/// App Attest assertions since iOS 27 include an extensions map and don't include attested credential data. They +/// set the AT flag which indicates data, so a standards-compliant parser barfs because there's an extensions map where +/// the AT data should/ be. +/// +/// If Apple fixes their headers or webauthn4j adopts some workaround, this class should be removed +/// +/// [webauthn4j #1497](https://github.com/webauthn4j/webauthn4j/issues/1497) +/// [webauthn4j #1518](https://github.com/webauthn4j/webauthn4j/pull/1518) +class AppAttestAssertionConverter { + + // https://www.w3.org/TR/webauthn/#sctn-authenticator-data + // rpIdHash(32) || flags(1) || signCount(4) || attestedCredentialData(variable) || extensions(variable) + private static final int FLAGS_OFFSET = 32; + private static final int HEADER_LENGTH = 37; + + // https://www.rfc-editor.org/info/rfc8949/#section-3.1:~:text=the%20CBOR%20Encoding-,3.1%20Major%20Types,-3.2%20Indefinite%20Lengths + // CBOR major type 5 (map), covering both definite and indefinite length maps + private static final int CBOR_MAJOR_TYPE_MASK = 0xe0; + private static final int CBOR_MAP_MAJOR_TYPE = 0xa0; + + private static final ObjectConverter OBJECT_CONVERTER = DeviceCheckManager.createObjectConverter(); + private static final AuthenticatorDataConverter AUTHENTICATOR_DATA_CONVERTER = new AuthenticatorDataConverter(OBJECT_CONVERTER); + + /// Parse an assertion request that potentially has misconfigured FLAGS. The returned data must still be verified. + static DCAssertionData parseAppleAssertion(final DCAssertionRequest request) throws DataConversionException { + final DCAssertion assertion = OBJECT_CONVERTER.getCborMapper().readValue(request.getAssertion(), DCAssertion.class); + final byte[] authenticatorDataBytes = assertion == null ? null : assertion.getAuthenticatorData(); + final AuthenticatorData authenticatorData = + authenticatorDataBytes == null ? null : convertAuthenticatorData(authenticatorDataBytes); + return new DCAssertionData(request.getKeyId(), authenticatorData, authenticatorDataBytes, + request.getClientDataHash(), assertion == null ? null : assertion.getSignature()); + } + + private static AuthenticatorData convertAuthenticatorData(final byte[] original) { + if (!hasAppleAssertionExtensions(original)) { + return AUTHENTICATOR_DATA_CONVERTER.convert(original); + } + + final AuthenticatorData parsed; + try { + // Create a copy just for parsing and set the flags as Apple should have set them + final byte[] modified = Arrays.copyOf(original, original.length); + modified[FLAGS_OFFSET] = (byte) ((original[FLAGS_OFFSET] & ~AuthenticatorData.BIT_AT) | AuthenticatorData.BIT_ED); + parsed = AUTHENTICATOR_DATA_CONVERTER.convert(modified); + } catch (final JacksonException _) { + // Jackson error messages can include the (untrusted) extension contents + throw new DataConversionException("Invalid App Attest assertion extension data"); + } + + // Return the parsed structures but keep the original (incorrect) flags, so signature validation works + return new AuthenticatorData<>(parsed.getRpIdHash(), original[FLAGS_OFFSET], parsed.getSignCount(), + parsed.getExtensions()); + } + + private static boolean hasAppleAssertionExtensions(final byte[] data) { + // If we don't have AT or Extensions at all, webauthn can handle them being incorrectly set without any conversion + return data.length > HEADER_LENGTH + // If the data correctly sets the AT flag, webauthn can also handle it + && AuthenticatorData.checkFlagAT(data[FLAGS_OFFSET]) + // If the first byte after the header looks like an extension, this is the special case. Valid assertions + // shouldn't have attestedCredentialData anyway, and if it did it should be a CBOR string + && (data[HEADER_LENGTH] & CBOR_MAJOR_TYPE_MASK) == CBOR_MAP_MAJOR_TYPE; + } +} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppleDeviceCheckManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppleDeviceCheckManager.java index 442c47a00..6c7000b95 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppleDeviceCheckManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppleDeviceCheckManager.java @@ -204,7 +204,7 @@ public class AppleDeviceCheckManager { new DCAssertionParameters(new DCServerProperty(teamId, bundleId, new DefaultChallenge(request)), appleDevice); try { - deviceCheckManager.validate(dcAssertionRequest, dcAssertionParameters); + deviceCheckManager.validate(AppAttestAssertionConverter.parseAppleAssertion(dcAssertionRequest), dcAssertionParameters); } catch (MaliciousCounterValueException e) { // We will only accept assertions that have a sign count greater than the last assertion we saw. Step 5 here: // https://developer.apple.com/documentation/devicecheck/validating-apps-that-connect-to-your-server#Verify-the-assertion diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverterTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverterTest.java new file mode 100644 index 000000000..d80e4b3d3 --- /dev/null +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/devicecheck/AppAttestAssertionConverterTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2026 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ +package org.whispersystems.textsecuregcm.storage.devicecheck; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import com.webauthn4j.appattest.DeviceCheckManager; +import com.webauthn4j.appattest.data.DCAssertionData; +import com.webauthn4j.appattest.data.DCAssertionRequest; +import com.webauthn4j.converter.exception.DataConversionException; +import com.webauthn4j.converter.util.ObjectConverter; +import com.webauthn4j.data.attestation.authenticator.AuthenticatorData; +import java.nio.ByteBuffer; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class AppAttestAssertionConverterTest { + + private static final ObjectConverter OBJECT_CONVERTER = DeviceCheckManager.createObjectConverter(); + + @ParameterizedTest + @ValueSource(bytes = {AuthenticatorData.BIT_AT, AuthenticatorData.BIT_ED, AuthenticatorData.BIT_AT | AuthenticatorData.BIT_ED}) + void parseBadAssertion(byte flags) { + final DCAssertionData data = AppAttestAssertionConverter.parseAppleAssertion( + new DCAssertionRequest(new byte[32], createAssertion(flags), new byte[32])); + + assertThat(data.getAuthenticatorDataBytes()).isEqualTo(createAuthenticatorData(flags)); + assertThat(data.getAuthenticatorData().getFlags()).isEqualTo(flags); + assertThat(data.getAuthenticatorData().getSignCount()).isEqualTo(32); + assertThat(data.getAuthenticatorData().getAttestedCredentialData()).isNull(); + assertThat(data.getAuthenticatorData().getExtensions().getKeys()) + .containsExactlyInAnyOrder("apple_bundle_version_01", "apple_validation_category_01"); + } + + // If this test fails, we have updated to a version of webauthn4j that handles misformatted attestations, and + // the helper class can be removed. + @Test + void webauthn4jRejectsIOS27Assertion() { + final DeviceCheckManager deviceCheckManager = new DeviceCheckManager(new AppleDeviceCheckTrustAnchor()); + final byte[] appAttestAssertion = createAssertion((byte) (AuthenticatorData.BIT_AT | AuthenticatorData.BIT_ED)); + assertThatExceptionOfType(DataConversionException.class).isThrownBy(() -> + deviceCheckManager.parse(new DCAssertionRequest(new byte[32], appAttestAssertion, new byte[32]))); + } + + /// An assertion that has extensions and no attestedCredentialData but potentially with mismatched flags + private static byte[] createAssertion(final byte flags) { + return OBJECT_CONVERTER.getCborMapper().writeValueAsBytes(Map.of( + "signature", new byte[64], + "authenticatorData", createAuthenticatorData(flags))); + } + + private static byte[] createAuthenticatorData(final byte flags) { + final byte[] extensions = OBJECT_CONVERTER.getCborMapper().writeValueAsBytes(Map.of( + "apple_bundle_version_01", "1", + "apple_validation_category_01", new byte[]{2, 0, 0, 0})); + + return ByteBuffer.allocate(37 + extensions.length) + .put(new byte[32]) // rpIdHash + .put((byte) flags) // flags + .putInt(32) // signCount + .put(extensions) + .array(); + } +}