fapi-nv-set-bits.int.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. *******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <inttypes.h>
  12. #include <string.h>
  13. #include "tss2_fapi.h"
  14. #include "test-fapi.h"
  15. #define LOGMODULE test
  16. #include "util/log.h"
  17. #include "util/aux_util.h"
  18. #define PASSWORD "abc"
  19. static TSS2_RC
  20. auth_callback(
  21. char const *objectPath,
  22. char const *description,
  23. const char **auth,
  24. void *userData)
  25. {
  26. UNUSED(description);
  27. UNUSED(userData);
  28. if (!objectPath) {
  29. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  30. }
  31. *auth = PASSWORD;
  32. return TSS2_RC_SUCCESS;
  33. }
  34. /** Test the FAPI function Fapi_NvSetBits.
  35. *
  36. * Tested FAPI commands:
  37. * - Fapi_Provision()
  38. * - Fapi_CreateNv()
  39. * - Fapi_NvSetBits()
  40. * - Fapi_Delete()
  41. * - Fapi_SetAuthCB()
  42. *
  43. * @param[in,out] context The FAPI_CONTEXT.
  44. * @retval EXIT_FAILURE
  45. * @retval EXIT_SUCCESS
  46. */
  47. int
  48. test_fapi_nv_set_bits(FAPI_CONTEXT *context)
  49. {
  50. TSS2_RC r;
  51. char *nvPathBitMap = "/nv/Owner/myNV_BitMap";
  52. r = Fapi_Provision(context, NULL, NULL, NULL);
  53. goto_if_error(r, "Error Fapi_Provision", error);
  54. r = Fapi_Provision(context, NULL, NULL, NULL);
  55. if (r != TSS2_FAPI_RC_ALREADY_PROVISIONED) {
  56. /* File exists or persistent key exists. */
  57. LOG_ERROR("Check whether provisioning directory exists failed.");
  58. goto error;
  59. }
  60. /* Test no password, noda set */
  61. r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", "");
  62. goto_if_error(r, "Error Fapi_CreateNv", error);
  63. uint64_t bitmap = 0x0102030405060608;
  64. r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
  65. goto_if_error(r, "Error Fapi_SetBits", error);
  66. r = Fapi_Delete(context, nvPathBitMap);
  67. goto_if_error(r, "Error Fapi_Delete", error);
  68. /* Test with password noda set */
  69. r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", PASSWORD);
  70. goto_if_error(r, "Error Fapi_CreateNv", error);
  71. r = Fapi_SetAuthCB(context, auth_callback, "");
  72. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  73. r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
  74. goto_if_error(r, "Error Fapi_CreateNv", error);
  75. r = Fapi_Delete(context, nvPathBitMap);
  76. goto_if_error(r, "Error Fapi_Delete", error);
  77. /* Test no password, noda set */
  78. r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", "");
  79. goto_if_error(r, "Error Fapi_CreateNv", error);
  80. r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
  81. goto_if_error(r, "Error Fapi_NvSetbits", error);
  82. r = Fapi_Delete(context, nvPathBitMap);
  83. goto_if_error(r, "Error Fapi_Delete", error);
  84. /* Test with password noda set */
  85. r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", PASSWORD);
  86. goto_if_error(r, "Error Fapi_CreateNv", error);
  87. r = Fapi_SetAuthCB(context, auth_callback, "");
  88. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  89. r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
  90. goto_if_error(r, "Error Fapi_SetBits", error);
  91. r = Fapi_Delete(context, nvPathBitMap);
  92. goto_if_error(r, "Error Fapi_Delete", error);
  93. r = Fapi_Delete(context, "/");
  94. goto_if_error(r, "Error Fapi_Delete", error);
  95. return EXIT_SUCCESS;
  96. error:
  97. Fapi_Delete(context, "/");
  98. return EXIT_FAILURE;
  99. }
  100. int
  101. test_invoke_fapi(FAPI_CONTEXT *context)
  102. {
  103. return test_fapi_nv_set_bits(context);
  104. }