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