/********************************* (C) РОТЕК ***********************************
 * @module  cert_req
 * @file    cert_req.c
 * @version 1.0.0
 * @date    XX.XX.XXXX
 *******************************************************************************
 * @history     Version  Author         Comment
 * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
 *******************************************************************************
 */
#if 0
#include "parse_rsa.h"

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
#include "mbedtls/memory_buffer_alloc.h"
#endif

#include "mbedtls/platform.h"

#include "mbedtls/x509_csr.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/error.h"
#include "mbedtls/certs.h"

#include "mbedtls/md4.h"

#include "settings_api.h"

#ifdef PRINTF_STDLIB
#include <stdio.h>
#endif
#ifdef PRINTF_CUSTOM
#include "tinystdio.h"
#endif
#include <stdlib.h>
#include <string.h>

extern SETTINGS_t sSettings;

/*
 * For heap usage estimates, we need an estimate of the overhead per allocated
 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
 * so use that as our baseline.
 */
#define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) )

#define MEMORY_MEASURE_INIT                                             \
    size_t max_used, max_blocks, max_bytes;                             \
    size_t prv_used, prv_blocks;                                        \
    mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks );      \
    mbedtls_memory_buffer_alloc_max_reset( );

#define MEMORY_MEASURE_PRINT( title_len )                               \
    mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks );      \
    max_used -= prv_used;                                               \
    max_blocks -= prv_blocks;                                           \
    max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks;             \
    mbedtls_printf( "%6u heap bytes\r\n", (unsigned) max_bytes );

#define RSA_PRIVATE_KEY_2048 ""                                            

#define HEAP_SIZE       (1u << 14)  // 16k
unsigned char malloc_buf[HEAP_SIZE];

void SSL_ParseRsaKey()
{
    mbedtls_pk_context pk;
    const char *rsa_keys = RSA_PRIVATE_KEY_2048;
    int ret;
    
    MEMORY_MEASURE_INIT
    
    //mbedtls_memory_buffer_alloc_self_test(1);
    
    mbedtls_memory_buffer_alloc_init(malloc_buf, sizeof(malloc_buf));
    //mbedtls_memory_buffer_alloc_status();    
    //MEMORY_MEASURE_PRINT(5)
    
    mbedtls_printf( "\r\n  . Loading the key..." );
    
    mbedtls_pk_init( &pk );
    ret = mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys,
                                strlen(rsa_keys) + 1, NULL, 0 );
    
    if( ret != 0 )
        mbedtls_printf( " failed\r\n  !  mbedtls_pk_parse_key returned %d\r\n", ret );
    else
        mbedtls_printf( " ok\r\n");
    
    //MEMORY_MEASURE_PRINT(5)      
    //mbedtls_memory_buffer_alloc_status();
}

#endif

/********************************* (C) РОТЕК **********************************/
