Skip to content

Commit b0e82af

Browse files
committed
review feedback
1 parent 54c95ad commit b0e82af

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/wh_server_cert.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,13 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
503503
else {
504504
rc = wh_Server_KeystoreReadKey(
505505
server, certId, NULL, cert_data, &cert_len);
506-
resp.cert_len = cert_len;
506+
if (rc == WH_ERROR_OK) {
507+
resp.cert_len = cert_len;
508+
}
509+
else if (rc == WH_ERROR_NOSPACE) {
510+
resp.cert_len = cert_len;
511+
rc = WH_ERROR_BUFFER_SIZE;
512+
}
507513
}
508514
}
509515
}
@@ -670,6 +676,9 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
670676
cert_len = req.cert_len;
671677
resp.rc = wh_Server_KeystoreReadKey(
672678
server, certId, NULL, cert_data, &cert_len);
679+
if (resp.rc == WH_ERROR_NOSPACE) {
680+
resp.rc = WH_ERROR_BUFFER_SIZE;
681+
}
673682
}
674683
}
675684
}

src/wh_server_keystore.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,16 @@ int wh_Server_KeystoreReadKey(whServerContext* server, whKeyId keyId,
647647
/* Check the cache using unified function */
648648
ret = _FindInCache(server, keyId, NULL, NULL, &cacheBuffer, &cacheMeta);
649649
if (ret == WH_ERROR_OK) {
650-
/* Found in cache */
651-
if (cacheMeta->len > *outSz)
652-
return WH_ERROR_NOSPACE;
650+
/* Found in cache - always populate metadata if requested */
653651
if (outMeta != NULL) {
654652
memcpy((uint8_t*)outMeta, (uint8_t*)cacheMeta,
655653
sizeof(whNvmMetadata));
656654
}
655+
/* Check buffer size only when data output is requested */
656+
if (out != NULL && cacheMeta->len > *outSz) {
657+
*outSz = cacheMeta->len;
658+
return WH_ERROR_NOSPACE;
659+
}
657660
if (out != NULL) {
658661
memcpy(out, cacheBuffer, cacheMeta->len);
659662
}

wolfhsm/wh_server_cert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int wh_Server_CertEraseTrusted(whServerContext* server, whNvmId id);
6767
* @param id The key ID of the certificate to read. If the key type is
6868
* WH_KEYTYPE_NVM, the certificate is read from NVM. Otherwise, the certificate
6969
* is read from the keystore cache (e.g. for wrapped certs cached via
70-
* wh_Client_KeyUnwrapAndCache).
70+
* wh_Client_CertUnwrapAndCache).
7171
* @param cert Buffer to store the certificate data
7272
* @param inout_cert_len On input, size of cert buffer. On output, actual cert
7373
* size
@@ -85,7 +85,7 @@ int wh_Server_CertReadTrusted(whServerContext* server, whKeyId id,
8585
* @param cert_len Length of the certificate data
8686
* @param trustedRootId Key ID of the trusted root certificate. Can be an NVM ID
8787
* (WH_KEYTYPE_NVM) or a cached key ID (e.g. WH_KEYTYPE_WRAPPED from
88-
* wh_Client_KeyUnwrapAndCache).
88+
* wh_Client_CertUnwrapAndCache).
8989
* @param flags Flags for the certificate verification (see WH_CERT_FLAGS_* in
9090
* wh_common.h)
9191
* @param cachedKeyFlags NVM usage flags to apply when caching the leaf public

0 commit comments

Comments
 (0)