/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Created in Samsung Ukraine R&D Center (SRK) under a contract between
 * LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
 * and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 *
 * Created on: Jul 12, 2016
 * Author: Sergey Kosogov <s.kosogov@samsung.com>
 * Brief: Store/load key data.
 */

#ifndef TIGER_KEY_DATA_STORE_H
#define TIGER_KEY_DATA_STORE_H

#include <stddef.h>
#include <stdint.h>

#include "TigerTci.h"
#include "TigerTypeObjectId.h"

/**
 * @brief Statuses returned by Tiger Key Data Store functions.
 */

// TODO(o.kachkan) move structs to separate module
/**
 * @brief Opaque type to store RSA key pair.
 */
typedef struct TigerKeyPair TigerKeyPair_t;

/**
 * @brief Saves key pair information with certificate information into TEE Persistent Object.
 * @param[in] alias - alias of the key pair to be saved.
 * @param[in] keypair - key pair info.
 * @return TEE_SUCCESS if success; corresponding error code otherwise.
 */
TEE_Result tigerSaveKeyPair(const TigerObjectId_t* const objectId, const TigerKeyPair_t* const keypair);

TigerKeyPair_t* tigerAllocateKeyPair(void);
void tigerFreeKeyPair(TigerKeyPair_t*);
void* tigerGetKeyPairContext(const TigerKeyPair_t*);

/**
 * @brief Generates an RSA key pair with a given bit-length
 * @param[out] generated key pair
 * @return TEE_SUCCESS if success; corresponding error code otherwise.
 */
TEE_Result tigerGenerateKeyPair(TigerKeyPair_t* keypair);

/**
 * @brief Write a public key corresponding to key pair with alias 'alias' to a SubjectPublicKeyInfo DER structure
 * @param[in] key pair alias
 * @param[in, out] reference to current position pointer in array: end of buffer on input, beginning of data on output
 * @param[in] size of buffer
 */
TEE_Result tigerGetPublicKey(const TigerObjectId_t* objId, uint8_t** p, size_t size);

/**
 * @brief Write a 'certificate' corresponding to key pair with alias 'alias' in JSON format
 * @param[in] key object id
 * @param[in, out] an initialized (NULL) pointer to write data to
 * @param[in, out] size of written data
 */
TEE_Result tigerGenerateJsonCertificate(const TigerObjectId_t* objId, const TigerObjectId_t* parentObjId, TciProcessUid_t uid, uint8_t* p, uint32_t* size);
////// end

/**
 * @brief Removes persistent object containing key pair and key pair information by alias.
 * @param[in] key pair object ID.
 * @return TIGER_KDS_OK if success; corresponding error code otherwise.
 */
TEE_Result tigerDeleteKeyPair(const TigerObjectId_t* const objId);

/**
 * @brief loads key pair from TEE Storage by alias
 * @param[in] alias
 * @param[out] loaded key pair
 * @return TIGER_KDS_OK if success; corresponding error code otherwise.
 */
TEE_Result tigerLoadKeyPair(const TigerObjectId_t* const objId, TigerKeyPair_t* result);

TEE_Result tigerKeyPairExists(const TigerObjectId_t* const id);

#endif // TIGER_KEY_DATA_STORE_H
