#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include<stdbool.h>
#include <SecCryptoMgrCommon.h>
#include <TestKeySE.h>
#include <Log.h>
#include <SecryptoIntf.h>
#if defined(DLT_ENABLED)
#include <dlt/dlt.h>
#endif
#define TEST_TOOL_VERSION "202209_0.1V"
#define MAX_SIZE 100
#define CMD_USAGE 2
#define scanStart(scan) ((scan)->scanTos = -1)
#define scanUngetc(scan) ((scan)->scanPtr[(scan)->scanTos]--)
#include<unistd.h>
#define max_str_len 150
char strkey[max_str_len];
int str_key_len;
char* charkey;
char *c_next;
char c[300] = {0};
char *ret = NULL;
void cleanUp(char *s)
{
char *d;
d = s;
while (*s)
{
if (*s == 8)
{
/* adjust for backspace */
s++;
d--;
}
else
{
*d++ = *s++;
}
}
*d = 0;
}
static void scanPush(tScan *scan, const char *s)
{
if (scan->scanTos < SCAN_DEPTH - 1)
{
scan->scanStack[++scan->scanTos] = s;
scan->scanPtr[scan->scanTos] = s;
}
}
static int32_t scanGetc(tScan *scan)
{
int32_t c;
for (c = 0; scan->scanTos >= 0; scan->scanTos--)
if ((c = *scan->scanPtr[scan->scanTos]++) != 0)
break;
return c;
}
static int32_t parseStartWord(tArgs *a, char *w)
{
if (a->aArgc >= ARGS_CNT)
{
return(-1);
}
else
{
a->aArgv[a->aArgc++] = w;
return(0);
}
}
int32_t diagParseArgs(const char *s, char **sRet, tArgs *a)
{
int32_t inDQ = false; /* True if in double quote */
int32_t inSQ = false; /* True if in single quote */
int32_t inW = false; /* True if in word */
char *d; /* Destination of copy */
char *e; /* End of buffer (last char) */
int32_t c;
tScan scan;
d = a->aBuffer;
e = a->aBuffer + sizeof (a->aBuffer) - 1;
a->aArgc = 0; /* Start at 0 please */
a->aArg = 0;
if (!s) /* Handle NULL string */
{
if (sRet)
{
*sRet = 0;
}
return 0;
}
scanStart(&scan);
scanPush(&scan, s);
while (1)
{
c = scanGetc(&scan);
if ('\\' == c) /* Escape char */
{
c = scanGetc(&scan);
if (c == '\0')
{
dprintf(LOG_CRITICAL,"ERROR: Can't escape EOL\n");
return(-1);
}
else
{
if (!inW)
{
if (parseStartWord(a, d))
{
return(-1);
}
inW = true;
}
if (d < e)
{
*d++ = c;
}
}
}
else if (isspace(c) || (';' == c) || (0 == c))
{
if (inDQ || inSQ) /* In quote - copy */
{
if (c == 0)
{
dprintf(LOG_CRITICAL,"ERROR: Command line ended "
"while in a quoted string\n");
return(-1);
}
if (d < e)
{
*d++ = c;
}
continue;
}
else if (inW) /* In word - break */
{
*d = 0;
if (d < e)
{
d++;
}
inW = false;
}
if (';' == c || 0 == c)
{
/*
* If end of line or statement, then update return
* pointer and finish up
*/
break;
}
}
else
{
/*
* Nothing special, if not in a word, then start new arg.
*/
if (!inW)
{
if (parseStartWord(a, d))
{
return(-1);
}
inW = true;
}
if ('"' == c && !inSQ)
{
inDQ = !inDQ;
}
else if ('\'' == c && !inDQ)
{
inSQ = !inSQ;
}
else
{
if (d < e)
{
*d++ = c;
}
}
}
}
if (sRet) /* Update sRet if non-NULL */
{
*sRet = ((c != 0 && scan.scanTos == 0) ?
(char *) scan.scanPtr[scan.scanTos] :
NULL);
}
return(0);
}
int32_t testSecGetDataSize(SECCRYPT_HANDLE handle,char* charkey)
{
size_t getDataSize = 0;
int32_t retStatus = SECCRYPTO_OK;
retStatus = seccryptoGetDataSz(handle,charkey,&getDataSize);
if (SECCRYPTO_OK == retStatus)
{
dprintf(LOG_INFO,"\033[1;32m");
dprintf(LOG_INFO,"[%s] : KEY is present : KeySize [%ld] \n",charkey,getDataSize);
}
else
{
dprintf(LOG_INFO,"\033[1;31m");
dprintf(LOG_INFO,"[%s] : KEY is not present \n",charkey);
}
dprintf(LOG_INFO,"\033[0m");
return EXIT_SUCCESS;
}
int32_t Check_status_of_key(SECCRYPT_HANDLE handle)
{
int32_t retStatus = SECCRYPTO_OK;
char *st_key = NULL;
char st_IDkey[max_str_len] = {0};
int str_IDkeylen = 0;
FILE *keyfile = fopen("/usr/bin/StorageID.txt", "r");
if (keyfile == NULL) {
dprintf(LOG_CRITICAL,"Error: file is not found or keys are not present in the file.\n");
exit(1);
}
while (fgets(st_IDkey, max_str_len, keyfile))
{
str_IDkeylen = strlen(st_IDkey);
st_key = (char*) malloc (MAX_SIZE * sizeof(char));
memset(st_key, 0, MAX_SIZE);
strncpy(st_key, st_IDkey, str_IDkeylen-1);
retStatus = testSecGetDataSize(handle,st_key);
}
free(st_key);
fclose(keyfile);
return retStatus;
}
int32_t testSecEraseData(SECCRYPT_HANDLE handle,char* delkey)
{
int32_t retStatus = SECCRYPTO_OK;
retStatus = seccryptoEraseData(handle,delkey);
if (SECCRYPTO_OK == retStatus)
{
dprintf(LOG_INFO,"\033[1;32m");
dprintf(LOG_INFO,"[%s] : KEY deleted \n",delkey);
}
else
{
dprintf(LOG_INFO,"\033[1;31m");
dprintf(LOG_INFO,"[%s] : KEY is not present \n",delkey);
}
dprintf(LOG_INFO,"\033[0m");
return EXIT_SUCCESS;
}
int32_t delete_key(SECCRYPT_HANDLE handle)
{
int32_t retStatus = SECCRYPTO_OK;
char *del_key = NULL;
char delkey[max_str_len] = {0};
int str_Keylen = 0;
FILE *delfile = fopen("/usr/bin/DeleteID.txt", "r");
if (delfile == NULL) {
dprintf(LOG_CRITICAL,"Error: file is not found or keys are not present in the file.\n");
exit(1);
}
while (fgets(delkey, max_str_len, delfile))
{
str_Keylen = strlen(delkey);
del_key = (char*) malloc (MAX_SIZE * sizeof(char));
memset(del_key, 0, MAX_SIZE);
strncpy(del_key, delkey, str_Keylen-1);
retStatus = testSecEraseData(handle, del_key);
}
free(del_key);
fclose(delfile);
return retStatus;
}
int32_t mgmtCmd(SECCRYPT_HANDLE handle,tArgs *args)
{
int32_t retStatus = SECCRYPTO_OK;
char *c;
if (ARG_CNT(args) < 1)
{
return CMD_USAGE;
}
/* Command or prefix */
c = ARG_GET(args);
if (!strcmp(c, "help"))
{
retStatus = CMD_USAGE;
}
if (!strcmp(c, "1"))
{
dprintf(LOG_CRITICAL,"To check Key status, for storage ID present in storageID.txt\n");
retStatus = Check_status_of_key(handle);
}
else if (!strcmp(c, "2"))
{
dprintf(LOG_CRITICAL,"To delete the Keys, for storage ID present in deleteStorageID.txt\n");
retStatus = delete_key(handle);
}
else
{
retStatus = CMD_USAGE;
}
if(!strcmp(c,"exit"))
{
dprintf(LOG_CRITICAL,"Exiting\n");
exit(0);
}
return retStatus;
}
char mgmtCmdUsage[] =
"\tType 1: To check Key status, for storage ID present in storageID.txt\n"
"\tType 2: To delete the Keys, for storage ID present in deleteStorageID.txt\n"
"\texit\n"
"\n";
int32_t main(int32_t argc, const char* argv[])
{
char clientName[64] = "seccryptoclient";
SECCRYPT_HANDLE handle = NULL;
int32_t retStatus = SECCRYPTO_OK;
tArgs a;
handle = secCryptoInitClntSrvc(clientName);
if ( NULL == handle)
{
#if defined(DLT_ENABLED)
DLT_UNREGISTER_APP();
#endif
return EXIT_FAILURE;
}
dprintf(LOG_INFO,"\ncrypto>");
dprintf(LOG_INFO,"%s",mgmtCmdUsage);
while (true)
{
dprintf(LOG_INFO,"\ncrypto>");
/* Fetch the command from the CLI interface */
ret = fgets(c, sizeof (c), stdin);
if (NULL == ret)
{
secCryptoCtrlCloseDiagSrvc(handle);
exit(0);
}
cleanUp(c);
/* Parse the input command and parameters */
/* Parses up to ; or EOL */
if (diagParseArgs(c, &c_next, &a))
{
dprintf(LOG_CRITICAL,"command failed\n");
}
#if DEBUG
parse_arg_dump(&a);
#endif
/* Execute the management command */
retStatus = mgmtCmd(handle, &a);
if (CMD_USAGE == retStatus)
{
dprintf(LOG_INFO,"%s",mgmtCmdUsage);
}
else if (SECCRYPTO_OK != retStatus)
{
dprintf(LOG_CRITICAL,"Error: command execution failed\n");
secCryptoCtrlCloseDiagSrvc(handle);
}
}
}
---------------------------------------------------------------------------------
header file ---------------------------------------------------------------------------------
#ifndef _DIAG_ARGS_H
#define _DIAG_ARGS_H
#include <SecryptoIntf.h>
#define ARGS_CNT 10 /* Max argv entries */
#define ARGS_BUFFER 1024 /* # bytes total for arguments */
/*
* Define: PARSE_VARIABLE_PREFIX1/PREFIX2/SYSTEM
* Purpose: Defines the character that indicate a variable reference,
* and the character the indicates a system variable.
*/
#define PARSE_VARIABLE_PREFIX1 '@' /* Deprecated */
#define PARSE_VARIABLE_PREFIX2 '$'
#define PARSE_VARIABLE_SYSTEM '_'
/*
* Typedef: parse_key_t
* Purpose: Type used as FIRST entry in structures passed to
* parse_lookup.
*/
typedef char *tParseKey;
/*
* Define: PARSE_ENTRIES
* Purpose: Determine the number of elements in a parse table.
*/
#define PARSE_ENTRIES(_p) COUNTOF(_p)
/*
* Typedef: args_t
* Purpose: argc/argv (sorta) structure for parsing arguments.
*
* If the macro ARG_GET is used to consume parameters, unsued parameters
* may be passed to lower levels by simply passing the args_t struct.
*/
typedef struct args
{
tParseKey aCmd; /* Initial string */
char *aArgv[ARGS_CNT]; /* argv pointers */
char aBuffer[ARGS_BUFFER]; /* Split up buffer */
int aArgc; /* Parsed arg counter */
int aArg; /* Pointer to NEXT arg */
} tArgs;
#define ARG_CMD(_a) (_a)->aCmd
#define _ARG_CUR(_a) \
((_a)->a_argv[(_a)->aArg])
#define ARG_CUR(_a) \
(((_a)->aArg >= (_a)->aArgc) ? NULL : _ARG_CUR(_a))
#define _ARG_GET(_a) \
((_a)->aArgv[(_a)->aArg++])
#define ARG_GET(_a) \
(((_a)->aArg >= (_a)->aArgc) ? NULL : _ARG_GET(_a))
#define ARG_NEXT(_a) (_a)->aArg++
#define ARG_PREV(_a) (_a)->aArg--
#define ARG_DISCARD(_a) \
((_a)->aArg = (_a)->aArgc)
/*
* Macro: ARG_CNT
* Purpose: Return the number of unconsumed arguments.
*/
#define ARG_CNT(_a) ((_a)->aArgc - (_a)->aArg)
/* parse.c exported routines */
extern int diagParseArgs(const char *s, char **sRet, tArgs *);
int32_t mgmtCmd(SECCRYPT_HANDLE handle, tArgs *args);
void cleanUp(char *s);
void dumpHex(const void* data, size_t size);
#define SCAN_DEPTH 10
typedef struct scan
{
const char *scanStack[SCAN_DEPTH];
const char *scanPtr[SCAN_DEPTH];
int scanTos;
}tScan;
#endif /* _DIAG_ARGS_H */
/* Comment below line to print of the logs */
#define LOG_LEVEL LOG_INFO
/* Log Level */
enum log_level {
LOG_CRITICAL=0,
LOG_ALWAYS=0,
LOG_INFO,
LOG_SPEW,
};
/* If LOG_LEVEL is not defined, by default it will print all the logs. */
#ifndef LOG_LEVEL
#define LOG_LEVEL 2
#endif
#define dprintf(level, fmt, args...) (level <= LOG_LEVEL) ? fprintf(stderr, ""fmt"", ## args) : fprintf(stderr," ")
To embed this program on your website, copy the following code and paste it into your website's HTML: