sys-create-loaded.int.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <inttypes.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "tss2_mu.h"
  14. #include "tss2_sys.h"
  15. #define LOGMODULE test
  16. #include "util/log.h"
  17. #include "sys-util.h"
  18. TSS2_RC
  19. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  20. {
  21. TSS2_RC rc = TSS2_RC_SUCCESS;
  22. TPM2B_SENSITIVE_CREATE in_sensitive = { 0 };
  23. TPMT_PUBLIC in_public = { 0 };
  24. TPM2B_TEMPLATE public_template = { 0 };
  25. TPM2B_PRIVATE out_private = { 0 };
  26. TPM2B_PUBLIC out_public = { 0 };
  27. TPM2B_NAME name = TPM2B_NAME_INIT;
  28. TPM2B_NAME qualified_name = TPM2B_NAME_INIT;
  29. TPM2_HANDLE object_handle = 0;
  30. TSS2L_SYS_AUTH_COMMAND auth_cmd = {
  31. .auths = {{ .sessionHandle = TPM2_RH_PW }},
  32. .count = 1
  33. };
  34. TSS2L_SYS_AUTH_RESPONSE auth_rsp = {
  35. .count = 0
  36. };
  37. if (sys_context == NULL)
  38. return TSS2_RC_LAYER_MASK | TSS2_BASE_RC_BAD_REFERENCE;
  39. in_public.type = TPM2_ALG_RSA;
  40. in_public.nameAlg = TPM2_ALG_SHA256;
  41. in_public.objectAttributes |= TPMA_OBJECT_RESTRICTED;
  42. in_public.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  43. in_public.objectAttributes |= TPMA_OBJECT_DECRYPT;
  44. in_public.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
  45. in_public.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
  46. in_public.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
  47. in_public.parameters.rsaDetail.symmetric.algorithm = TPM2_ALG_AES;
  48. in_public.parameters.rsaDetail.symmetric.keyBits.aes = 128;
  49. in_public.parameters.rsaDetail.symmetric.mode.aes = TPM2_ALG_CFB;
  50. in_public.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
  51. in_public.parameters.rsaDetail.keyBits = 2048;
  52. uint8_t public_buf[sizeof(in_public)] = {0};
  53. size_t offset = 0;
  54. rc = Tss2_MU_TPMT_PUBLIC_Marshal(&in_public, public_buf,
  55. sizeof(in_public), &offset);
  56. if (rc != TPM2_RC_SUCCESS) {
  57. LOG_ERROR("Tss2_MU_TPMT_PUBLIC_Marshal FAILED! Response Code: 0x%x", rc);
  58. exit(1);
  59. }
  60. public_template.size = offset;
  61. memcpy(public_template.buffer, public_buf, offset);
  62. /* Create an object using CreateLoaded.
  63. * The result should be that the created object
  64. * stays in the TPM
  65. */
  66. LOG_INFO("Calling CreateLoaded");
  67. rc = Tss2_Sys_CreateLoaded (sys_context,
  68. TPM2_RH_OWNER,
  69. &auth_cmd,
  70. &in_sensitive,
  71. &public_template,
  72. &object_handle,
  73. &out_private,
  74. &out_public,
  75. &name,
  76. &auth_rsp);
  77. if (rc == TPM2_RC_SUCCESS) {
  78. LOG_INFO("success object handle: 0x%x", object_handle);
  79. } else {
  80. LOG_ERROR("CreateLoaded FAILED! Response Code : 0x%x", rc);
  81. exit(1);
  82. }
  83. memset(&out_public, '\0', sizeof(out_public));
  84. memset(&name, '\0', sizeof(name));
  85. /* Check if the object is really loaded by accessing its
  86. * public area */
  87. LOG_INFO("Calling ReadPublic");
  88. rc = Tss2_Sys_ReadPublic (sys_context,
  89. object_handle,
  90. NULL,
  91. &out_public,
  92. &name,
  93. &qualified_name,
  94. NULL);
  95. if (rc == TPM2_RC_SUCCESS) {
  96. LOG_INFO("success! Object's qualified name is:");
  97. LOGBLOB_INFO(qualified_name.name, qualified_name.size, "%s", "name:");
  98. } else {
  99. LOG_ERROR("Tss2_Sys_ReadPublic FAILED! Response Code : 0x%x", rc);
  100. exit(1);
  101. }
  102. rc = Tss2_Sys_FlushContext (sys_context, object_handle);
  103. if (rc != TSS2_RC_SUCCESS) {
  104. LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
  105. exit(1);
  106. }
  107. return rc;
  108. }