123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /*############################################################################
- # Copyright 2017 Intel Corporation
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- ############################################################################*/
- /*!
- * \file
- * \brief Member C++ wrapper implementation.
- */
- #include "epid/common-testhelper/member_wrapper-testhelper.h"
- #include <stdint.h>
- #include <cstdio>
- #include <cstring>
- #include <new>
- #include <sstream>
- #include <stdexcept>
- #include <string>
- extern "C" {
- #include "epid/common/types.h"
- }
- #include "epid/common-testhelper/mem_params-testhelper.h"
- /// Handle SDK Error with Break
- #define BREAK_ON_EPID_ERROR(ret) \
- if (kEpidNoErr != (ret)) { \
- break; \
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
- BitSupplier rnd_func, void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, &priv_key.f, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, nullptr);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
- MembershipCredential const& cred,
- BitSupplier rnd_func, void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, nullptr, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidProvisionCredential(ctx_, &pub_key, &cred, nullptr);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
- HashAlg hash_alg, BitSupplier rnd_func,
- void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, &priv_key.f, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidMemberSetHashAlg(ctx_, hash_alg);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberSetHashAlg()");
- }
- sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, nullptr);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(BitSupplier rnd_func, void* rnd_param)
- : ctx_(nullptr) {
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, nullptr, ¶ms);
- ctx_ = CreateMember(¶ms);
- }
- MemberCtxObj::MemberCtxObj(MemberParams const* params) : ctx_(nullptr) {
- ctx_ = CreateMember(params);
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
- MemberPrecomp const& precomp, BitSupplier rnd_func,
- void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, &priv_key.f, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, &precomp);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key, PrivKey const& priv_key,
- HashAlg hash_alg, MemberPrecomp const& precomp,
- BitSupplier rnd_func, void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, &priv_key.f, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidMemberSetHashAlg(ctx_, hash_alg);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidProvisionKey(ctx_, &pub_key, &priv_key, &precomp);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
- MembershipCredential const& cred,
- MemberPrecomp const& precomp, BitSupplier rnd_func,
- void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, nullptr, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidProvisionCredential(ctx_, &pub_key, &cred, &precomp);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionCredential()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::MemberCtxObj(GroupPubKey const& pub_key,
- MembershipCredential const& cred, HashAlg hash_alg,
- BitSupplier rnd_func, void* rnd_param)
- : ctx_(nullptr) {
- EpidStatus sts = kEpidErr;
- MemberParams params = {0};
- SetMemberParams(rnd_func, rnd_param, nullptr, ¶ms);
- ctx_ = CreateMember(¶ms);
- sts = EpidMemberSetHashAlg(ctx_, hash_alg);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionKey()");
- }
- sts = EpidProvisionCredential(ctx_, &pub_key, &cred, nullptr);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidProvisionCredential()");
- }
- sts = EpidMemberStartup(ctx_);
- if (kEpidNoErr != sts) {
- DeleteMember(&ctx_);
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberStartup()");
- }
- }
- MemberCtxObj::~MemberCtxObj() { DeleteMember(&ctx_); }
- MemberCtx* MemberCtxObj::ctx() const { return ctx_; }
- MemberCtxObj::operator MemberCtx*() const { return ctx_; }
- MemberCtxObj::operator const MemberCtx*() const { return ctx_; }
- MemberCtx* MemberCtxObj::CreateMember(MemberParams const* params) const {
- size_t context_size = 0;
- EpidStatus sts = kEpidErr;
- MemberCtx* member_ctx = NULL;
- sts = EpidMemberGetSize(params, &context_size);
- if (kEpidNoErr != sts) {
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberGetSize()");
- }
- // allocate and zero initialize, no throw on failure
- member_ctx = (MemberCtx*)new (std::nothrow) uint8_t[context_size]();
- if (!member_ctx) {
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- std::ostringstream err;
- err << "Failed to allocate " << context_size
- << "bytes after EpidMemberGetSize()";
- throw std::logic_error(err.str());
- }
- sts = EpidMemberInit(params, member_ctx);
- if (kEpidNoErr != sts) {
- delete[](uint8_t*) member_ctx;
- printf("%s(%d): %s\n", __FILE__, __LINE__, "test defect:");
- throw std::logic_error(std::string("Failed to call: ") +
- "EpidMemberInit()");
- }
- return member_ctx;
- }
- void MemberCtxObj::DeleteMember(MemberCtx** ctx) const {
- if (ctx) {
- EpidMemberDeinit(*ctx);
- delete[](uint8_t*)(*ctx);
- *ctx = NULL;
- }
- }
|