Exemple #1
0
def connect_to_usb_bitbox(debug: bool) -> int:
    """
    Connects and runs the main menu on a BitBox02 connected
    over USB.
    """
    try:
        bitbox = devices.get_any_bitbox02()
    except devices.TooManyFoundException:
        print("Multiple bitboxes detected. Only one supported")
        return 1
    except devices.NoneFoundException:
        try:
            bootloader = devices.get_any_bitbox02_bootloader()
        except devices.TooManyFoundException:
            print("Multiple bitbox bootloaders detected. Only one supported")
            return 1
        except devices.NoneFoundException:
            print("Neither bitbox nor bootloader found.")
            return 1
        else:
            hid_device = hid.device()
            hid_device.open_path(bootloader["path"])
            bootloader_connection = bitbox02.Bootloader(
                u2fhid.U2FHid(hid_device), bootloader)
            boot_app = SendMessageBootloader(bootloader_connection)
            return boot_app.run()
    else:

        def show_pairing(code: str) -> bool:
            print(
                "Please compare and confirm the pairing code on your BitBox02:"
            )
            print(code)
            return True

        def attestation_check(result: bool) -> None:
            if result:
                print("Device attestation PASSED")
            else:
                print("Device attestation FAILED")

        hid_device = hid.device()
        hid_device.open_path(bitbox["path"])
        bitbox_connection = bitbox02.BitBox02(
            transport=u2fhid.U2FHid(hid_device),
            device_info=bitbox,
            show_pairing_callback=show_pairing,
            attestation_check_callback=attestation_check,
        )

        if debug:
            print("Device Info:")
            pprint.pprint(bitbox)
        return SendMessage(bitbox_connection, debug).run()
Exemple #2
0
def connect_to_usart_bitboxbase(debug: bool,
                                serial_port: usart.SerialPort) -> int:
    """
    Connects and runs the main menu over a BitBoxBase connected
    over UART.
    """
    print("Trying to connect to BitBoxBase firmware...")
    bootloader_device: devices.DeviceInfo = get_bitboxbase_default_device(
        serial_port.port)

    def show_pairing(code: str) -> bool:
        print("(Pairing should be automatic) Pairing code:")
        print(code)
        return True

    def attestation_check(result: bool) -> None:
        if result:
            print("Device attestation PASSED")
        else:
            print("Device attestation FAILED")

    try:
        transport = usart.U2FUsart(serial_port)
        base_dev = BitBoxBase(
            transport,
            bootloader_device,
            show_pairing_callback=show_pairing,
            attestation_check_callback=attestation_check,
        )
        if debug:
            print("Device Info:")
            pprint.pprint(base_dev)
        return SendMessageBitBoxBase(base_dev, debug).run()
    except usart.U2FUsartErrorResponse as err:
        if err.error_code != usart.U2FUsartErrorResponse.ENDPOINT_UNAVAILABLE:
            raise
    except usart.U2FUsartTimeoutError:
        print("Timed out. Maybe the device is not connected?", file=sys.stderr)
        return 1

    print("BitBox unavailable. Starting bootloader connection.")
    transport = usart.U2FUsart(serial_port)
    bootloader = bitbox02.Bootloader(transport, bootloader_device)
    return SendMessageBootloader(bootloader).run()
Exemple #3
0
def main() -> int:
    """Main function"""
    parser = argparse.ArgumentParser(
        description="Tool for communicating with bitbox device")
    parser.add_argument("--debug",
                        action="store_true",
                        help="Print messages sent and received")
    parser.add_argument("--u2f",
                        action="store_true",
                        help="Use u2f menu instead")
    args = parser.parse_args()

    if args.u2f:
        try:
            u2fbitbox = u2f.bitbox02.get_bitbox02_u2f_device()
        except bitbox02.TooManyFoundException:
            print("Multiple bitboxes detected. Only one supported")
        except bitbox02.NoneFoundException:
            print("No bitboxes detected")
        else:
            u2fdevice = u2f.bitbox02.BitBox02U2F(u2fbitbox)
            u2fapp = U2FApp(u2fdevice, args.debug)
            return u2fapp.run()
        return 1

    try:
        bitbox = bitbox02.get_any_bitbox02()
    except bitbox02.TooManyFoundException:
        print("Multiple bitboxes detected. Only one supported")
        return 1
    except bitbox02.NoneFoundException:
        try:
            bootloader = bitbox02.get_any_bitbox02_bootloader()
        except bitbox02.TooManyFoundException:
            print("Multiple bitbox bootloaders detected. Only one supported")
        except bitbox02.NoneFoundException:
            print("Neither bitbox nor bootloader found.")
        else:
            boot_app = SendMessageBootloader(bitbox02.Bootloader(bootloader))
            return boot_app.run()
    else:

        def show_pairing(code: str) -> None:
            print(
                "Please compare and confirm the pairing code on your BitBox02:"
            )
            print(code)

        def attestation_check(result: bool) -> None:
            if result:
                print("Device attestation PASSED")
            else:
                print("Device attestation FAILED")

        device = bitbox02.BitBox02(
            device_info=bitbox,
            show_pairing_callback=show_pairing,
            attestation_check_callback=attestation_check,
        )

        if args.debug:
            print("Device Info:")
            pprint.pprint(bitbox)

        return SendMessage(device, args.debug).run()
    return 1