Skip to content

Commit 264d9a8

Browse files
committed
relax NULL checks in freshen key and helper function to support optional
output arguments
1 parent 0f0072a commit 264d9a8

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/wh_server_keystore.c

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ static int _GetKeyCacheSlot(whKeyCacheContext* ctx, uint16_t keySz,
144144
{
145145
int foundIndex = -1;
146146
int i;
147+
uint8_t* slotBuf = NULL;
148+
whNvmMetadata* slotMeta = NULL;
147149

148-
if (ctx == NULL || outBuf == NULL || outMeta == NULL) {
150+
if (ctx == NULL) {
149151
return WH_ERROR_BADARGS;
150152
}
151153

@@ -169,11 +171,11 @@ static int _GetKeyCacheSlot(whKeyCacheContext* ctx, uint16_t keySz,
169171
}
170172
}
171173

172-
/* Zero slot and return pointers */
174+
/* Zero slot and capture pointers */
173175
if (foundIndex >= 0) {
174176
memset(&ctx->cache[foundIndex], 0, sizeof(whCacheSlot));
175-
*outBuf = ctx->cache[foundIndex].buffer;
176-
*outMeta = ctx->cache[foundIndex].meta;
177+
slotBuf = ctx->cache[foundIndex].buffer;
178+
slotMeta = ctx->cache[foundIndex].meta;
177179
}
178180
}
179181
else {
@@ -195,18 +197,26 @@ static int _GetKeyCacheSlot(whKeyCacheContext* ctx, uint16_t keySz,
195197
}
196198
}
197199

198-
/* Zero slot and return pointers */
200+
/* Zero slot and capture pointers */
199201
if (foundIndex >= 0) {
200202
memset(&ctx->bigCache[foundIndex], 0, sizeof(whBigCacheSlot));
201-
*outBuf = ctx->bigCache[foundIndex].buffer;
202-
*outMeta = ctx->bigCache[foundIndex].meta;
203+
slotBuf = ctx->bigCache[foundIndex].buffer;
204+
slotMeta = ctx->bigCache[foundIndex].meta;
203205
}
204206
}
205207

206208
if (foundIndex == -1) {
207209
return WH_ERROR_NOSPACE;
208210
}
209211

212+
/* Copy out pointers only if caller provided non-NULL output parameters */
213+
if (outBuf != NULL) {
214+
*outBuf = slotBuf;
215+
}
216+
if (outMeta != NULL) {
217+
*outMeta = slotMeta;
218+
}
219+
210220
return WH_ERROR_OK;
211221
}
212222

@@ -490,17 +500,25 @@ static int _ExistsInCache(whServerContext* server, whKeyId keyId)
490500
int wh_Server_KeystoreFreshenKey(whServerContext* server, whKeyId keyId,
491501
uint8_t** outBuf, whNvmMetadata** outMeta)
492502
{
493-
int ret = 0;
494-
int foundIndex = -1;
495-
int foundBigIndex = -1;
496-
whNvmMetadata tmpMeta[1];
503+
int ret = 0;
504+
int foundIndex = -1;
505+
int foundBigIndex = -1;
506+
uint8_t* cacheBufLocal = NULL;
507+
whNvmMetadata* cacheMetaLocal = NULL;
508+
uint8_t** cacheBufOut;
509+
whNvmMetadata** cacheMetaOut;
510+
whNvmMetadata tmpMeta[1];
497511

498512
if ((server == NULL) || WH_KEYID_ISERASED(keyId)) {
499513
return WH_ERROR_BADARGS;
500514
}
501515

502-
ret = _FindInCache(server, keyId, &foundIndex, &foundBigIndex, outBuf,
503-
outMeta);
516+
/* Use local buffers to allow for optional (NULL) output parameters */
517+
cacheBufOut = (outBuf != NULL) ? outBuf : (uint8_t**)&cacheBufLocal;
518+
cacheMetaOut = (outMeta != NULL) ? outMeta : &cacheMetaLocal;
519+
520+
ret = _FindInCache(server, keyId, &foundIndex, &foundBigIndex, cacheBufOut,
521+
cacheMetaOut);
504522
if (ret != WH_ERROR_OK) {
505523
/* For wrapped keys, just probe the cache and error if not found. We
506524
* don't support automatically unwrapping and caching outside of the
@@ -514,19 +532,21 @@ int wh_Server_KeystoreFreshenKey(whServerContext* server, whKeyId keyId,
514532
if (ret == WH_ERROR_OK) {
515533
/* Key found in NVM, get a free cache slot */
516534
ret = wh_Server_KeystoreGetCacheSlot(server, keyId, tmpMeta->len,
517-
outBuf, outMeta);
535+
cacheBufOut, cacheMetaOut);
518536
if (ret == WH_ERROR_OK) {
519537
/* Read the key from NVM into the cache slot */
520-
ret = wh_Nvm_Read(server->nvm, keyId, 0, tmpMeta->len, *outBuf);
538+
ret = wh_Nvm_Read(server->nvm, keyId, 0, tmpMeta->len,
539+
*cacheBufOut);
521540
if (ret == WH_ERROR_OK) {
522541
/* Copy the metadata to the cache slot if key read is
523542
* successful*/
524-
memcpy((uint8_t*)*outMeta, (uint8_t*)tmpMeta,
543+
memcpy((uint8_t*)*cacheMetaOut, (uint8_t*)tmpMeta,
525544
sizeof(whNvmMetadata));
526545
}
527546
}
528547
}
529548
}
549+
530550
return ret;
531551
}
532552

0 commit comments

Comments
 (0)