createprimary.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \brief TPM2_CreatePrimary command implementation.
  18. * \file
  19. */
  20. #include "epid/member/tpm2/createprimary.h"
  21. #include "epid/common/src/memory.h"
  22. #include "epid/member/tpm2/ibm_tss/conversion.h"
  23. #include "epid/member/tpm2/ibm_tss/printtss.h"
  24. #include "epid/member/tpm2/ibm_tss/state.h"
  25. #include "tss2/TPM_Types.h"
  26. #include "tss2/tss.h"
  27. EpidStatus Tpm2CreatePrimary(Tpm2Ctx* ctx, G1ElemStr* p_str) {
  28. if (!ctx || !ctx->epid2_params || !p_str) {
  29. return kEpidBadArgErr;
  30. }
  31. CreatePrimary_In in = {0};
  32. CreatePrimary_Out out;
  33. TPM_RC rc = TPM_RC_SUCCESS;
  34. TPMI_ALG_PUBLIC algPublic = TPM_ALG_ECC;
  35. TPMI_ECC_CURVE curveID = TPM_ECC_BN_P256;
  36. TPMI_ALG_HASH halg = TPM_ALG_NULL;
  37. TPMI_ALG_HASH nalg = TPM_ALG_NULL;
  38. TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RS_PW;
  39. TPM2B_ECC_POINT public_area;
  40. unsigned int sessionAttributes0 = 0;
  41. const char* parentPasswordPtr = NULL;
  42. in.primaryHandle = TPM_RH_ENDORSEMENT;
  43. halg = EpidtoTpm2HashAlg(ctx->hash_alg);
  44. if (halg == TPM_ALG_NULL) {
  45. return kEpidHashAlgorithmNotSupported;
  46. }
  47. nalg = halg;
  48. /* Table 185 - TPM2B_PUBLIC inPublic */
  49. /* Table 184 - TPMT_PUBLIC in.inPublic.publicArea */
  50. in.inPublic.publicArea.type = algPublic;
  51. in.inPublic.publicArea.nameAlg = nalg;
  52. /* Table 32 - TPMA_OBJECT objectAttributes */
  53. in.inPublic.publicArea.objectAttributes.val |= TPMA_OBJECT_NODA;
  54. in.inPublic.publicArea.objectAttributes.val |= TPMA_OBJECT_FIXEDTPM;
  55. in.inPublic.publicArea.objectAttributes.val |= TPMA_OBJECT_FIXEDPARENT;
  56. in.inPublic.publicArea.objectAttributes.val |=
  57. TPMA_OBJECT_SENSITIVEDATAORIGIN;
  58. in.inPublic.publicArea.parameters.eccDetail.symmetric.algorithm =
  59. TPM_ALG_NULL;
  60. in.inPublic.publicArea.parameters.eccDetail.scheme.scheme = TPM_ALG_ECDAA;
  61. in.inPublic.publicArea.parameters.eccDetail.scheme.details.ecdaa.hashAlg =
  62. halg;
  63. in.inPublic.publicArea.parameters.eccDetail.scheme.details.ecdaa.count = 1;
  64. in.inPublic.publicArea.parameters.eccDetail.curveID = curveID;
  65. in.inPublic.publicArea.parameters.eccDetail.kdf.scheme = TPM_ALG_NULL;
  66. in.inSensitive.sensitive.userAuth.t.size = 0;
  67. in.inSensitive.sensitive.data.t.size = 0;
  68. in.inPublic.publicArea.objectAttributes.val |=
  69. TPMA_OBJECT_SENSITIVEDATAORIGIN;
  70. in.inPublic.publicArea.objectAttributes.val |= TPMA_OBJECT_USERWITHAUTH;
  71. in.inPublic.publicArea.objectAttributes.val &= ~TPMA_OBJECT_ADMINWITHPOLICY;
  72. in.inPublic.publicArea.objectAttributes.val |= TPMA_OBJECT_SIGN;
  73. in.inPublic.publicArea.objectAttributes.val &= ~TPMA_OBJECT_DECRYPT;
  74. in.inPublic.publicArea.objectAttributes.val &= ~TPMA_OBJECT_RESTRICTED;
  75. in.inPublic.publicArea.unique.ecc.y.t.size = 0;
  76. in.inPublic.publicArea.unique.ecc.x.t.size = 0;
  77. in.inPublic.publicArea.authPolicy.t.size = 0;
  78. in.inPublic.publicArea.unique.rsa.t.size = 0;
  79. in.outsideInfo.t.size = 0;
  80. in.creationPCR.count = 0;
  81. rc = TSS_Execute(ctx->tss, (RESPONSE_PARAMETERS*)&out,
  82. (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_CreatePrimary,
  83. sessionHandle0, parentPasswordPtr, sessionAttributes0,
  84. TPM_RH_NULL, NULL, 0);
  85. if (rc != TPM_RC_SUCCESS) {
  86. print_tpm2_response_code("TPM2_CreatePrimary", rc);
  87. if (TPM_RC_ATTRIBUTES == rc || TPM_RC_KDF == rc || TPM_RC_SYMMETRIC == rc ||
  88. TPM_RC_TYPE == rc || TPM_RC_SCHEME == rc || TPM_RC_SIZE == rc ||
  89. TPM_RC_KEY == rc)
  90. return kEpidBadArgErr;
  91. return kEpidErr;
  92. }
  93. ctx->key_handle = out.objectHandle;
  94. public_area.point = out.outPublic.publicArea.unique.ecc;
  95. return WriteTpm2EcPoint(&public_area, p_str);
  96. }