/**
 * @file       drk_parser.h
 * @brief      Find certificates in DRK certificates blob. Find RSA parameters
 *             in DRK private key blob.
 * @author     Oleksandr Fadieiev (o.fadieiev@samsung.com)
 * @version    1.0
 * @date       Created April 20, 2017
 * @copyright  In Samsung Ukraine R&D Center (SURC) under a contract between
 * @par        LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * @par        "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 * @par        Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
 */
#ifndef _SOURCES_DRK_PARSER_H_
#define _SOURCES_DRK_PARSER_H_

#include <stdint.h>
#include <stdlib.h>

typedef enum {
  kDrkResultSuccess = 0,
  kDrkResultErrorGeneric,
  kDrkResultErrorBadParameters,
  kDrkResultErrorShortBuffer
} DrkResult;

/**
 * Retrieves public and private key certificates from DRK TLV structure.
 * DRK blob has a special format.
 *      | 0x01 | 2 bytes of length | DRK cert | 0x01 | 2 bytes of length | RSA cert ->
 *   -> | 0x03 | 2 bytes of length | RSA priv | 0x04 | 2 bytes of length | TID
 *
 * @param[in] certificates, certificates_size DRK blob with public and private certificates.
 * @param[out] drk_cert, drk_cert_size DRK certificate in x.509 format
 * @param[out] rsa_cert, rsa_cert_size DRK session RSA certificate in x.509 format
 * @param[out] rsa_priv, rsa_priv_size DRK session RSA private key certificate in specific ASN.1 format
 *
 * @return kDrkResultSuccess in case of success, kDrkResultError in case of error.
 */
 DrkResult DrkParserGetCertificates(const void *certificates,
                                   size_t certificates_size,
                                   const void **drk_cert, size_t *drk_cert_size,
                                   const void **rsa_cert, size_t *rsa_cert_size,
                                   const void **rsa_priv, size_t *rsa_priv_size);

/**
 * Retrieves RSA parameters in ASN.1 structure of private key certificate.
 * Format of the private key certificate is the following:
 *   SEQUENCE {
 *     version INTEGER,
 *     modulus INTEGER, -- n
 *     exponent INTEGER, -- e
 *     private key INTEGER, -- d
 *     prime1 INTEGER,
 *     prime2 INTEGER,
 *     exponent1 INTEGER,
 *     exponent2 INTEGER,
 *     coefficient INTEGER
 *   }
 *
 * @param[in] private_cert, private_cert_size Private key certificate in ASN.1 format
 * @param[out] n, n_size Output pointer to RSA modulus in @p private_cert and its size
 * @param[out] e, e_size Output pointer to RSA exponent in @p private_cert and its size
 * @param[out] d, d_size Output pointer to RSA private key in @p private_cert and its size
 *
 * @return kDrkResultSuccess in case of success, kDrkResultError in case of error.
 */
DrkResult DrkParserGetRsaParameters(const void *private_cert,
                                    size_t private_cert_size,
                                    const void **n, size_t *n_size,
                                    const void **e, size_t *e_size,
                                    const void **d, size_t *d_size,
                                    const void **prime1, size_t *prime1_size,
                                    const void **prime2, size_t *prime2_size,
                                    const void **exp1, size_t *exp1_size,
                                    const void **exp2, size_t *exp2_size,
                                    const void **coef, size_t *coef_size);


#endif // _SOURCES_DRK_PARSER_H_
