3232
3333/** Local declarations */
3434static int wh_ServerTask (void * cf , const char * keyFilePath , int keyId ,
35- int clientId );
35+ int clientId , whNvmFlags flags );
3636
3737static void _sleepMs (long milliseconds );
3838#if !defined(WOLFHSM_CFG_NO_CRYPTO )
5656const char * type = "tcp" ; /* default to tcp type */
5757
5858static int loadAndStoreKeys (whServerContext * server , whKeyId * outKeyId ,
59- const char * keyFilePath , int keyId , int clientId )
59+ const char * keyFilePath , int keyId , int clientId ,
60+ whNvmFlags flags )
6061{
6162#if !defined(WOLFHSM_CFG_NO_CRYPTO )
6263 int ret ;
@@ -83,14 +84,14 @@ static int loadAndStoreKeys(whServerContext* server, whKeyId* outKeyId,
8384 ret = 0 ;
8485 close (keyFd );
8586
86- WOLFHSM_CFG_PRINTF (
87- "Loading key from %s (size=%d) with keyId =0x%02X and clientId =0x%01X \n" ,
88- keyFilePath , keySz , keyId , clientId );
87+ WOLFHSM_CFG_PRINTF ("Loading key from %s (size=%d) with keyId=0x%02X and "
88+ "clientId =0x%01X and flags =0x%04X \n" ,
89+ keyFilePath , keySz , keyId , clientId , flags );
8990
9091 /* cache the key in the HSM, get HSM assigned keyId */
9192 /* set the metadata fields */
9293 meta .id = WH_MAKE_KEYID (WH_KEYTYPE_CRYPTO , clientId , keyId );
93- meta .flags = 0 ;
94+ meta .flags = ( whNvmFlags ) flags ;
9495 meta .len = keySz ;
9596 memcpy (meta .label , keyLabel , strlen (keyLabel ));
9697
@@ -99,10 +100,11 @@ static int loadAndStoreKeys(whServerContext* server, whKeyId* outKeyId,
99100 ret = wh_Server_KeystoreGetUniqueId (server , & meta .id );
100101 WOLFHSM_CFG_PRINTF ("got unique ID = 0x%02X\n" , meta .id & WH_KEYID_MASK );
101102 }
102- WOLFHSM_CFG_PRINTF (
103- "key NVM ID = 0x%04X\n\ttype=0x%01X\n\tuser=0x%01X\n\tkeyId=0x%02X\n" ,
104- meta .id , WH_KEYID_TYPE (meta .id ), WH_KEYID_USER (meta .id ),
105- WH_KEYID_ID (meta .id ));
103+ WOLFHSM_CFG_PRINTF ("key NVM ID = "
104+ "0x%04X\n\ttype=0x%01X\n\tuser=0x%01X\n\tkeyId=0x%"
105+ "02X\n\tflags=0x%04X\n" ,
106+ meta .id , WH_KEYID_TYPE (meta .id ), WH_KEYID_USER (meta .id ),
107+ WH_KEYID_ID (meta .id ), meta .flags );
106108
107109 if (ret == 0 ) {
108110 ret = wh_Server_KeystoreCacheKey (server , & meta , keyBuf );
@@ -124,12 +126,13 @@ static int loadAndStoreKeys(whServerContext* server, whKeyId* outKeyId,
124126 (void )keyFilePath ;
125127 (void )keyId ;
126128 (void )clientId ;
129+ (void )flags ;
127130 return WH_ERROR_NOTIMPL ;
128131#endif /* !WOLFHSM_CFG_NO_CRYPTO */
129132}
130133
131134static int wh_ServerTask (void * cf , const char * keyFilePath , int keyId ,
132- int clientId )
135+ int clientId , whNvmFlags flags )
133136{
134137 whServerContext server [1 ];
135138 whServerConfig * config = (whServerConfig * )cf ;
@@ -146,7 +149,7 @@ static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
146149 /* Load keys into cache if file path is provided */
147150 if (keyFilePath != NULL ) {
148151 ret = loadAndStoreKeys (server , & loadedKeyId , keyFilePath , keyId ,
149- clientId );
152+ clientId , flags );
150153 if (ret != 0 ) {
151154 WOLFHSM_CFG_PRINTF ("server failed to load key, ret=%d\n" , ret );
152155 (void )wh_Server_Cleanup (server );
@@ -206,9 +209,9 @@ static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
206209
207210 /* Reload keys into cache if file path was provided */
208211 if (keyFilePath != NULL ) {
209- ret =
210- loadAndStoreKeys ( server , & loadedKeyId ,
211- keyFilePath , keyId , clientId );
212+ ret = loadAndStoreKeys ( server , & loadedKeyId ,
213+ keyFilePath , keyId , clientId ,
214+ flags );
212215 if (ret != 0 ) {
213216 WOLFHSM_CFG_PRINTF ("server failed to load key, ret=%d\n" ,
214217 ret );
@@ -266,12 +269,13 @@ static int _hardwareCryptoCb(int devId, struct wc_CryptoInfo* info, void* ctx)
266269#endif
267270static void Usage (const char * exeName )
268271{
269- WOLFHSM_CFG_PRINTF ("Usage: %s --key <key_file_path> --id <key_id> --client <client_id> "
270- "--nvminit <nvm_init_file_path> --type <type>\n" ,
271- exeName );
272+ WOLFHSM_CFG_PRINTF (
273+ "Usage: %s --key <key_file_path> --id <key_id> --client <client_id> "
274+ "--nvminit <nvm_init_file_path> --type <type> --flags <flags>\n" ,
275+ exeName );
272276 WOLFHSM_CFG_PRINTF ("Example: %s --key key.bin --id 123 --client 456 "
273- "--nvminit nvm_init.txt --type tcp\n" ,
274- exeName );
277+ "--nvminit nvm_init.txt --type tcp --flags 0 \n" ,
278+ exeName );
275279 WOLFHSM_CFG_PRINTF ("type: tcp (default), shm, dma\n" );
276280}
277281
@@ -283,6 +287,8 @@ int main(int argc, char** argv)
283287 const char * nvmInitFilePath = NULL ;
284288 int keyId = WH_KEYID_ERASED ; /* Default key ID if none provided */
285289 int clientId = 12 ; /* Default client ID if none provided */
290+ whNvmFlags flags =
291+ WH_NVM_FLAGS_USAGE_ANY ; /* Default flags if none provided */
286292 uint8_t memory [WH_POSIX_FLASH_RAM_SIZE ] = {0 };
287293 whServerConfig s_conf [1 ];
288294
@@ -310,6 +316,19 @@ int main(int argc, char** argv)
310316 else if (strcmp (argv [i ], "--type" ) == 0 && i + 1 < argc ) {
311317 type = argv [++ i ];
312318 }
319+ else if (strcmp (argv [i ], "--flags" ) == 0 && i + 1 < argc ) {
320+ char * end ;
321+ errno = 0 ;
322+ unsigned long val = strtoul (argv [i + 1 ], & end , 0 );
323+
324+ if (errno || * end || val > 0xFFFF ) {
325+ WOLFHSM_CFG_PRINTF ("Invalid --flags value: %s\n" , argv [i + 1 ]);
326+ return -1 ;
327+ }
328+
329+ flags = (whNvmFlags )val ;
330+ i ++ ;
331+ }
313332 else {
314333 WOLFHSM_CFG_PRINTF ("Invalid argument: %s\n" , argv [i ]);
315334 Usage (argv [0 ]);
@@ -399,7 +418,7 @@ int main(int argc, char** argv)
399418 return rc ;
400419 }
401420
402- rc = wh_ServerTask (s_conf , keyFilePath , keyId , clientId );
421+ rc = wh_ServerTask (s_conf , keyFilePath , keyId , clientId , flags );
403422 if (rc != WH_ERROR_OK ) {
404423 WOLFHSM_CFG_PRINTF ("Server task failed: %d\n" , rc );
405424 return rc ;
@@ -418,7 +437,7 @@ int main(int argc, char** argv)
418437 (void )keyFilePath ;
419438 (void )keyId ;
420439 (void )clientId ;
421- rc = wh_ServerTask (s_conf , keyFilePath , keyId , clientId );
440+ rc = wh_ServerTask (s_conf , keyFilePath , keyId , clientId , flags );
422441 if (rc != WH_ERROR_OK ) {
423442 WOLFHSM_CFG_PRINTF ("Server task failed: %d\n" , rc );
424443 return rc ;
0 commit comments