Exemple #1
0
def auth(pass_obj: dict, beacon: int, certificate: str, key: str, force: bool) -> None:
    """Perform the Debug Authentication."""
    try:
        logger.info("Starting Debug Authentication")
        mail_box = pass_obj['debug_mailbox']
        with open(certificate, 'rb') as f:
            debug_cred_data = f.read()
        debug_cred = DebugCredential.parse(debug_cred_data)
        dac_data = dm_commands.DebugAuthenticationStart(dm=mail_box).run()
        # convert List[int] to bytes
        dac_data_bytes = struct.pack(f'<{len(dac_data)}I', *dac_data)
        dac = DebugAuthenticationChallenge.parse(dac_data_bytes)
        logger.debug(f'DAC: \n{dac.info()}')
        dar = DebugAuthenticateResponse.create(
            version=pass_obj['protocol'], socc=dac.socc,
            dc=debug_cred, auth_beacon=beacon, dac=dac, dck=key
        )
        logger.debug(f'DAR:\n{dar.info()}')
        dar_data = dar.export()
        # convert bytes to List[int]
        dar_data_words = list(struct.unpack(f'<{len(dar_data)//4}I', dar_data))
        dar_response = dm_commands.DebugAuthenticationResponse(
            dm=mail_box, paramlen=len(dar_data_words)
        ).run(dar_data_words)
        logger.debug(f'DAR response: {dar_response}')
        exit_response = dm_commands.ExitDebugMailbox(dm=mail_box).run()
        logger.debug(f'Exit response: {exit_response}')
        logger.info("Debug Authentication successful")
    except Exception as e:
        logger.error(f"Start Debug Mailbox failed!\n{e}")
Exemple #2
0
def gendc(ctx: click.Context, plugin: click.Path, dc_file_path: str, config: click.File,
          elf2sb_config: click.File, force: bool) -> None:
    """Generate debug certificate (DC).

    \b
    PATH    - path to dc file
    """
    if plugin:
        # if a plugin is present simply load it
        # The SignatureProvider will automatically pick up any implementation(s)
        from importlib.util import spec_from_file_location, module_from_spec
        spec = spec_from_file_location(name='plugin', location=plugin)  # type: ignore
        mod = module_from_spec(spec)
        spec.loader.exec_module(mod)  # type: ignore

    is_rsa = ctx.obj['is_rsa']
    protocol = ctx.obj['protocol_version']
    check_destination_dir(dc_file_path, force)
    check_file_exists(dc_file_path, force)

    logger.info("Loading configuration from yml file...")
    yaml_content = yaml.safe_load(config)  # type: ignore
    if elf2sb_config:
        logger.info("Loading configuration from elf2sb config file...")
        rot_info = RootOfTrustInfo(json.load(elf2sb_config))  # type: ignore
        yaml_content["rot_meta"] = rot_info.public_keys
        yaml_content["rotk"] = rot_info.private_key
        yaml_content["rot_id"] = rot_info.public_key_index

    # enforcing rot_id presence in yaml config...
    assert "rot_id" in yaml_content, "Config file doesn't contain the 'rot_id' field"

    logger.info(f"Creating {'RSA' if is_rsa else 'ECC'} debug credential object...")
    dc = DebugCredential.create_from_yaml_config(version=protocol, yaml_config=yaml_content)
    dc.sign()
    data = dc.export()
    logger.info("Saving the debug credential to a file...")
    with open(dc_file_path, 'wb') as f:
        f.write(data)
Exemple #3
0
def gendc(ctx: click.Context, dc_file_path: str, config: click.File, elf2sb_config: click.File, force: bool) -> None:
    """Generate debug certificate (DC).

    \b
    PATH    - path to dc file
    """
    is_rsa = ctx.obj['is_rsa']
    protocol = ctx.obj['protocol_version']
    check_destination_dir(dc_file_path, force)
    check_file_exists(dc_file_path, force)

    logger.info("Loading configuration from yml file...")
    yaml_content = yaml.safe_load(config)   #type: ignore
    if elf2sb_config:
        logger.info("Loading configuration from elf2sb config file...")
        rot_info = RootOfTrustInfo(json.load(elf2sb_config))    #type: ignore
        yaml_content["rot_meta"] = rot_info.public_keys
        yaml_content["rotk"] = rot_info.private_key
    logger.info(f"Creating {'RSA' if is_rsa else 'ECC'} debug credential object...")
    dc = DebugCredential.from_yaml_config(version=protocol, yaml_config=yaml_content)
    data = dc.export()
    logger.info("Saving the debug credential to a file...")
    with open(dc_file_path, 'wb') as f:
        f.write(data)