Skip to content

Commit a6b484b

Browse files
add more macro guards around authentication feature, use XSTRLEN for portability, zero out sensitive data before function return
1 parent bebc912 commit a6b484b

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

src/wh_auth.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
/* Pick up compile-time configuration */
4040
#include "wolfhsm/wh_settings.h"
4141

42+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
43+
4244
#include <stdint.h>
4345
#include <stddef.h>
4446
#include <string.h>
@@ -449,3 +451,5 @@ int wh_Auth_Unlock(whAuthContext* auth)
449451
return wh_Lock_Release(&auth->lock);
450452
}
451453
#endif /* WOLFHSM_CFG_THREADSAFE */
454+
455+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */

src/wh_auth_base.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
/* Pick up compile-time configuration */
2424
#include "wolfhsm/wh_settings.h"
2525

26+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
27+
2628
#include <stdint.h>
2729
#include <stddef.h>
2830
#include <string.h>
@@ -111,14 +113,16 @@ static whAuthBase_User* wh_Auth_BaseCheckPin(const char* username,
111113
const void* auth_data,
112114
uint16_t auth_data_len)
113115
{
114-
whAuthBase_User* found_user;
116+
whAuthBase_User* found_user = NULL;
117+
whAuthBase_User* ret = NULL;
115118
unsigned char authCheck[WH_AUTH_BASE_MAX_CREDENTIALS_LEN];
116119
uint16_t authCheck_len;
117120
int rc;
118121

119122
/* Process auth_data: hash if crypto enabled, copy if disabled */
120123
rc = wh_Auth_BaseHashPin(auth_data, auth_data_len, authCheck);
121124
if (rc != WH_ERROR_OK) {
125+
wh_Utils_ForceZero(authCheck, sizeof(authCheck));
122126
return NULL;
123127
}
124128
#ifndef WOLFHSM_CFG_NO_CRYPTO
@@ -132,9 +136,11 @@ static whAuthBase_User* wh_Auth_BaseCheckPin(const char* username,
132136
found_user->credentials_len == authCheck_len &&
133137
wh_Utils_ConstantCompare(found_user->credentials, authCheck,
134138
authCheck_len) == 0) {
135-
return found_user;
139+
ret = found_user;
136140
}
137-
return NULL;
141+
142+
wh_Utils_ForceZero(authCheck, sizeof(authCheck));
143+
return ret;
138144
}
139145

140146
#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO)
@@ -553,3 +559,5 @@ int wh_Auth_BaseUserSetCredentials(void* context, uint16_t user_id,
553559
(void)auth_context;
554560
return rc;
555561
}
562+
563+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */

src/wh_client_auth.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
/* Pick up compile-time configuration */
2626
#include "wolfhsm/wh_settings.h"
2727

28-
#ifdef WOLFHSM_CFG_ENABLE_CLIENT
28+
#if defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
29+
defined(WOLFHSM_CFG_ENABLE_AUTHENTICATION)
2930

3031
/* System libraries */
3132
#include <string.h> /* For memcpy, strncpy */
@@ -43,16 +44,16 @@
4344
#include "wolfhsm/wh_utils.h"
4445

