/*
 * =====================================================================================
 *
 *       Filename:  libdk_native_client.h
 *
 *    Description:  Socket handler to communicate SKM service.
 *
 *        Version:  1.0
 *        Created:  10/06/2015 01:43:45 AM
 *
 *         Author:  Dongwook Shim (), dw.shim@samsung.com
 *        Company:  Samsung Electronics
 *
 *        Copyright (c) 2015 by Samsung Electronics, All rights reserved. 
 *
 * =====================================================================================
 */

/* =======================================================================
 *
 * Error numbers.
 *
 * =======================================================================
 */
#define NO_ERROR			0
#define INVALID_DATA			-1
#define NOT_SUPPORTED_VERSION		-2
#define NOT_SUPPORTED_FUNCTION		-3
#define NOT_EXIST_DRK			-4
#define OPERATION_FAILED		-5
#define ACCESS_DENIED			-6
#define TOO_SMALL_BUFFER		-7

/* =======================================================================
 *
 * Key type definitions.
 *
 * =======================================================================
 */
#define KEY_TYPE_RSA			0x01
#define KEY_TYPE_SYMM			0x02
#define KEY_TYPE_EC			0x04

/* =======================================================================
 *
 * DRK status.
 *
 * =======================================================================
 */
#define DRK_IS_EXIST			1
#define DRK_IS_NOT_EXIST		0

/* =======================================================================
 *
 * TLV tags.
 *
 * =======================================================================
 */
#define TLV_TAG_EXPONENT		1
#define TLV_TAG_ISSUER			2
#define TLV_TAG_HASH_ALGO		3
#define TLV_TAG_SUBJECT			4
#define TLV_TAG_KEYUSAGE		5
#define TLV_TAG_EXT_KEYUSAGE		6
#define TLV_TAG_SIGN_DATA_BLOB		7
#define TLV_TAG_CERT_SM			8
#define TLV_TAG_CERT_SD			9
#define TLV_TAG_TIMESTAMP		10
#define TLV_TAG_WRAPPED_PCR		11
#define TLV_TAG_EXTEND_PCR_DATA		12
#define TLV_TAG_TID			13
#define TLV_TAG_WRAPPED_KEY		14
#define TLV_TAG_TLV_KEY_INFO		15
#define TLV_ATTRSDK			16

/**
 * Function : isExistDeviceRootKey()
 * Description : Check device root key is exist
 * Parameters : @ drkType : KEY_TYPE_RSA or KEY_TYPE_SYMM. 
 *                          KEY_TYPE_EC is not supported now. 
 * Return value : DRK_IS_EXIST(1) or DRK_IS_NOT_EXIST(0). 
 *                On error, error number(negative) is returned.
 */
int isExistDeviceRootKey(int drkType);

/**
 * Function : getDrkUID()
 * Description : Read DRK certificate's UID.
 * Parameters : @ drkType : KEY_TYPE_RSA or KEY_TYPE_SYMM. 
 *                          KEY_TYPE_EC is not supported now. 
 *              @ drkUid : Output buffer to save UID.
 *              @ drkUidLen : Output buffer's size.
 * Return value : NO_ERROR(0) is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int getDrkUID(int drkType, char *drkUid, uint32_t drkUidLen);

/**
 * Function : createServiceKeySession()
 * Description : Create service key session and get service key.
 *               Should call releaseServiceKeySession() after parsing service key.
 * Parameters : @ serviceName : service name to ask service key.
 *              @ keyType : KEY_TYPE_RSA or KEY_TYPE_SYMM or KEY_TYPE_EC.
 *              @ isEnableTlv : Use tlv value on making certificate.
 *              @ serviceKey : Output buffer to save encrypted key.
 *              @ serviceKeyLen : Output buffer's size.
 * Return value : Saved servicekey size is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int createServiceKeySession(char *serviceName, int keyType, uint8_t isEnableTlv,
		char *serviceKey, uint32_t serviceKeyLen);

/**
 * Function : releaseServiceKeySession()
 * Description : Release service key session.
 *               It is only affected on QSEE based models and not affected on Mobicore models.
 *               DeviceRootKeyService service should be waited for next operation in 10s 
 *               if this function is not called after calling releaseServiceKeySession().
 * Parameters : None.
 * Return value : NO_ERROR(0) is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int releaseServiceKeySession(void);

/**
 * Function : initTlv()
 * Description : Initialize tlv sturcture for extention fields in certificate.
 * Parameters : None.
 * Return value : NO_ERROR(0) is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int initTlv(void);

/**
 * Function : addTlv()
 * Description : Add tag and value to extention fields in certificate.
 * Parameters : @ tlvTag : Extention field number to add.
 *              @ tlvValue : Tlv value to add. It must be ASN.1 format.
 *              @ tlvValueLen : Length of tlvVlaue.
 * Return value : NO_ERROR(0) is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int addTlv(int tlvTag, char *tlvValue, int tlvValueLen);

/**
 * Function : setTestDeviceRootKey()
 * Description : Install test device root key for developing.
 *               Only approved user can execute this function and it is needed signed raw data.
 * Parameters : @ rawData : Signed raw data.
 *              @ rawDataLen : raw data's length.
 * Return value : NO_ERROR(0) is returned if it is successful.
 *                On error, error number(negative) is returned.
 */
int setTestDeviceRootKey(char *rawData, int rawDataLen);