provision_bulk.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /// EpidProvisionKey implementation.
  17. /*!
  18. * \file
  19. */
  20. #include <epid/member/api.h>
  21. #include <string.h>
  22. #include "epid/common/src/memory.h"
  23. #include "epid/common/stdtypes.h"
  24. #include "epid/common/types.h"
  25. #include "epid/member/src/context.h"
  26. #include "epid/member/src/storage.h"
  27. #include "epid/member/tpm2/context.h"
  28. #include "epid/member/tpm2/load_external.h"
  29. EpidStatus EpidProvisionKey(MemberCtx* ctx, GroupPubKey const* pub_key,
  30. PrivKey const* priv_key,
  31. MemberPrecomp const* precomp_str) {
  32. EpidStatus sts = kEpidErr;
  33. uint32_t const nv_index = 0x01c10100;
  34. MembershipCredential credential = {0};
  35. if (!pub_key || !priv_key || !ctx) {
  36. return kEpidBadArgErr;
  37. }
  38. // The member verifies that gid in public key and in private key
  39. // match. If mismatch, abort and return operation failed.
  40. if (memcmp(&pub_key->gid, &priv_key->gid, sizeof(GroupId))) {
  41. return kEpidBadArgErr;
  42. }
  43. sts = Tpm2LoadExternal(ctx->tpm2_ctx, &priv_key->f);
  44. if (kEpidNoErr != sts) {
  45. return sts;
  46. }
  47. credential.A = priv_key->A;
  48. credential.x = priv_key->x;
  49. credential.gid = priv_key->gid;
  50. if (ctx->primary_key_set) {
  51. Tpm2ResetContext(&ctx->tpm2_ctx);
  52. ctx->primary_key_set = false;
  53. }
  54. sts = Tpm2LoadExternal(ctx->tpm2_ctx, &priv_key->f);
  55. if (kEpidNoErr != sts) {
  56. return sts;
  57. }
  58. ctx->primary_key_set = true;
  59. sts = EpidNvWriteMembershipCredential(ctx->tpm2_ctx, pub_key, &credential,
  60. nv_index);
  61. if (kEpidNoErr == sts) {
  62. if (precomp_str) {
  63. ctx->precomp = *precomp_str;
  64. ctx->precomp_ready = true;
  65. } else {
  66. EpidZeroMemory(&ctx->precomp, sizeof(ctx->precomp));
  67. ctx->precomp_ready = false;
  68. }
  69. ctx->pub_key = *pub_key;
  70. ctx->is_provisioned = true;
  71. ctx->credential.A = credential.A;
  72. ctx->credential.x = credential.x;
  73. ctx->credential.gid = credential.gid;
  74. }
  75. return sts;
  76. }