|
43 | 43 | #include "wolfhsm/wh_message_comm.h" |
44 | 44 | #include "wolfhsm/wh_message_nvm.h" |
45 | 45 | #include "wolfhsm/wh_message_auth.h" |
| 46 | +#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO) |
| 47 | +#include "wolfhsm/wh_message_cert.h" |
| 48 | +#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */ |
46 | 49 |
|
47 | 50 | /* Server API's */ |
48 | 51 | #include "wolfhsm/wh_server.h" |
@@ -319,6 +322,171 @@ static int _wh_Server_HandlePkcs11Request(whServerContext* server, |
319 | 322 | return rc; |
320 | 323 | } |
321 | 324 |
|
| 325 | +/* Helper to format an authorization error response for any group/action. |
| 326 | + * All response structures have int32_t rc as the first field. |
| 327 | + * Returns the response size to send. */ |
| 328 | +static uint16_t _wh_Server_FormatAuthErrorResponse(uint16_t magic, uint16_t group, |
| 329 | + uint16_t action, int32_t error_code, void* resp_packet) |
| 330 | +{ |
| 331 | + uint16_t resp_size = sizeof(int32_t); /* Minimum: just the rc field */ |
| 332 | + |
| 333 | + if (resp_packet == NULL) { |
| 334 | + return 0; |
| 335 | + } |
| 336 | + |
| 337 | + /* Write error code to first int32_t (rc field) - all responses start with this */ |
| 338 | + *(int32_t*)resp_packet = (int32_t)wh_Translate32(magic, (uint32_t)error_code); |
| 339 | + |
| 340 | + switch (group) { |
| 341 | + case WH_MESSAGE_GROUP_AUTH: |
| 342 | + /* Auth group has some responses larger than SimpleResponse */ |
| 343 | + switch (action) { |
| 344 | + case WH_MESSAGE_AUTH_ACTION_LOGIN: |
| 345 | + { |
| 346 | + whMessageAuth_LoginResponse resp = {0}; |
| 347 | + resp.rc = error_code; |
| 348 | + resp.user_id = WH_USER_ID_INVALID; |
| 349 | + resp.permissions = 0; |
| 350 | + wh_MessageAuth_TranslateLoginResponse(magic, &resp, |
| 351 | + (whMessageAuth_LoginResponse*)resp_packet); |
| 352 | + resp_size = sizeof(resp); |
| 353 | + break; |
| 354 | + } |
| 355 | + case WH_MESSAGE_AUTH_ACTION_USER_ADD: |
| 356 | + { |
| 357 | + whMessageAuth_UserAddResponse resp = {0}; |
| 358 | + resp.rc = error_code; |
| 359 | + resp.user_id = WH_USER_ID_INVALID; |
| 360 | + wh_MessageAuth_TranslateUserAddResponse(magic, &resp, |
| 361 | + (whMessageAuth_UserAddResponse*)resp_packet); |
| 362 | + resp_size = sizeof(resp); |
| 363 | + break; |
| 364 | + } |
| 365 | + case WH_MESSAGE_AUTH_ACTION_USER_GET: |
| 366 | + { |
| 367 | + whMessageAuth_UserGetResponse resp = {0}; |
| 368 | + resp.rc = error_code; |
| 369 | + resp.user_id = WH_USER_ID_INVALID; |
| 370 | + memset(resp.permissions, 0, sizeof(resp.permissions)); |
| 371 | + wh_MessageAuth_TranslateUserGetResponse(magic, &resp, |
| 372 | + (whMessageAuth_UserGetResponse*)resp_packet); |
| 373 | + resp_size = sizeof(resp); |
| 374 | + break; |
| 375 | + } |
| 376 | + default: |
| 377 | + { |
| 378 | + /* Use SimpleResponse for other auth actions */ |
| 379 | + whMessageAuth_SimpleResponse resp = {0}; |
| 380 | + resp.rc = error_code; |
| 381 | + wh_MessageAuth_TranslateSimpleResponse(magic, &resp, |
| 382 | + (whMessageAuth_SimpleResponse*)resp_packet); |
| 383 | + resp_size = sizeof(resp); |
| 384 | + break; |
| 385 | + } |
| 386 | + } |
| 387 | + break; |
| 388 | + |
| 389 | + case WH_MESSAGE_GROUP_NVM: |
| 390 | + /* NVM group - some actions have larger responses than SimpleResponse */ |
| 391 | + switch (action) { |
| 392 | + case WH_MESSAGE_NVM_ACTION_INIT: |
| 393 | + { |
| 394 | + whMessageNvm_InitResponse resp = {0}; |
| 395 | + resp.rc = error_code; |
| 396 | + resp.servernvm_id = 0; |
| 397 | + resp.clientnvm_id = 0; |
| 398 | + wh_MessageNvm_TranslateInitResponse(magic, &resp, |
| 399 | + (whMessageNvm_InitResponse*)resp_packet); |
| 400 | + resp_size = sizeof(resp); |
| 401 | + break; |
| 402 | + } |
| 403 | + case WH_MESSAGE_NVM_ACTION_GETAVAILABLE: |
| 404 | + { |
| 405 | + whMessageNvm_GetAvailableResponse resp = {0}; |
| 406 | + resp.rc = error_code; |
| 407 | + resp.avail_size = 0; |
| 408 | + resp.reclaim_size = 0; |
| 409 | + resp.avail_objects = 0; |
| 410 | + resp.reclaim_objects = 0; |
| 411 | + wh_MessageNvm_TranslateGetAvailableResponse(magic, &resp, |
| 412 | + (whMessageNvm_GetAvailableResponse*)resp_packet); |
| 413 | + resp_size = sizeof(resp); |
| 414 | + break; |
| 415 | + } |
| 416 | + case WH_MESSAGE_NVM_ACTION_LIST: |
| 417 | + { |
| 418 | + whMessageNvm_ListResponse resp = {0}; |
| 419 | + resp.rc = error_code; |
| 420 | + resp.count = 0; |
| 421 | + resp.id = 0; |
| 422 | + wh_MessageNvm_TranslateListResponse(magic, &resp, |
| 423 | + (whMessageNvm_ListResponse*)resp_packet); |
| 424 | + resp_size = sizeof(resp); |
| 425 | + break; |
| 426 | + } |
| 427 | + case WH_MESSAGE_NVM_ACTION_GETMETADATA: |
| 428 | + { |
| 429 | + whMessageNvm_GetMetadataResponse resp = {0}; |
| 430 | + resp.rc = error_code; |
| 431 | + resp.id = 0; |
| 432 | + resp.access = 0; |
| 433 | + resp.flags = 0; |
| 434 | + resp.len = 0; |
| 435 | + memset(resp.label, 0, sizeof(resp.label)); |
| 436 | + wh_MessageNvm_TranslateGetMetadataResponse(magic, &resp, |
| 437 | + (whMessageNvm_GetMetadataResponse*)resp_packet); |
| 438 | + resp_size = sizeof(resp); |
| 439 | + break; |
| 440 | + } |
| 441 | + case WH_MESSAGE_NVM_ACTION_READ: |
| 442 | + { |
| 443 | + whMessageNvm_ReadResponse resp = {0}; |
| 444 | + resp.rc = error_code; |
| 445 | + wh_MessageNvm_TranslateReadResponse(magic, &resp, |
| 446 | + (whMessageNvm_ReadResponse*)resp_packet); |
| 447 | + resp_size = sizeof(resp); |
| 448 | + break; |
| 449 | + } |
| 450 | + default: |
| 451 | + { |
| 452 | + /* Use SimpleResponse for other NVM actions */ |
| 453 | + whMessageNvm_SimpleResponse resp = {0}; |
| 454 | + resp.rc = error_code; |
| 455 | + wh_MessageNvm_TranslateSimpleResponse(magic, &resp, |
| 456 | + (whMessageNvm_SimpleResponse*)resp_packet); |
| 457 | + resp_size = sizeof(resp); |
| 458 | + break; |
| 459 | + } |
| 460 | + } |
| 461 | + break; |
| 462 | + |
| 463 | +#if defined(WOLFHSM_CFG_CERTIFICATE_MANAGER) && !defined(WOLFHSM_CFG_NO_CRYPTO) |
| 464 | + case WH_MESSAGE_GROUP_CERT: |
| 465 | + /* Cert group - use SimpleResponse for all actions */ |
| 466 | + { |
| 467 | + whMessageCert_SimpleResponse resp = {0}; |
| 468 | + resp.rc = error_code; |
| 469 | + wh_MessageCert_TranslateSimpleResponse(magic, &resp, |
| 470 | + (whMessageCert_SimpleResponse*)resp_packet); |
| 471 | + resp_size = sizeof(resp); |
| 472 | + } |
| 473 | + break; |
| 474 | +#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */ |
| 475 | + |
| 476 | + default: |
| 477 | + /* For other groups, use minimum size (just rc field). |
| 478 | + * Most response structures have int32_t rc as first field, so clients |
| 479 | + * should be able to read at least the error code. If a group needs |
| 480 | + * special handling, it can be added above. */ |
| 481 | + /* Error code already written above */ |
| 482 | + resp_size = sizeof(int32_t); |
| 483 | + break; |
| 484 | + } |
| 485 | + |
| 486 | + return resp_size; |
| 487 | +} |
| 488 | + |
| 489 | + |
322 | 490 | int wh_Server_HandleRequestMessage(whServerContext* server) |
323 | 491 | { |
324 | 492 | uint16_t magic = 0; |
@@ -359,23 +527,21 @@ int wh_Server_HandleRequestMessage(whServerContext* server) |
359 | 527 | if (server->auth != NULL) { |
360 | 528 | rc = wh_Auth_CheckRequestAuthorization(server->auth, group, action); |
361 | 529 | if (rc != WH_ERROR_OK) { |
362 | | - /* Authorization failed - send error response to client but keep server running */ |
363 | | - int32_t error_response = (int32_t)WH_AUTH_PERMISSION_ERROR; |
364 | | - uint16_t resp_size = sizeof(error_response); |
365 | | - |
366 | | - /* Translate the error response for endian conversion */ |
367 | | - error_response = (int32_t)wh_Translate32(magic, (uint32_t)error_response); |
368 | | - |
369 | | - /* Send error response to client */ |
370 | | - do { |
371 | | - rc = wh_CommServer_SendResponse(server->comm, magic, kind, seq, |
372 | | - resp_size, &error_response); |
373 | | - } while (rc == WH_ERROR_NOTREADY); |
374 | | - |
375 | | - /* Log the authorization failure */ |
376 | | - WH_LOG_ON_ERROR_F(&server->log, WH_LOG_LEVEL_ERROR, WH_AUTH_PERMISSION_ERROR, |
377 | | - "Authorization failed for (group=%d, action=%d, seq=%d)", |
378 | | - group, action, seq); |
| 530 | + /* Authorization failed - format and send error response to client */ |
| 531 | + int32_t error_code = (int32_t)WH_AUTH_PERMISSION_ERROR; |
| 532 | + uint16_t resp_size = _wh_Server_FormatAuthErrorResponse(magic, group, |
| 533 | + action, error_code, data); |
| 534 | + |
| 535 | + /* Send error response to client */ |
| 536 | + do { |
| 537 | + rc = wh_CommServer_SendResponse(server->comm, magic, kind, seq, |
| 538 | + resp_size, data); |
| 539 | + } while (rc == WH_ERROR_NOTREADY); |
| 540 | + |
| 541 | + /* Log the authorization failure */ |
| 542 | + WH_LOG_ON_ERROR_F(&server->log, WH_LOG_LEVEL_ERROR, WH_AUTH_PERMISSION_ERROR, |
| 543 | + "Authorization failed for (group=%d, action=%d, seq=%d)", |
| 544 | + group, action, seq); |
379 | 545 |
|
380 | 546 | return rc; |
381 | 547 | } |
|
0 commit comments