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

Commit c5375e46 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[CDM] Fix race condition that caused handshake init collision detection...

Merge "[CDM] Fix race condition that caused handshake init collision detection to be bypassed occasionally." into main
parents 1866ca8b 0475831e
Loading
Loading
Loading
Loading
+25 −12
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ public class SecureChannel {
    private int mVerificationResult = FLAG_FAILURE_UNKNOWN;
    private boolean mPskVerified;

    private final Object mHandshakeLock = new Object();


    /**
     * Create a new secure channel object. This secure channel allows secure messages to be
@@ -342,6 +344,7 @@ public class SecureChannel {
    }

    private void initiateHandshake() throws IOException, BadHandleException , HandshakeException {
        synchronized (mHandshakeLock) {
            if (mConnectionContext != null) {
                Slog.d(TAG, "Ukey2 handshake is already completed.");
                return;
@@ -357,6 +360,7 @@ public class SecureChannel {
            }
            sendMessage(MessageType.HANDSHAKE_INIT, constructHandshakeInitMessage(mClientInit));
        }
    }

    // In an occasion where both participants try to initiate a handshake, resolve the conflict
    // with a dice roll simulated by the message byte content comparison.
@@ -414,8 +418,17 @@ public class SecureChannel {
        // Mark "in-progress" upon receiving the first message
        mInProgress = true;

        // Complete a series of handshake exchange and processing
        synchronized (mHandshakeLock) {
            completeHandshake(handshakeInitMessage);
        }
    }

    private void completeHandshake(byte[] initMessage) throws IOException, HandshakeException,
            BadHandleException, CryptoException, AlertException {

        // Handle a potential collision where both devices tried to initiate a connection
        byte[] handshakeMessage = handleHandshakeCollision(handshakeInitMessage);
        byte[] handshakeMessage = handleHandshakeCollision(initMessage);

        // Proceed with the rest of Ukey2 handshake
        if (mHandshakeContext == null) { // Server-side logic