-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlib.rs
325 lines (284 loc) · 11.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use anyhow::Context;
use concordium_base::{
common::{base16_decode_string, types::TransactionTime, Versioned, VERSION_0},
curve_arithmetic::*,
id,
id::{
constants::{ArCurve, AttributeKind, IpPairing},
identity_provider::{
create_initial_cdi, sign_identity_object, sign_identity_object_v1,
validate_id_recovery_request, validate_request as ip_validate_request,
validate_request_v1 as ip_validate_request_v1,
},
types::*,
},
ps_sig,
};
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
#[cfg(feature = "nodejs")]
use serde_json::ser::to_string;
type Bls12 = IpPairing;
type G1 = ArCurve;
type ExampleCurve = G1;
type ExampleAttributeList = AttributeList<<Bls12 as Pairing>::ScalarField, AttributeKind>;
// Parse an IpInfo taking into account the version.
// For now only version 0 is supported.
fn parse_exact_versioned_ip_info(bytes: &[u8]) -> anyhow::Result<IpInfo<Bls12>> {
let v: Versioned<IpInfo<Bls12>> =
serde_json::from_slice(bytes).context("Could not parse versioned ip info.")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect IpInfo version.");
}
}
// Parse anonymity revokers taking into account the version.
// For now only version 0 is supported.
fn parse_exact_versioned_ars_infos(bytes: &[u8]) -> anyhow::Result<ArInfos<ArCurve>> {
let v: Versioned<ArInfos<ArCurve>> =
serde_json::from_slice(bytes).context("Could not parse versioned ar infos.")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect Ars version.");
}
}
// Parse an GlobalContext taking into account the version.
// For now only version 0 is supported.
fn parse_exact_versioned_global_context(
bytes: &[u8],
) -> anyhow::Result<GlobalContext<ExampleCurve>> {
let v: Versioned<GlobalContext<ExampleCurve>> =
serde_json::from_slice(bytes).context("Could not parse versioned global context.")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect Global context version.");
}
}
fn parse_exact_versioned_pio_from_request(
bytes: &[u8],
) -> anyhow::Result<PreIdentityObject<Bls12, ExampleCurve>> {
let v: serde_json::Value = serde_json::from_slice(bytes)
.context("Could not parse JSON containing idObjectRequest.")?;
let pre_id_obj_value = v
.get("idObjectRequest")
.context("Field 'idObjectRequest' not found")?;
let v = serde_json::from_value::<Versioned<PreIdentityObject<_, _>>>(pre_id_obj_value.clone())
.context("Could not parse preIdentityObject")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect version of pre identity object.");
}
}
fn parse_exact_versioned_pio_from_request_v1(
bytes: &[u8],
) -> anyhow::Result<PreIdentityObjectV1<Bls12, ExampleCurve>> {
let v: serde_json::Value = serde_json::from_slice(bytes)
.context("Could not parse JSON containing idObjectRequest.")?;
let pre_id_obj_value = v
.get("idObjectRequest")
.context("Field 'idObjectRequest' not found")?;
let v =
serde_json::from_value::<Versioned<PreIdentityObjectV1<_, _>>>(pre_id_obj_value.clone())
.context("Could not parse preIdentityObject")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect version of pre identity object.");
}
}
fn parse_exact_versioned_recovery_request(
bytes: &[u8],
) -> anyhow::Result<IdRecoveryRequest<ExampleCurve>> {
let v: serde_json::Value = serde_json::from_slice(bytes)
.context("Could not parse JSON containing idObjectRequest.")?;
let pre_id_obj_value = v
.get("idRecoveryRequest")
.context("Field 'idObjectRequest' not found")?;
let v = serde_json::from_value::<Versioned<IdRecoveryRequest<_>>>(pre_id_obj_value.clone())
.context("Could not parse preIdentityObject")?;
if v.version == VERSION_0 {
Ok(v.value)
} else {
anyhow::bail!("Incorrect version of pre identity object.");
}
}
/// Validate a request
fn validate_request(
global_context_bytes: &[u8],
ip_info_bytes: &[u8],
ars_infos_bytes: &[u8],
request_bytes: &[u8],
) -> anyhow::Result<AccountAddress> {
let global_context: GlobalContext<ExampleCurve> =
parse_exact_versioned_global_context(global_context_bytes)?;
let ip_info: IpInfo<Bls12> = parse_exact_versioned_ip_info(ip_info_bytes)?;
let ars_infos: ArInfos<ArCurve> = parse_exact_versioned_ars_infos(ars_infos_bytes)?;
let request: PreIdentityObject<Bls12, ExampleCurve> =
parse_exact_versioned_pio_from_request(request_bytes)?;
let context = IpContext {
ip_info: &ip_info,
ars_infos: &ars_infos.anonymity_revokers,
global_context: &global_context,
};
let addr = account_address_from_registration_id(&request.pub_info_for_ip.reg_id);
if let Err(e) = ip_validate_request(&request, context) {
anyhow::bail!("Ip validation failed: {:?}", e);
}
Ok(addr)
}
/// Validate a version 1 request
fn validate_request_v1(
global_context_bytes: &[u8],
ip_info_bytes: &[u8],
ars_infos_bytes: &[u8],
request_bytes: &[u8],
) -> anyhow::Result<()> {
let global_context: GlobalContext<ExampleCurve> =
parse_exact_versioned_global_context(global_context_bytes)?;
let ip_info: IpInfo<Bls12> = parse_exact_versioned_ip_info(ip_info_bytes)?;
let ars_infos: ArInfos<ArCurve> = parse_exact_versioned_ars_infos(ars_infos_bytes)?;
let request: PreIdentityObjectV1<Bls12, ExampleCurve> =
parse_exact_versioned_pio_from_request_v1(request_bytes)?;
let context = IpContext {
ip_info: &ip_info,
ars_infos: &ars_infos.anonymity_revokers,
global_context: &global_context,
};
if let Err(e) = ip_validate_request_v1(&request, context) {
anyhow::bail!("Ip validation failed: {:?}", e);
}
Ok(())
}
#[derive(SerdeSerialize, SerdeDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct IdentityCreation {
id_obj: Versioned<IdentityObject<Bls12, ExampleCurve, AttributeKind>>,
ar_record: Versioned<AnonymityRevocationRecord<ExampleCurve>>,
request: Versioned<AccountCredentialMessage<Bls12, ExampleCurve, AttributeKind>>,
account_address: AccountAddress,
}
#[derive(SerdeSerialize, SerdeDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct IdentityCreationV1 {
id_obj: Versioned<IdentityObjectV1<Bls12, ExampleCurve, AttributeKind>>,
ar_record: Versioned<AnonymityRevocationRecord<ExampleCurve>>,
}
/// Create an identity object, the anonymity revocation record, and the initial
/// account object.
fn create_identity_object(
ip_info_bytes: &[u8],
request_bytes: &[u8],
alist_bytes: &[u8],
expiry: u64,
ip_private_key_bytes: &[u8],
ip_cdi_private_key_bytes: &[u8],
) -> anyhow::Result<IdentityCreation> {
let ip_info: IpInfo<Bls12> = parse_exact_versioned_ip_info(ip_info_bytes)?;
let alist: ExampleAttributeList =
serde_json::from_slice(alist_bytes).context("Could not parse attribute list")?;
let ip_private_key_str = std::str::from_utf8(ip_private_key_bytes)?;
let ip_cdi_private_key_str = std::str::from_utf8(ip_cdi_private_key_bytes)?;
let ip_private_key: ps_sig::SecretKey<Bls12> =
base16_decode_string(ip_private_key_str).context("Could not parse ip_private_key")?;
let ip_cdi_private_key: ed25519_dalek::SecretKey = base16_decode_string(ip_cdi_private_key_str)
.context("Could not parse ip_cdi_private_key")?;
let request: PreIdentityObject<Bls12, ExampleCurve> =
parse_exact_versioned_pio_from_request(request_bytes)?;
let signature = match sign_identity_object(&request, &ip_info, &alist, &ip_private_key) {
Ok(sig) => sig,
Err(e) => anyhow::bail!("Signing failed, {}", e),
};
let ar_record = Versioned::new(VERSION_0, AnonymityRevocationRecord {
id_cred_pub: request.pub_info_for_ip.id_cred_pub,
ar_data: request.ip_ar_data.clone(),
max_accounts: alist.max_accounts,
threshold: request.choice_ar_parameters.threshold,
});
let icdi = create_initial_cdi(
&ip_info,
request.pub_info_for_ip.clone(),
&alist,
TransactionTime::from(expiry),
&ip_cdi_private_key,
);
let id = IdentityObject {
pre_identity_object: request,
alist,
signature,
};
let vid = Versioned::new(VERSION_0, id);
let account_address =
account_address_from_registration_id(&vid.value.pre_identity_object.pub_info_for_ip.reg_id);
let message = AccountCredentialMessage {
message_expiry: TransactionTime { seconds: expiry },
credential: AccountCredential::Initial::<id::constants::IpPairing, _, _> { icdi },
};
let v_initial_cdi = Versioned::new(VERSION_0, message);
let response = IdentityCreation {
id_obj: vid,
account_address,
ar_record,
request: v_initial_cdi,
};
Ok(response)
}
/// Create a version 1 identity object and the anonymity revocation record.
fn create_identity_object_v1(
ip_info_bytes: &[u8],
request_bytes: &[u8],
alist_bytes: &[u8],
ip_private_key_bytes: &[u8],
) -> anyhow::Result<IdentityCreationV1> {
let ip_info: IpInfo<Bls12> = parse_exact_versioned_ip_info(ip_info_bytes)?;
let alist: ExampleAttributeList =
serde_json::from_slice(alist_bytes).context("Could not parse attribute list")?;
let ip_private_key_str = std::str::from_utf8(ip_private_key_bytes)?;
let ip_private_key: id::ps_sig::SecretKey<Bls12> =
base16_decode_string(ip_private_key_str).context("Could not parse ip_private_key")?;
let request: PreIdentityObjectV1<Bls12, ExampleCurve> =
parse_exact_versioned_pio_from_request_v1(request_bytes)?;
let signature = match sign_identity_object_v1(&request, &ip_info, &alist, &ip_private_key) {
Ok(sig) => sig,
Err(e) => anyhow::bail!("Signing failed, {}", e),
};
let ar_record = Versioned::new(VERSION_0, AnonymityRevocationRecord {
id_cred_pub: request.id_cred_pub,
ar_data: request.ip_ar_data.clone(),
max_accounts: alist.max_accounts,
threshold: request.choice_ar_parameters.threshold,
});
let id = IdentityObjectV1 {
pre_identity_object: request,
alist,
signature,
};
let vid = Versioned::new(VERSION_0, id);
let response = IdentityCreationV1 {
id_obj: vid,
ar_record,
};
Ok(response)
}
/// Validate an identity recovery request
fn validate_recovery_request(
global_context_bytes: &[u8],
ip_info_bytes: &[u8],
request_bytes: &[u8],
) -> anyhow::Result<()> {
let global_context: GlobalContext<ExampleCurve> =
parse_exact_versioned_global_context(global_context_bytes)?;
let ip_info: IpInfo<Bls12> = parse_exact_versioned_ip_info(ip_info_bytes)?;
let recovery_request: IdRecoveryRequest<ExampleCurve> =
parse_exact_versioned_recovery_request(request_bytes)?;
if !validate_id_recovery_request(&ip_info, &global_context, &recovery_request) {
anyhow::bail!("Invalid ID ownership proof.");
}
Ok(())
}
#[cfg(feature = "csharp")]
mod cs_exports;
#[cfg(feature = "nodejs")]
mod nodejs_exports;