diff --git a/res/values/strings_ext.xml b/res/values/strings_ext.xml
index 0ac5bf5656d..52caca80bf5 100644
--- a/res/values/strings_ext.xml
+++ b/res/values/strings_ext.xml
@@ -371,4 +371,26 @@ For compatibility, this app can still see clipboard metadata, such as whether an
"The clipboard access setting, blocked access message setting, and temporary Paste access are shared among these apps:
%1$s"
+ Device attestation
+ Last server verdict
+ Not verified yet. Tap Verify.
+ %1$s (%2$s) at %3$s
+ Verify
+ Ask the MAOS server for a fresh challenge for your session and answer it with a one-time hardware attestation. The verdict shown here is the server's, not this device's.
+ Session code
+ Start at maproj.org/attest, then enter the session code it shows here. Long-press the field to paste.
+ Scan QR code
+ Scan the code shown at maproj.org/attest, or enter the session code below instead.
+ That is not an attestation code. Try again.
+ Enter the session code from maproj.org/attest first.
+ Verifying...
+ Server verdict: verified
+ Server verdict: failed (%1$s)
+ Your verification
+ See your verification: maproj.org/attest — code %1$s\n%2$s
+ How this works
+ A hardware-backed key (StrongBox where available, otherwise the trusted execution environment) is generated with a one-time server challenge. Its certificate chain is sent to api.maproj.org, which verifies it up to Google's hardware attestation root and checks that verified boot is Verified and the device is locked. No approval is needed: any device with valid hardware evidence gets a verdict. After verifying, see the result on the page that gave you the session code; this screen also shows a receipt code for reopening it. This page never grades itself.
+ Device attestation
+ Server-verified hardware attestation
+
diff --git a/res/xml/maos_attestation_settings.xml b/res/xml/maos_attestation_settings.xml
new file mode 100644
index 00000000000..622c3177b5f
--- /dev/null
+++ b/res/xml/maos_attestation_settings.xml
@@ -0,0 +1,45 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/res/xml/security_dashboard_settings.xml b/res/xml/security_dashboard_settings.xml
index 3bb7d69bf0d..7f29614bcfc 100644
--- a/res/xml/security_dashboard_settings.xml
+++ b/res/xml/security_dashboard_settings.xml
@@ -62,6 +62,18 @@
settings:keywords="@string/keywords_biometric_settings" />
+
+
+
= 400 ? conn.getErrorStream() : conn.getInputStream();
+ String text = readAll(in);
+ if (code >= 400) {
+ String reason = new JSONObject(text).optString("reason", "http_" + code);
+ throw new IllegalStateException(reason);
+ }
+ return new JSONObject(text);
+ }
+
+ private static String readAll(InputStream in) throws Exception {
+ StringBuilder sb = new StringBuilder();
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(in, StandardCharsets.UTF_8))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ sb.append(line);
+ }
+ }
+ return sb.toString();
+ }
+
+ private static byte[] hexToBytes(String hex) {
+ byte[] out = new byte[hex.length() / 2];
+ for (int i = 0; i < out.length; i++) {
+ out[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
+ }
+ return out;
+ }
+}
diff --git a/src/com/android/settings/security/MaosAttestationFragment.java b/src/com/android/settings/security/MaosAttestationFragment.java
new file mode 100644
index 00000000000..0eefb3e8939
--- /dev/null
+++ b/src/com/android/settings/security/MaosAttestationFragment.java
@@ -0,0 +1,202 @@
+/*
+ * MAOS "Device attestation" settings page (one-time, the Auditor model).
+ *
+ * Deliberately minimal: a status line (the last server verdict, stored
+ * from the server's response), a single "Verify" action, and — once a
+ * verification has completed — the code and URL for viewing that one
+ * result at maproj.org/attest. There is no periodic or on-boot
+ * verification, no enrollment, and no background work of any kind.
+ */
+package com.android.settings.security;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Bundle;
+
+import androidx.preference.EditTextPreference;
+import androidx.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settingslib.search.SearchIndexable;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.concurrent.Executors;
+
+@SearchIndexable
+public class MaosAttestationFragment extends DashboardFragment {
+
+ private static final String TAG = "MaosAttestation";
+ private static final String KEY_STATUS = "maos_attestation_status";
+ private static final String KEY_SESSION = "maos_attestation_session_code";
+ private static final String KEY_VERIFY = "maos_attestation_verify";
+ private static final String KEY_RESULT = "maos_attestation_result";
+
+ @Override
+ public int getMetricsCategory() {
+ return SettingsEnums.SECURITY;
+ }
+
+ @Override
+ protected String getLogTag() {
+ return TAG;
+ }
+
+ @Override
+ protected int getPreferenceScreenResId() {
+ return R.xml.maos_attestation_settings;
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ updateStatus();
+ }
+
+ @Override
+ public boolean onPreferenceTreeClick(Preference preference) {
+ if (KEY_VERIFY.equals(preference.getKey())) {
+ runVerify(preference);
+ return true;
+ }
+ if (KEY_RESULT.equals(preference.getKey())) {
+ openVerification();
+ return true;
+ }
+ return super.onPreferenceTreeClick(preference);
+ }
+
+ private void openVerification() {
+ Context context = getContext();
+ if (context == null) {
+ return;
+ }
+ SharedPreferences prefs = context.getSharedPreferences(
+ MaosAttestationClient.PREFS, Context.MODE_PRIVATE);
+ String url = prefs.getString(MaosAttestationClient.KEY_LAST_URL, null);
+ if (url == null || url.isEmpty()) {
+ return;
+ }
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+ try {
+ startActivity(intent);
+ } catch (Exception e) {
+ // No browser to open it with: the code and URL stay readable
+ // on this screen, which is the point of showing them.
+ }
+ }
+
+ private void runVerify(Preference verifyPref) {
+ Context context = getContext();
+ if (context == null) {
+ return;
+ }
+ String sessionCode = sessionCode();
+ if (sessionCode.isEmpty()) {
+ verifyPref.setSummary(R.string.maos_attestation_session_required);
+ return;
+ }
+ verifyPref.setEnabled(false);
+ verifyPref.setSummary(R.string.maos_attestation_verifying);
+ Executors.newSingleThreadExecutor().execute(() -> {
+ String message;
+ try {
+ MaosAttestationClient.Result result =
+ new MaosAttestationClient(context).verifyNow(sessionCode);
+ message = result.pass
+ ? getString(R.string.maos_attestation_passed)
+ : getString(R.string.maos_attestation_failed, result.reason);
+ } catch (Exception e) {
+ message = getString(R.string.maos_attestation_failed,
+ String.valueOf(e.getMessage()));
+ }
+ String done = message;
+ if (getActivity() != null) {
+ getActivity().runOnUiThread(() -> {
+ verifyPref.setEnabled(true);
+ verifyPref.setSummary(done);
+ updateStatus();
+ });
+ }
+ });
+ }
+
+ private void updateStatus() {
+ Preference status = findPreference(KEY_STATUS);
+ Context context = getContext();
+ if (status == null || context == null) {
+ return;
+ }
+ SharedPreferences prefs = context.getSharedPreferences(
+ MaosAttestationClient.PREFS, Context.MODE_PRIVATE);
+ String result = prefs.getString(MaosAttestationClient.KEY_LAST_RESULT, null);
+ if (result == null) {
+ status.setSummary(R.string.maos_attestation_never);
+ } else {
+ long at = prefs.getLong(MaosAttestationClient.KEY_LAST_AT, 0);
+ String level = prefs.getString(MaosAttestationClient.KEY_LAST_LEVEL, "");
+ String when = at == 0 ? "" : DateFormat.getDateTimeInstance().format(new Date(at));
+ status.setSummary(getString(R.string.maos_attestation_last, result, level, when));
+ }
+ Preference verification = findPreference(KEY_RESULT);
+ if (verification != null) {
+ String code = prefs.getString(MaosAttestationClient.KEY_LAST_CODE, null);
+ String url = prefs.getString(MaosAttestationClient.KEY_LAST_URL, null);
+ if (code == null || code.isEmpty() || url == null || url.isEmpty()) {
+ verification.setVisible(false);
+ } else {
+ verification.setVisible(true);
+ verification.setSummary(
+ getString(R.string.maos_attestation_result_summary, code, url));
+ }
+ }
+ }
+
+ private String sessionCode() {
+ Preference pref = findPreference(KEY_SESSION);
+ if (pref instanceof EditTextPreference) {
+ String text = ((EditTextPreference) pref).getText();
+ return text == null ? "" : text.trim();
+ }
+ return "";
+ }
+
+ @Override
+ public void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ // Scanned session code: fill the field and verify right away.
+ getParentFragmentManager().setFragmentResultListener(
+ MaosAttestationScannerFragment.RESULT_KEY, this, (requestKey, result) -> {
+ String code = result.getString(MaosAttestationScannerFragment.RESULT_CODE, "");
+ Preference session = findPreference(KEY_SESSION);
+ if (session instanceof EditTextPreference) {
+ ((EditTextPreference) session).setText(code);
+ }
+ Preference verify = findPreference(KEY_VERIFY);
+ if (verify != null) {
+ runVerify(verify);
+ }
+ });
+ }
+
+ @Override
+ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
+ super.onCreatePreferences(savedInstanceState, rootKey);
+ Preference pref = findPreference(KEY_SESSION);
+ if (pref instanceof EditTextPreference) {
+ ((EditTextPreference) pref).setOnBindEditTextListener(editText -> {
+ editText.setSingleLine(true);
+ editText.setHint("XXXX-XXXX");
+ editText.setSelection(editText.getText().length());
+ });
+ }
+ }
+
+ public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+ new BaseSearchIndexProvider(R.xml.maos_attestation_settings);
+}
diff --git a/src/com/android/settings/security/MaosAttestationScannerFragment.java b/src/com/android/settings/security/MaosAttestationScannerFragment.java
new file mode 100644
index 00000000000..7f1ceb0c385
--- /dev/null
+++ b/src/com/android/settings/security/MaosAttestationScannerFragment.java
@@ -0,0 +1,173 @@
+/*
+ * MAOS attestation QR scanner: scans the session code shown at
+ * maproj.org/attest using SettingsLib's QrCamera (the same scanner path
+ * as Wi-Fi DPP and ADB pairing), then hands the code back to
+ * MaosAttestationFragment, which runs Verify against that session.
+ * Manual code entry on the attestation screen is the fallback.
+ */
+package com.android.settings.security;
+
+import android.app.settings.SettingsEnums;
+import android.graphics.Matrix;
+import android.graphics.Rect;
+import android.graphics.SurfaceTexture;
+import android.os.Bundle;
+import android.util.Size;
+import android.view.LayoutInflater;
+import android.view.TextureView;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import com.android.settings.R;
+import com.android.settings.wifi.dpp.WifiDppQrCodeBaseFragment;
+import com.android.settingslib.qrcode.QrCamera;
+
+public class MaosAttestationScannerFragment extends WifiDppQrCodeBaseFragment
+ implements TextureView.SurfaceTextureListener, QrCamera.ScannerCallback {
+
+ static final String RESULT_KEY = "maos_attestation_scan";
+ static final String RESULT_CODE = "session_code";
+
+ private QrCamera mCamera;
+ private TextureView mTextureView;
+ private TextView mErrorMessage;
+
+ @Override
+ public int getMetricsCategory() {
+ return SettingsEnums.SECURITY;
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ return inflater.inflate(R.layout.adb_qrcode_scanner_fragment, container, false);
+ }
+
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+ mTextureView = view.findViewById(R.id.preview_view);
+ mTextureView.setSurfaceTextureListener(this);
+ mErrorMessage = view.findViewById(R.id.error_message);
+ setHeaderTitle(R.string.maos_attestation_scan_title);
+ mSummary.setText(R.string.maos_attestation_scan_summary);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ if (mCamera != null && mTextureView.getSurfaceTexture() != null) {
+ mCamera.start(mTextureView.getSurfaceTexture());
+ }
+ }
+
+ @Override
+ public void onPause() {
+ if (mCamera != null) {
+ mCamera.stop();
+ }
+ super.onPause();
+ }
+
+ @Override
+ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+ if (mCamera == null) {
+ mCamera = new QrCamera(getContext(), this);
+ mCamera.start(surface);
+ }
+ }
+
+ @Override
+ public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
+
+ @Override
+ public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
+ if (mCamera != null) {
+ mCamera.stop();
+ mCamera = null;
+ }
+ return true;
+ }
+
+ @Override
+ public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
+
+ @Override
+ public Size getViewSize() {
+ return new Size(mTextureView.getWidth(), mTextureView.getHeight());
+ }
+
+ @Override
+ public void setTransform(Matrix transform) {
+ if (getActivity() != null) {
+ getActivity().runOnUiThread(() -> mTextureView.setTransform(transform));
+ }
+ }
+
+ @Override
+ public Rect getFramePosition(Size previewSize, int cameraOrientation) {
+ return new Rect(0, 0, previewSize.getHeight(), previewSize.getHeight());
+ }
+
+ /** Session code from `maos-attest://verify?code=XXXX-XXXX`, a bare
+ * code, or any URL carrying `code=` / `sessionCode=`. */
+ static String parseSessionCode(String qr) {
+ if (qr == null) {
+ return null;
+ }
+ String s = qr.trim();
+ for (String key : new String[]{"code=", "sessionCode="}) {
+ int at = s.indexOf(key);
+ if (at >= 0) {
+ String rest = s.substring(at + key.length());
+ StringBuilder out = new StringBuilder();
+ for (int i = 0; i < rest.length(); i++) {
+ char c = rest.charAt(i);
+ if (c == '&' || Character.isWhitespace(c) || c == '"' || c == '\'') {
+ break;
+ }
+ out.append(c);
+ }
+ return out.toString();
+ }
+ }
+ return s;
+ }
+
+ @Override
+ public boolean isValid(String qrCode) {
+ String code = parseSessionCode(qrCode);
+ if (code == null || code.replace("-", "").length() != 8) {
+ mErrorMessage.setText(R.string.maos_attestation_scan_invalid);
+ mErrorMessage.setVisibility(View.VISIBLE);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void handleSuccessfulResult(String qrCode) {
+ if (mCamera != null) {
+ mCamera.stop();
+ mCamera = null;
+ }
+ Bundle result = new Bundle();
+ result.putString(RESULT_CODE, parseSessionCode(qrCode));
+ getParentFragmentManager().setFragmentResult(RESULT_KEY, result);
+ getParentFragmentManager().popBackStack();
+ }
+
+ @Override
+ public void handleCameraFailure() {
+ if (mCamera != null) {
+ mCamera.stop();
+ mCamera = null;
+ }
+ }
+
+ @Override
+ protected boolean isFooterAvailable() {
+ return false;
+ }
+}