/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at @.^.^.LICENSE.
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * CDDL HEADER END
 *
 * Copyright (c) RISC OS Developments 2026. All rights reserved.
 * SPDX-License-Identifier: CDDL-1.0
 */

#ifndef TLSMANAGER_MANIFEST_H
#define TLSMANAGER_MANIFEST_H

#include <stddef.h>
#include "policy.h"

#define TLSMANAGER_MANIFEST_VERSION 1u
#define TLSMANAGER_MANIFEST_HEADER_SIZE 48u
#define TLSMANAGER_MANIFEST_SIGNATURE_SIZE 256u
#define TLSMANAGER_MANIFEST_MAX_ANCHORS 256u

typedef struct tlsmanager_counter
{
    unsigned int high;
    unsigned int low;
} tlsmanager_counter;

typedef enum tlsmanager_manifest_result
{
    TLSMANAGER_MANIFEST_OK = 0,
    TLSMANAGER_MANIFEST_TRUNCATED,
    TLSMANAGER_MANIFEST_MAGIC,
    TLSMANAGER_MANIFEST_VERSION_ERROR,
    TLSMANAGER_MANIFEST_LENGTH,
    TLSMANAGER_MANIFEST_COUNT,
    TLSMANAGER_MANIFEST_ORDER,
    TLSMANAGER_MANIFEST_ROLLBACK,
    TLSMANAGER_MANIFEST_NOT_YET_VALID,
    TLSMANAGER_MANIFEST_EXPIRED,
    TLSMANAGER_MANIFEST_KEY,
    TLSMANAGER_MANIFEST_SIGNATURE
} tlsmanager_manifest_result;

typedef int (*tlsmanager_signature_verifier)(
    unsigned int key_id,
    const unsigned char *payload,
    size_t payload_size,
    const unsigned char signature[TLSMANAGER_MANIFEST_SIGNATURE_SIZE],
    void *opaque);

typedef struct tlsmanager_manifest
{
    tlsmanager_counter sequence;
    tlsmanager_counter created;
    tlsmanager_counter expires;
    unsigned int key_id;
    unsigned int anchor_count;
    const unsigned char *anchors;
} tlsmanager_manifest;

tlsmanager_manifest_result TLSManagerManifestParse(
    const unsigned char *data,
    size_t size,
    tlsmanager_counter now,
    tlsmanager_counter minimum_sequence,
    tlsmanager_signature_verifier verifier,
    void *verifier_opaque,
    tlsmanager_manifest *manifest);

const char *TLSManagerManifestResultText(tlsmanager_manifest_result result);

#endif
