member_wrapper-testhelper.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * \file
  18. * \brief Member C++ wrapper implementation.
  19. */
  20. #include "epid/common-testhelper/member_wrapper-testhelper.h"
  21. #include <stdint.h>
  22. #include <cstdio>
  23. #include <cstring>
  24. #include <new>
  25. #include <sstream>
  26. #include <stdexcept>
  27. #include <string>
  28. extern "C" {
  29. #include "epid/common/types.h"
  30. }
  31. #include "epid/common-testhelper/mem_params-testhelper.h"
  32. /// Handle SDK Error with Break
  33. #define BREAK_ON_EPID_ERROR(ret) \
  34. if (kEpidNoErr != (ret)) { \
  35. break; \
  36. }
  37. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
  38. BitSupplier rnd_func, void* rnd_param)
  39. : ctx_(nullptr) {
  40. EpidStatus sts = kEpidErr;
  41. MemberParams params = {0};
  42. SetMemberParams(rnd_func, rnd_param, &priv_key.f, &params);
  43. ctx_ = CreateMember(&params);
  44. sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, nullptr);
  45. if (kEpidNoErr != sts) {
  46. DeleteMember(&ctx_);
  47. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  48. throw std::logic_error(std::string("Failed to call: ") +
  49. "EpidProvisionKey()");
  50. }
  51. sts = EpidMemberStartup(ctx_);
  52. if (kEpidNoErr != sts) {
  53. DeleteMember(&ctx_);
  54. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  55. throw std::logic_error(std::string("Failed to call: ") +
  56. "EpidMemberStartup()");
  57. }
  58. }
  59. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
  60. MembershipCredential const& cred,
  61. BitSupplier rnd_func, void* rnd_param)
  62. : ctx_(nullptr) {
  63. EpidStatus sts = kEpidErr;
  64. MemberParams params = {0};
  65. SetMemberParams(rnd_func, rnd_param, nullptr, &params);
  66. ctx_ = CreateMember(&params);
  67. sts = EpidProvisionCredential(ctx_, &pub_key, &cred, nullptr);
  68. if (kEpidNoErr != sts) {
  69. DeleteMember(&ctx_);
  70. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  71. throw std::logic_error(std::string("Failed to call: ") +
  72. "EpidProvisionKey()");
  73. }
  74. sts = EpidMemberStartup(ctx_);
  75. if (kEpidNoErr != sts) {
  76. DeleteMember(&ctx_);
  77. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  78. throw std::logic_error(std::string("Failed to call: ") +
  79. "EpidMemberStartup()");
  80. }
  81. }
  82. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
  83. HashAlg hash_alg, BitSupplier rnd_func,
  84. void* rnd_param)
  85. : ctx_(nullptr) {
  86. EpidStatus sts = kEpidErr;
  87. MemberParams params = {0};
  88. SetMemberParams(rnd_func, rnd_param, &priv_key.f, &params);
  89. ctx_ = CreateMember(&params);
  90. sts = EpidMemberSetHashAlg(ctx_, hash_alg);
  91. if (kEpidNoErr != sts) {
  92. DeleteMember(&ctx_);
  93. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  94. throw std::logic_error(std::string("Failed to call: ") +
  95. "EpidMemberSetHashAlg()");
  96. }
  97. sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, nullptr);
  98. if (kEpidNoErr != sts) {
  99. DeleteMember(&ctx_);
  100. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  101. throw std::logic_error(std::string("Failed to call: ") +
  102. "EpidProvisionKey()");
  103. }
  104. sts = EpidMemberStartup(ctx_);
  105. if (kEpidNoErr != sts) {
  106. DeleteMember(&ctx_);
  107. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  108. throw std::logic_error(std::string("Failed to call: ") +
  109. "EpidMemberStartup()");
  110. }
  111. }
  112. MemberCtxObj::MemberCtxObj(BitSupplier rnd_func, void* rnd_param)
  113. : ctx_(nullptr) {
  114. MemberParams params = {0};
  115. SetMemberParams(rnd_func, rnd_param, nullptr, &params);
  116. ctx_ = CreateMember(&params);
  117. }
  118. MemberCtxObj::MemberCtxObj(MemberParams const* params) : ctx_(nullptr) {
  119. ctx_ = CreateMember(params);
  120. }
  121. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
  122. MemberPrecomp const& precomp, BitSupplier rnd_func,
  123. void* rnd_param)
  124. : ctx_(nullptr) {
  125. EpidStatus sts = kEpidErr;
  126. MemberParams params = {0};
  127. SetMemberParams(rnd_func, rnd_param, &priv_key.f, &params);
  128. ctx_ = CreateMember(&params);
  129. sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, &precomp);
  130. if (kEpidNoErr != sts) {
  131. DeleteMember(&ctx_);
  132. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  133. throw std::logic_error(std::string("Failed to call: ") +
  134. "EpidProvisionKey()");
  135. }
  136. sts = EpidMemberStartup(ctx_);
  137. if (kEpidNoErr != sts) {
  138. DeleteMember(&ctx_);
  139. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  140. throw std::logic_error(std::string("Failed to call: ") +
  141. "EpidMemberStartup()");
  142. }
  143. }
  144. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
  145. HashAlg hash_alg, MemberPrecomp const& precomp,
  146. BitSupplier rnd_func, void* rnd_param)
  147. : ctx_(nullptr) {
  148. EpidStatus sts = kEpidErr;
  149. MemberParams params = {0};
  150. SetMemberParams(rnd_func, rnd_param, &priv_key.f, &params);
  151. ctx_ = CreateMember(&params);
  152. sts = EpidMemberSetHashAlg(ctx_, hash_alg);
  153. if (kEpidNoErr != sts) {
  154. DeleteMember(&ctx_);
  155. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  156. throw std::logic_error(std::string("Failed to call: ") +
  157. "EpidProvisionKey()");
  158. }
  159. sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, &precomp);
  160. if (kEpidNoErr != sts) {
  161. DeleteMember(&ctx_);
  162. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  163. throw std::logic_error(std::string("Failed to call: ") +
  164. "EpidProvisionKey()");
  165. }
  166. sts = EpidMemberStartup(ctx_);
  167. if (kEpidNoErr != sts) {
  168. DeleteMember(&ctx_);
  169. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  170. throw std::logic_error(std::string("Failed to call: ") +
  171. "EpidMemberStartup()");
  172. }
  173. }
  174. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
  175. MembershipCredential const& cred,
  176. MemberPrecomp const& precomp, BitSupplier rnd_func,
  177. void* rnd_param)
  178. : ctx_(nullptr) {
  179. EpidStatus sts = kEpidErr;
  180. MemberParams params = {0};
  181. SetMemberParams(rnd_func, rnd_param, nullptr, &params);
  182. ctx_ = CreateMember(&params);
  183. sts = EpidProvisionCredential(ctx_, &pub_key, &cred, &precomp);
  184. if (kEpidNoErr != sts) {
  185. DeleteMember(&ctx_);
  186. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  187. throw std::logic_error(std::string("Failed to call: ") +
  188. "EpidProvisionCredential()");
  189. }
  190. sts = EpidMemberStartup(ctx_);
  191. if (kEpidNoErr != sts) {
  192. DeleteMember(&ctx_);
  193. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  194. throw std::logic_error(std::string("Failed to call: ") +
  195. "EpidMemberStartup()");
  196. }
  197. }
  198. MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
  199. MembershipCredential const& cred, HashAlg hash_alg,
  200. BitSupplier rnd_func, void* rnd_param)
  201. : ctx_(nullptr) {
  202. EpidStatus sts = kEpidErr;
  203. MemberParams params = {0};
  204. SetMemberParams(rnd_func, rnd_param, nullptr, &params);
  205. ctx_ = CreateMember(&params);
  206. sts = EpidMemberSetHashAlg(ctx_, hash_alg);
  207. if (kEpidNoErr != sts) {
  208. DeleteMember(&ctx_);
  209. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  210. throw std::logic_error(std::string("Failed to call: ") +
  211. "EpidProvisionKey()");
  212. }
  213. sts = EpidProvisionCredential(ctx_, &pub_key, &cred, nullptr);
  214. if (kEpidNoErr != sts) {
  215. DeleteMember(&ctx_);
  216. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  217. throw std::logic_error(std::string("Failed to call: ") +
  218. "EpidProvisionCredential()");
  219. }
  220. sts = EpidMemberStartup(ctx_);
  221. if (kEpidNoErr != sts) {
  222. DeleteMember(&ctx_);
  223. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  224. throw std::logic_error(std::string("Failed to call: ") +
  225. "EpidMemberStartup()");
  226. }
  227. }
  228. MemberCtxObj::~MemberCtxObj() { DeleteMember(&ctx_); }
  229. MemberCtx* MemberCtxObj::ctx() const { return ctx_; }
  230. MemberCtxObj::operator MemberCtx*() const { return ctx_; }
  231. MemberCtxObj::operator const MemberCtx*() const { return ctx_; }
  232. MemberCtx* MemberCtxObj::CreateMember(MemberParams const* params) const {
  233. size_t context_size = 0;
  234. EpidStatus sts = kEpidErr;
  235. MemberCtx* member_ctx = NULL;
  236. sts = EpidMemberGetSize(params, &context_size);
  237. if (kEpidNoErr != sts) {
  238. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  239. throw std::logic_error(std::string("Failed to call: ") +
  240. "EpidMemberGetSize()");
  241. }
  242. // allocate and zero initialize, no throw on failure
  243. member_ctx = (MemberCtx*)new (std::nothrow) uint8_t[context_size]();
  244. if (!member_ctx) {
  245. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  246. std::ostringstream err;
  247. err << "Failed to allocate " << context_size
  248. << "bytes after EpidMemberGetSize()";
  249. throw std::logic_error(err.str());
  250. }
  251. sts = EpidMemberInit(params, member_ctx);
  252. if (kEpidNoErr != sts) {
  253. delete[](uint8_t*) member_ctx;
  254. printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
  255. throw std::logic_error(std::string("Failed to call: ") +
  256. "EpidMemberInit()");
  257. }
  258. return member_ctx;
  259. }
  260. void MemberCtxObj::DeleteMember(MemberCtx** ctx) const {
  261. if (ctx) {
  262. EpidMemberDeinit(*ctx);
  263. delete[](uint8_t*)(*ctx);
  264. *ctx = NULL;
  265. }
  266. }