Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e778ed3a authored by nathch's avatar nathch
Browse files

Route backup data through IntermediateEncryptingTransport

The main change is in TransportManager which now uses
TransportClientManager.createEncryptingClientManager

We do this so that IntermediateEncryptingTransport can encrypt (decrypt) the
data when sending it (receiving it) from the real transport.

Test: atest -v BackupEncryptionUnitTests
Test: atest -v RunBackupFrameworksServicesRoboTests
Test: atest -v $(find frameworks/base/services/tests/servicestests/src/com/android/server/backup -name '\''*Test.java'\'')'
Test: atest -v CtsBackupTestCases CtsBackupHostTestCases
Change-Id: I01a0108da6b1f868ac6cb4591445ecc62316452f
parent 52eba65f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -16,7 +16,10 @@

package com.android.server.backup.encryption.transport;

import static com.android.server.backup.encryption.BackupEncryptionService.TAG;

import android.os.RemoteException;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
@@ -47,6 +50,7 @@ public class IntermediateEncryptingTransport extends DelegatingTransport {
    }

    private void connect() throws RemoteException {
        Log.i(TAG, "connecting " + mTransportClient);
        synchronized (mConnectLock) {
            if (mRealTransport == null) {
                mRealTransport = mTransportClient.connect("IntermediateEncryptingTransport");
+1 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ public class IntermediateEncryptingTransportManager {
     * Create an instance of {@link IntermediateEncryptingTransport}.
     */
    private IntermediateEncryptingTransport create(Intent realTransportIntent) {
        Log.d(TAG, "create: intent:" + realTransportIntent);
        return new IntermediateEncryptingTransport(mTransportClientManager.getTransportClient(
                realTransportIntent.getComponent(), realTransportIntent.getExtras(), CALLER));
    }
+2 −1
Original line number Diff line number Diff line
@@ -93,7 +93,8 @@ public class TransportManager {
        mTransportWhitelist = Preconditions.checkNotNull(whitelist);
        mCurrentTransportName = selectedTransport;
        mTransportStats = new TransportStats();
        mTransportClientManager = new TransportClientManager(mUserId, context, mTransportStats);
        mTransportClientManager = TransportClientManager.createEncryptingClientManager(mUserId,
                context, mTransportStats);
    }

    @VisibleForTesting
+36 −11
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.backup.transport;
import static com.android.server.backup.TransportManager.SERVICE_ACTION_TRANSPORT_HOST;
import static com.android.server.backup.transport.TransportUtils.formatMessage;

import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.content.ComponentName;
import android.content.Context;
@@ -32,6 +33,7 @@ import com.android.server.backup.transport.TransportUtils.Priority;
import java.io.PrintWriter;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Function;

/**
 * Manages the creation and disposal of {@link TransportClient}s. The only class that should use
@@ -52,6 +54,7 @@ public class TransportClientManager {
    private final Object mTransportClientsLock = new Object();
    private int mTransportClientsCreated = 0;
    private Map<TransportClient, String> mTransportClientsCallerMap = new WeakHashMap<>();
    private final Function<ComponentName, Intent> mIntentFunction;

    /**
     * Return an {@link Intent} which resolves to an intermediate {@link IBackupTransport} that
@@ -64,6 +67,14 @@ public class TransportClientManager {
                .putExtra(ENCRYPTING_TRANSPORT_REAL_TRANSPORT_KEY, tranportComponent);
    }

    /**
     * Return an {@link Intent} which resolves to the {@link IBackupTransport} for the {@link
     * ComponentName}.
     */
    private static Intent getRealTransportIntent(ComponentName transportComponent) {
        return new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent);
    }

    /**
     * Given a {@link Intent} originally created by {@link
     * #getEncryptingTransportIntent(ComponentName)}, returns the {@link Intent} which resolves to
@@ -72,18 +83,35 @@ public class TransportClientManager {
    public static Intent getRealTransportIntent(Intent encryptingTransportIntent) {
        ComponentName transportComponent = encryptingTransportIntent.getParcelableExtra(
                ENCRYPTING_TRANSPORT_REAL_TRANSPORT_KEY);
        Intent intent = new Intent(SERVICE_ACTION_TRANSPORT_HOST)
                .setComponent(transportComponent)
        Intent intent = getRealTransportIntent(transportComponent)
                .putExtras(encryptingTransportIntent.getExtras());
        intent.removeExtra(ENCRYPTING_TRANSPORT_REAL_TRANSPORT_KEY);
        return intent;
    }

    /**
     * Create a {@link TransportClientManager} such that {@link #getTransportClient(ComponentName,
     * Bundle, String)} returns a {@link TransportClient} which connects to an intermediate {@link
     * IBackupTransport} that encrypts (or decrypts) the data when sending it (or receiving it) from
     * the {@link IBackupTransport} for the given {@link ComponentName}.
     */
    public static TransportClientManager createEncryptingClientManager(@UserIdInt int userId,
            Context context, TransportStats transportStats) {
        return new TransportClientManager(userId, context, transportStats,
                TransportClientManager::getEncryptingTransportIntent);
    }

    public TransportClientManager(@UserIdInt int userId, Context context,
            TransportStats transportStats) {
        this(userId, context, transportStats, TransportClientManager::getRealTransportIntent);
    }

    private TransportClientManager(@UserIdInt int userId, Context context,
            TransportStats transportStats, Function<ComponentName, Intent> intentFunction) {
        mUserId = userId;
        mContext = context;
        mTransportStats = transportStats;
        mIntentFunction = intentFunction;
    }

    /**
@@ -97,10 +125,7 @@ public class TransportClientManager {
     * @return A {@link TransportClient}.
     */
    public TransportClient getTransportClient(ComponentName transportComponent, String caller) {
        Intent bindIntent =
                new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent);

        return getTransportClient(transportComponent, caller, bindIntent);
        return getTransportClient(transportComponent, null, caller);
    }

    /**
@@ -115,11 +140,11 @@ public class TransportClientManager {
     * @return A {@link TransportClient}.
     */
    public TransportClient getTransportClient(
            ComponentName transportComponent, Bundle extras, String caller) {
        Intent bindIntent =
                new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent);
            ComponentName transportComponent, @Nullable Bundle extras, String caller) {
        Intent bindIntent = mIntentFunction.apply(transportComponent);
        if (extras != null) {
            bindIntent.putExtras(extras);

        }
        return getTransportClient(transportComponent, caller, bindIntent);
    }