src/fwup_verify.c
/**
 * @brief Verify that the firmware archive is ok
 * @param input_filename the firmware update filename
 * @param public_keys the public keys if authentication check
 * @return 0 if successful
 */
@brief Verify that the firmware archive is ok
@param input_filename the firmware update filename
@param public_keys the public keys if authentication check
@return 0 if successful
@brief Verify that the firmware archive is ok @param input_filename the firmware update filename @param public_keys the public keys if authentication check @return 0 if successful
int fwup_verify(const char *input_filename, unsigned char * const *public_keys)
{
    unsigned char *meta_conf_signature = NULL;
    struct resource_list *all_resources = NULL;
    cfg_t *cfg = NULL;
    int rc = 0;
    struct archive *a = archive_read_new();
    archive_read_support_format_zip(a);
    if (!input_filename)
        ERR_CLEANUP_MSG("Specify an input firmware file");
    rc = fwup_archive_open_filename(a, input_filename);
    if (rc != ARCHIVE_OK)
        ERR_CLEANUP_MSG("%s", archive_error_string(a));
    struct archive_entry *ae;
    rc = archive_read_next_header(a, &ae);
    if (rc != ARCHIVE_OK)
        ERR_CLEANUP_MSG("%s", archive_error_string(a));
    if (strcmp(archive_entry_pathname(ae), "meta.conf.ed25519") == 0) {
        off_t total_size;
        if (archive_read_all_data(a, ae, (char **) &meta_conf_signature, crypto_sign_BYTES, &total_size) < 0)
            ERR_CLEANUP_MSG("Error reading meta.conf.ed25519 from archive.\n"
                            "Check for file corruption or libarchive built without zlib support");
        if (total_size != crypto_sign_BYTES)
            ERR_CLEANUP_MSG("Unexpected meta.conf.ed25519 size: %d", total_size);
        rc = archive_read_next_header(a, &ae);
        if (rc != ARCHIVE_OK)
            ERR_CLEANUP_MSG("Expecting more than meta.conf.ed25519 in archive");
    }
    if (strcmp(archive_entry_pathname(ae), "meta.conf") != 0)
        ERR_CLEANUP_MSG("Expecting meta.conf to be at the beginning of %s", input_filename);
    OK_OR_CLEANUP(cfgfile_parse_fw_ae(a, ae, &cfg, meta_conf_signature, public_keys));
    OK_OR_CLEANUP(rlist_get_all(cfg, &all_resources));
    while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
        const char *filename = archive_entry_pathname(ae);
        char resource_name[FWFILE_MAX_ARCHIVE_PATH];
        OK_OR_CLEANUP(archive_filename_to_resource(filename, resource_name, sizeof(resource_name)));
        OK_OR_CLEANUP(check_resource(all_resources, resource_name, a, ae));
    }
    for (struct resource_list *r = all_resources; r != NULL; r = r->next) {
        if (!r->processed)
            ERR_CLEANUP_MSG("Resource %s not found in archive", cfg_title(r->resource));
    }
    const char *success_message;
    if (*public_keys && meta_conf_signature)
        success_message = "Valid archive with a good signature\n";
    else if (!*public_keys && meta_conf_signature)
        success_message = "Valid archive with an unverified signature. Specify a public key to authenticate.\n";
    else
        success_message = "Valid archive without a signature\n";
    fwup_output(FRAMING_TYPE_SUCCESS, 0, success_message);
cleanup:
    rlist_free(all_resources);
    archive_read_close(a);
    archive_read_free(a);
    if (meta_conf_signature)
        free(meta_conf_signature);
    if (cfg)
        cfgfile_free(cfg);
    return rc;
}
int fwup_verify ( const char * input_filename , unsigned char * const * public_keys ) { unsigned char * meta_conf_signature = NULL ; struct resource_list * all_resources = NULL ; cfg_t * cfg = NULL ; int rc = 0 ; struct archive * a = archive_read_new ( ) ; archive_read_support_format_zip ( a ) ; if ( ! input_filename ) ERR_CLEANUP_MSG ( " " ) ; rc = fwup_archive_open_filename ( a , input_filename ) ; if ( rc != ARCHIVE_OK ) ERR_CLEANUP_MSG ( " " , archive_error_string ( a ) ) ; struct archive_entry * ae ; rc = archive_read_next_header ( a , & ae ) ; if ( rc != ARCHIVE_OK ) ERR_CLEANUP_MSG ( " " , archive_error_string ( a ) ) ; if ( strcmp ( archive_entry_pathname ( ae ) , " " ) == 0 ) { off_t total_size ; if ( archive_read_all_data ( a , ae , ( char * * ) & meta_conf_signature , crypto_sign_BYTES , & total_size ) < 0 ) ERR_CLEANUP_MSG ( " \n " " " ) ; if ( total_size != crypto_sign_BYTES ) ERR_CLEANUP_MSG ( " " , total_size ) ; rc = archive_read_next_header ( a , & ae ) ; if ( rc != ARCHIVE_OK ) ERR_CLEANUP_MSG ( " " ) ; } if ( strcmp ( archive_entry_pathname ( ae ) , " " ) != 0 ) ERR_CLEANUP_MSG ( " " , input_filename ) ; OK_OR_CLEANUP ( cfgfile_parse_fw_ae ( a , ae , & cfg , meta_conf_signature , public_keys ) ) ; OK_OR_CLEANUP ( rlist_get_all ( cfg , & all_resources ) ) ; while ( archive_read_next_header ( a , & ae ) == ARCHIVE_OK ) { const char * filename = archive_entry_pathname ( ae ) ; char resource_name [ FWFILE_MAX_ARCHIVE_PATH ] ; OK_OR_CLEANUP ( archive_filename_to_resource ( filename , resource_name , sizeof ( resource_name ) ) ) ; OK_OR_CLEANUP ( check_resource ( all_resources , resource_name , a , ae ) ) ; } for ( struct resource_list * r = all_resources ; r != NULL ; r = r -> next ) { if ( ! r -> processed ) ERR_CLEANUP_MSG ( " " , cfg_title ( r -> resource ) ) ; } const char * success_message ; if ( * public_keys && meta_conf_signature ) success_message = " \n " ; else if ( ! * public_keys && meta_conf_signature ) success_message = " \n " ; else success_message = " \n " ; fwup_output ( FRAMING_TYPE_SUCCESS , 0 , success_message ) ; cleanup : rlist_free ( all_resources ) ; archive_read_close ( a ) ; archive_read_free ( a ) ; if ( meta_conf_signature ) free ( meta_conf_signature ) ; if ( cfg ) cfgfile_free ( cfg ) ; return rc ; }
@brief Verify that the firmware archive is ok
 @param input_filename the firmware update filename
 @param public_keys the public keys if authentication check
 @return 0 if successful
@brief Verify that the firmware archive is ok @param input_filename the firmware update filename @param public_keys the public keys if authentication check @return 0 if successful
// Check that all resources have been validated