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

Commit 92b0401f authored by Anna Herrera's avatar Anna Herrera
Browse files

Revert "RootCanal: Authentication challenge"

This reverts commit 5876607c.

Reason for revert: Caused test failure b/238461288

Change-Id: I1dcfa51ad347e76149bce9e70546982797822286
parent 5876607c
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -44,8 +44,6 @@ cc_binary {
        "liblog",
        "libutils",
        "libprotobuf-cpp-lite",
        "libcrypto",
        "libssl",
    ],
    cflags: [
        "-fvisibility=hidden",
@@ -98,8 +96,6 @@ cc_library_shared {
        "liblog",
        "libutils",
        "libprotobuf-cpp-lite",
        "libcrypto",
        "libssl",
    ],
    cflags: [
        "-Wall",
+0 −2
Original line number Diff line number Diff line
@@ -176,8 +176,6 @@ cc_binary_host {
    shared_libs: [
        "liblog",
        "libunwindstack",
        "libcrypto",
        "libssl",
    ],
    whole_static_libs: [
        "libbt-rootcanal",
+0 −2
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ rust_ffi {
        "libbt_packets",
        "libbytes",
        "libnum_traits",
        "libopenssl",
        "libthiserror",
        "libpin_utils",
    ],
@@ -57,7 +56,6 @@ rust_test_host {
        "libbt_packets",
        "libbytes",
        "libnum_traits",
        "libopenssl",
        "libthiserror",
        "libpin_utils",
    ],
+47 −98
Original line number Diff line number Diff line
// Bluetooth Core, Vol 2, Part C, 4.2.1

use num_traits::ToPrimitive;
use openssl::rand::rand_bytes;
use openssl::symm::{encrypt, Cipher};
use std::convert::TryInto;

use crate::either::Either;
use crate::num_hci_command_packets;
@@ -13,11 +10,6 @@ use crate::procedure::legacy_pairing;
use crate::procedure::secure_simple_pairing;
use crate::procedure::Context;

fn generate_sres(random_number: &[u8; 16], key: &[u8; 16]) -> [u8; 4] {
    let cipher = Cipher::aes_128_cbc();
    encrypt(cipher, key, None, random_number).unwrap()[0..4].try_into().unwrap()
}

async fn secure_simple_pairing_supported(ctx: &impl Context) -> bool {
    let ssp_bit = hci::LMPFeaturesPage1Bits::SecureSimplePairingHostSupport.to_u64().unwrap();
    let local_supported = ctx.extended_features(1) & ssp_bit != 0;
@@ -33,45 +25,28 @@ async fn secure_simple_pairing_supported(ctx: &impl Context) -> bool {
    local_supported && peer_supported.await
}

pub async fn send_authentication_challenge(
    ctx: &impl Context,
    transaction_id: u8,
    link_key: [u8; 16],
) -> Result<(), ()> {
    let mut random_number = [0; 16];
    rand_bytes(&mut random_number).unwrap();
    ctx.send_lmp_packet(lmp::AuRandBuilder { transaction_id, random_number }.build());

    match ctx.receive_lmp_packet::<Either<lmp::SresPacket, lmp::NotAcceptedPacket>>().await {
        Either::Left(response) => {
            if *response.get_authentication_rsp() == generate_sres(&random_number, &link_key) {
                Ok(())
            } else {
                Err(())
            }
        }
        Either::Right(_) => Err(()),
    }
pub async fn send_authentication_challenge(ctx: &impl Context, transaction_id: u8) {
    ctx.send_lmp_packet(lmp::AuRandBuilder { transaction_id, random_number: [0; 16] }.build());
    let _ = ctx.receive_lmp_packet::<lmp::SresPacket>().await;
}

pub async fn receive_authentication_challenge(ctx: &impl Context, link_key: [u8; 16]) {
    let au_rand = ctx.receive_lmp_packet::<lmp::AuRandPacket>().await;
    ctx.send_lmp_packet(
        lmp::SresBuilder {
            transaction_id: 0,
            authentication_rsp: generate_sres(au_rand.get_random_number(), &link_key),
pub async fn initiate(ctx: &impl Context) {
    let _ = ctx.receive_hci_command::<hci::AuthenticationRequestedPacket>().await;
    ctx.send_hci_event(
        hci::AuthenticationRequestedStatusBuilder {
            num_hci_command_packets,
            status: hci::ErrorCode::Success,
        }
        .build(),
    );
}

pub async fn request_link_key_from_host(ctx: &impl Context) -> Option<[u8; 16]> {
    ctx.send_hci_event(hci::LinkKeyRequestBuilder { bd_addr: ctx.peer_address() }.build());
    match ctx.receive_hci_command::<Either<

    let pairing = match ctx.receive_hci_command::<Either<
        hci::LinkKeyRequestReplyPacket,
        hci::LinkKeyRequestNegativeReplyPacket,
    >>().await {
        Either::Left(reply) => {
        Either::Left(_reply) => {
            ctx.send_hci_event(
                hci::LinkKeyRequestReplyCompleteBuilder {
                    num_hci_command_packets,
@@ -80,7 +55,7 @@ pub async fn request_link_key_from_host(ctx: &impl Context) -> Option<[u8; 16]>
                }
                .build(),
            );
            Some(*reply.get_link_key())
            false
        },
        Either::Right(_) => {
            ctx.send_hci_event(
@@ -91,34 +66,14 @@ pub async fn request_link_key_from_host(ctx: &impl Context) -> Option<[u8; 16]>
                }
                .build(),
            );
            None
        }
    }
}

pub async fn initiate(ctx: &impl Context) {
    let _ = ctx.receive_hci_command::<hci::AuthenticationRequestedPacket>().await;
    ctx.send_hci_event(
        hci::AuthenticationRequestedStatusBuilder {
            num_hci_command_packets,
            status: hci::ErrorCode::Success,
        }
        .build(),
    );

    let (pairing, link_key) = match request_link_key_from_host(ctx).await {
        Some(host_link_key) => (false, host_link_key),
        None => {
            // No link key, start pairing
            let result = if secure_simple_pairing_supported(ctx).await {
                secure_simple_pairing::initiate(ctx).await
            } else {
                legacy_pairing::initiate(ctx).await
            };

            if let Ok(pairing_link_key) = result {
                (true, pairing_link_key)
            } else {
            if result.is_err() {
                ctx.send_hci_event(
                    hci::AuthenticationCompleteBuilder {
                        status: hci::ErrorCode::AuthenticationFailure,
@@ -128,33 +83,37 @@ pub async fn initiate(ctx: &impl Context) {
                );
                return;
            }
            true
        }
    };

    let auth_response = send_authentication_challenge(ctx, 0, link_key).await;
    send_authentication_challenge(ctx, 0).await;

    // Link Key Calculation
    if pairing && auth_response.is_ok() {
        // Bluetooth Core, Vol 2, Part C, 4.2.19
        receive_authentication_challenge(ctx, link_key).await;
    if pairing {
        let _random_number = ctx.receive_lmp_packet::<lmp::AuRandPacket>().await;

        // TODO: Resolve authentication challenge
        ctx.send_lmp_packet(
            lmp::SresBuilder { transaction_id: 0, authentication_rsp: [0; 4] }.build(),
        );

        ctx.send_hci_event(
            hci::LinkKeyNotificationBuilder {
                bd_addr: ctx.peer_address(),
                key_type: hci::KeyType::AuthenticatedP192,
                link_key,
                link_key: [0; 16],
            }
            .build(),
        );
    }

    let status = if auth_response.is_ok() {
        hci::ErrorCode::Success
    } else {
        hci::ErrorCode::AuthenticationFailure
    };
    ctx.send_hci_event(
        hci::AuthenticationCompleteBuilder { status, connection_handle: ctx.peer_handle() }.build(),
        hci::AuthenticationCompleteBuilder {
            status: hci::ErrorCode::Success,
            connection_handle: ctx.peer_handle(),
        }
        .build(),
    );
}

@@ -165,16 +124,10 @@ pub async fn respond(ctx: &impl Context) {
    >>()
    .await
    {
        Either::Left(au_rand) => {
            match request_link_key_from_host(ctx).await {
                Some(link_key) => {
                    let remote_random_number = au_rand.get_random_number();
                    ctx.send_lmp_packet(lmp::SresBuilder { transaction_id: 0, authentication_rsp: generate_sres(remote_random_number, &link_key) }.build());
                },
                None => {
                    ctx.send_lmp_packet(lmp::NotAcceptedBuilder { transaction_id: 0, not_accepted_opcode: lmp::Opcode::AuRand, error_code: hci::ErrorCode::PinOrKeyMissing.to_u8().unwrap() }.build());
                }
            }
        Either::Left(_random_number) => {
            // TODO: Resolve authentication challenge
            // TODO: Ask for link key
            ctx.send_lmp_packet(lmp::SresBuilder { transaction_id: 0, authentication_rsp: [0; 4] }.build());
        },
        Either::Right(pairing) => {
            let result = match pairing {
@@ -189,25 +142,21 @@ pub async fn respond(ctx: &impl Context) {
            }

            // Link Key Calculation
            let link_key = if let Ok(link_key) = result {
                link_key
             } else {
                return;
             };
            receive_authentication_challenge(ctx, link_key).await;
            let auth_result = send_authentication_challenge(ctx, 0, link_key).await;

            if auth_result.is_ok() {
            let _random_number = ctx.receive_lmp_packet::<lmp::AuRandPacket>().await;
            // TODO: Resolve authentication challenge
            ctx.send_lmp_packet(lmp::SresBuilder { transaction_id: 0, authentication_rsp: [0; 4] }.build());

            send_authentication_challenge(ctx, 0).await;

            ctx.send_hci_event(
                hci::LinkKeyNotificationBuilder {
                    bd_addr: ctx.peer_address(),
                    key_type: hci::KeyType::AuthenticatedP192,
                        link_key,
                    link_key: [0; 16],
                }
                .build(),
            );
        }
            // TODO: Handle error
        }
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ use crate::procedure::Context;

use crate::num_hci_command_packets;

pub async fn initiate(ctx: &impl Context) -> Result<([u8; 16]), ()> {
pub async fn initiate(ctx: &impl Context) -> Result<(), ()> {
    ctx.send_hci_event(hci::PinCodeRequestBuilder { bd_addr: ctx.peer_address() }.build());

    let _pin_code = ctx.receive_hci_command::<hci::PinCodeRequestReplyPacket>().await;
@@ -30,10 +30,10 @@ pub async fn initiate(ctx: &impl Context) -> Result<([u8; 16]), ()> {

    let _ = ctx.receive_lmp_packet::<lmp::CombKeyPacket>().await;

    Ok([0; 16])
    Ok(())
}

pub async fn respond(ctx: &impl Context, _request: lmp::InRandPacket) -> Result<([u8; 16]), ()> {
pub async fn respond(ctx: &impl Context, _request: lmp::InRandPacket) -> Result<(), ()> {
    ctx.send_hci_event(hci::PinCodeRequestBuilder { bd_addr: ctx.peer_address() }.build());

    let _pin_code = ctx.receive_hci_command::<hci::PinCodeRequestReplyPacket>().await;
@@ -55,5 +55,5 @@ pub async fn respond(ctx: &impl Context, _request: lmp::InRandPacket) -> Result<

    ctx.send_lmp_packet(lmp::CombKeyBuilder { transaction_id: 0, random_number: [0; 16] }.build());

    Ok([0; 16])
    Ok(())
}
Loading