4546
/* Does not find the user name in the list, only verifies that the user name is
46-
* not too long and not null. */
47+
* not NULL, not empty, and not too long. */
4748
static int _UserNameIsValid(const char* username)
4849
{
4950
size_t len;
5051

51-
if (username == NULL) {
52+
if (username == NULL || username[0] == '\0') {
5253
return 0;
5354
}
5455

55-
len = strnlen(username, WH_MESSAGE_AUTH_MAX_USERNAME_LEN);
56+
len = XSTRLEN(username);
5657
return (len < WH_MESSAGE_AUTH_MAX_USERNAME_LEN);
5758
}
5859

@@ -726,4 +727,4 @@ int wh_Client_AuthUserSetCredentials(
726727
return rc;
727728
}
728729

729-
#endif /* WOLFHSM_CFG_ENABLE_CLIENT */
730+
#endif /* WOLFHSM_CFG_ENABLE_CLIENT && WOLFHSM_CFG_ENABLE_AUTHENTICATION */

src/wh_message_auth.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
/* Pick up compile-time configuration */
2626
#include "wolfhsm/wh_settings.h"
2727

28+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
29+
2830
#include <stdint.h>
2931
#include <stddef.h>
3032
#include <string.h>
@@ -368,3 +370,5 @@ int wh_MessageAuth_TranslateUserSetCredentialsRequest(
368370

369371
return 0;
370372
}
373+
374+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */

src/wh_server.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ int wh_Server_Init(whServerContext* server, whServerConfig* config)
8585

8686
memset(server, 0, sizeof(*server));
8787
server->nvm = config->nvm;
88+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
8889
server->auth = config->auth;
90+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
8991

9092
#ifndef WOLFHSM_CFG_NO_CRYPTO
9193
server->crypto = config->crypto;

src/wh_server_auth.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
/* Pick up compile-time configuration */
2626
#include "wolfhsm/wh_settings.h"
2727

28-
#ifdef WOLFHSM_CFG_ENABLE_SERVER
28+
#if defined(WOLFHSM_CFG_ENABLE_SERVER) && \
29+
defined(WOLFHSM_CFG_ENABLE_AUTHENTICATION)
2930

3031
/* System libraries */
3132
#include <stdint.h>
@@ -285,4 +286,4 @@ int wh_Server_HandleAuthRequest(whServerContext* server, uint16_t magic,
285286
return rc;
286287
}
287288

288-
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
289+
#endif /* WOLFHSM_CFG_ENABLE_SERVER && WOLFHSM_CFG_ENABLE_AUTHENTICATION */

src/wh_utils.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ int wh_Utils_memeqzero(uint8_t* buffer, uint32_t size)
9595
* Uses volatile to prevent the compiler from optimizing away the writes. */
9696
void wh_Utils_ForceZero(void* mem, uint32_t size)
9797
{
98-
volatile uint8_t* p = (volatile uint8_t*)mem;
98+
volatile uint8_t* p;
99+
100+
if (mem == NULL || size == 0) {
101+
return;
102+
}
103+
104+
p = (volatile uint8_t*)mem;
99105
while (size > 0) {
100106
*p = 0;
101107
p++;

test/wh_test_clientserver.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,8 +1740,10 @@ static int wh_ClientServer_PosixMemMapThreadTest(whTestNvmBackendType nvmType)
17401740
.crypto = crypto,
17411741
#endif
17421742
}};
1743+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
17431744
s_conf->auth = NULL; /* For non authenticated tests set auth context to NULL
17441745
* which avoids authentication checks. */
1746+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
17451747

17461748
WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf));
17471749

wolfhsm/wh_server.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ typedef struct whServerContext_t whServerContext;
4040
#include "wolfhsm/wh_comm.h"
4141
#include "wolfhsm/wh_keycache.h"
4242
#include "wolfhsm/wh_nvm.h"
43+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
4344
#include "wolfhsm/wh_auth.h"
45+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
4446
#include "wolfhsm/wh_message_customcb.h"
4547
#include "wolfhsm/wh_log.h"
4648
#ifdef WOLFHSM_CFG_DMA
@@ -139,7 +141,9 @@ typedef struct {
139141
typedef struct whServerConfig_t {
140142
whCommServerConfig* comm_config;
141143
whNvmContext* nvm;
144+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
142145
whAuthContext* auth;
146+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
143147

144148
#ifndef WOLFHSM_CFG_NO_CRYPTO
145149
whServerCryptoContext* crypto;
@@ -162,7 +166,9 @@ typedef struct whServerConfig_t {
162166
/* Context structure to maintain the state of an HSM server */
163167
struct whServerContext_t {
164168
whNvmContext* nvm;
169+
#ifdef WOLFHSM_CFG_ENABLE_AUTHENTICATION
165170
whAuthContext* auth;
171+
#endif /* WOLFHSM_CFG_ENABLE_AUTHENTICATION */
166172
whCommServer comm[1];
167173
#ifndef WOLFHSM_CFG_NO_CRYPTO
168174
whServerCryptoContext* crypto;

0 commit comments

Comments
 (0)