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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::non::*;
use cyfs_base::*;
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct CryptoOutputRequestCommon {
pub req_path: Option<String>,
pub dec_id: Option<ObjectId>,
pub target: Option<ObjectId>,
pub flags: u32,
}
impl Default for CryptoOutputRequestCommon {
fn default() -> Self {
Self {
req_path: None,
dec_id: None,
target: None,
flags: 0,
}
}
}
impl fmt::Display for CryptoOutputRequestCommon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "req_path: {:?}", self.req_path)?;
if let Some(dec_id) = &self.dec_id {
write!(f, ", dec_id: {}", dec_id)?;
}
if let Some(target) = &self.target {
write!(f, ", target: {}", target.to_string())?;
}
write!(f, ", flags: {}", self.flags)?;
Ok(())
}
}
pub const CRYPTO_REQUEST_FLAG_SIGN_BY_PEOPLE: u32 = 0x01 << 1;
pub const CRYPTO_REQUEST_FLAG_SIGN_BY_DEVICE: u32 = 0x01 << 2;
pub const CRYPTO_REQUEST_FLAG_SIGN_SET_DESC: u32 = 0x01 << 3;
pub const CRYPTO_REQUEST_FLAG_SIGN_SET_BODY: u32 = 0x01 << 4;
pub const CRYPTO_REQUEST_FLAG_SIGN_PUSH_DESC: u32 = 0x01 << 5;
pub const CRYPTO_REQUEST_FLAG_SIGN_PUSH_BODY: u32 = 0x01 << 6;
pub struct CryptoSignObjectOutputRequest {
pub common: CryptoOutputRequestCommon,
pub object: NONObjectInfo,
pub flags: u32,
}
impl CryptoSignObjectOutputRequest {
pub fn new(object_id: ObjectId, object_raw: Vec<u8>, flags: u32) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
object: NONObjectInfo::new(object_id, object_raw, None),
flags,
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum SignObjectResult {
Signed,
Pending,
}
impl ToString for SignObjectResult {
fn to_string(&self) -> String {
(match *self {
Self::Signed => "signed",
Self::Pending => "pending",
})
.to_owned()
}
}
impl FromStr for SignObjectResult {
type Err = BuckyError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let ret = match value {
"signed" => Self::Signed,
"pending" => Self::Pending,
v @ _ => {
let msg = format!("unknown sign object result : {}", v);
error!("{}", msg);
return Err(BuckyError::from(msg));
}
};
Ok(ret)
}
}
#[derive(Debug, Clone)]
pub struct CryptoSignObjectOutputResponse {
pub result: SignObjectResult,
pub object: Option<NONObjectInfo>,
}
impl fmt::Display for CryptoSignObjectOutputResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "result: {:?}", self.result)?;
if let Some(object) = &self.object {
write!(f, ", object: {:?}", object)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum VerifySignType {
Desc,
Body,
Both,
}
impl VerifySignType {
pub fn desc(&self) -> bool {
match *self {
Self::Desc | Self::Both => true,
_ => false,
}
}
pub fn body(&self) -> bool {
match *self {
Self::Body | Self::Both => true,
_ => false,
}
}
}
impl ToString for VerifySignType {
fn to_string(&self) -> String {
(match *self {
Self::Desc => "desc",
Self::Body => "body",
Self::Both => "both",
})
.to_owned()
}
}
impl FromStr for VerifySignType {
type Err = BuckyError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let ret = match value {
"desc" => Self::Desc,
"body" => Self::Body,
"both" => Self::Both,
v @ _ => {
let msg = format!("unknown verify sign type : {}", v);
error!("{}", msg);
return Err(BuckyError::from(msg));
}
};
Ok(ret)
}
}
#[derive(Debug, Clone)]
pub struct VerifySigns {
pub desc_signs: Option<Vec<u8>>,
pub body_signs: Option<Vec<u8>>,
}
#[derive(Debug, Clone)]
pub enum VerifyObjectType {
Owner,
Own,
Object(NONSlimObjectInfo),
Sign(VerifySigns),
}
impl VerifyObjectType {
pub fn as_str(&self) -> &str {
match *self {
Self::Owner => "owner",
Self::Own => "own",
Self::Object(_) => "object",
Self::Sign(_) => "sign",
}
}
}
impl ToString for VerifyObjectType {
fn to_string(&self) -> String {
self.as_str().to_owned()
}
}
#[derive(Debug, Clone)]
pub struct CryptoVerifyObjectOutputRequest {
pub common: CryptoOutputRequestCommon,
pub sign_type: VerifySignType,
pub object: NONObjectInfo,
pub sign_object: VerifyObjectType,
}
impl CryptoVerifyObjectOutputRequest {
pub fn new_verify_by_owner(sign_type: VerifySignType, object: NONObjectInfo) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
sign_type,
object,
sign_object: VerifyObjectType::Owner,
}
}
pub fn new_verify_by_own(object: NONObjectInfo) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
sign_type: VerifySignType::Body,
object,
sign_object: VerifyObjectType::Owner,
}
}
pub fn new_verify_by_object(
sign_type: VerifySignType,
object: NONObjectInfo,
sign_object: NONSlimObjectInfo,
) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
sign_type,
object,
sign_object: VerifyObjectType::Object(sign_object),
}
}
pub fn new_verify_by_signs(
sign_type: VerifySignType,
object: NONObjectInfo,
signs: VerifySigns,
) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
sign_type,
object,
sign_object: VerifyObjectType::Sign(signs),
}
}
}
#[derive(Debug, Clone)]
pub struct VerifySignResult {
pub index: u8,
pub valid: bool,
pub sign_object_id: ObjectId,
}
#[derive(Debug, Clone)]
pub struct VerifyObjectResult {
pub valid: bool,
pub desc_signs: Vec<VerifySignResult>,
pub body_signs: Vec<VerifySignResult>,
}
impl Default for VerifyObjectResult {
fn default() -> Self {
Self {
valid: false,
desc_signs: Vec::new(),
body_signs: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct CryptoVerifyObjectOutputResponse {
pub result: VerifyObjectResult,
}
impl fmt::Display for CryptoVerifyObjectOutputResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "result: {:?}", self.result)
}
}
pub const CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER: u32 = 0x01 << 1;
pub const CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE: u32 = 0x01 << 2;
#[derive(Debug, Clone)]
pub enum CryptoEncryptType {
EncryptData = 0,
GenAESKeyAndEncrypt = 1,
}
impl CryptoEncryptType {
pub fn as_str(&self) -> &str {
match *self {
Self::EncryptData => "encrypt_data",
Self::GenAESKeyAndEncrypt => "gen_aeskey_and_encrypt",
}
}
}
impl ToString for CryptoEncryptType {
fn to_string(&self) -> String {
self.as_str().to_owned()
}
}
impl FromStr for CryptoEncryptType {
type Err = BuckyError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let ret = match value {
"encrypt_data" => Self::EncryptData,
"gen_aeskey_and_encrypt" => Self::GenAESKeyAndEncrypt,
v @ _ => {
let msg = format!("unknown crypto encrypt type: {}", v);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
};
Ok(ret)
}
}
#[derive(Debug, Clone)]
pub struct CryptoEncryptDataOutputRequest {
pub common: CryptoOutputRequestCommon,
pub encrypt_type: CryptoEncryptType,
pub data: Option<Vec<u8>>,
pub flags: u32,
}
impl CryptoEncryptDataOutputRequest {
pub fn new() -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
encrypt_type: CryptoEncryptType::EncryptData,
data: None,
flags: CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE,
}
}
pub fn by_owner(mut self) -> Self {
self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
self
}
pub fn by_device(mut self) -> Self {
self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
self
}
pub fn data(mut self, data: Vec<u8>) -> Self {
self.data = Some(data);
self
}
pub fn encrypt_data(mut self) -> Self {
self.encrypt_type = CryptoEncryptType::EncryptData;
self
}
pub fn gen_aeskey_and_encrypt(mut self) -> Self {
self.encrypt_type = CryptoEncryptType::GenAESKeyAndEncrypt;
self
}
}
pub struct CryptoEncryptDataOutputResponse {
pub aes_key: Option<AesKey>,
pub result: Vec<u8>,
}
impl fmt::Display for CryptoEncryptDataOutputResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(aes_key) = &self.aes_key {
write!(f, "aes_key: {}", aes_key.len())?;
}
write!(f, ", result: {}", self.result.len())
}
}
#[derive(Debug, Clone)]
pub enum CryptoDecryptType {
DecryptData = 0,
DecryptAESKey = 1,
}
impl CryptoDecryptType {
pub fn as_str(&self) -> &str {
match *self {
Self::DecryptData => "decrypt_data",
Self::DecryptAESKey => "decrypt_aeskey",
}
}
}
impl ToString for CryptoDecryptType {
fn to_string(&self) -> String {
self.as_str().to_owned()
}
}
impl FromStr for CryptoDecryptType {
type Err = BuckyError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let ret = match value {
"decrypt_data" => Self::DecryptData,
"decrypt_aeskey" => Self::DecryptAESKey,
v @ _ => {
let msg = format!("unknown crypto decrypt type: {}", v);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
}
};
Ok(ret)
}
}
#[derive(Debug, Clone)]
pub struct CryptoDecryptDataOutputRequest {
pub common: CryptoOutputRequestCommon,
pub decrypt_type: CryptoDecryptType,
pub data: Vec<u8>,
pub flags: u32,
}
impl CryptoDecryptDataOutputRequest {
pub fn new(data: Vec<u8>) -> Self {
Self {
common: CryptoOutputRequestCommon::default(),
decrypt_type: CryptoDecryptType::DecryptData,
data,
flags: CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE,
}
}
pub fn by_owner(mut self) -> Self {
self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
self
}
pub fn by_device(mut self) -> Self {
self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
self
}
pub fn data(mut self, data: Vec<u8>) -> Self {
self.data = data;
self
}
pub fn decrypt_data(mut self) -> Self {
self.decrypt_type = CryptoDecryptType::DecryptData;
self
}
pub fn decrypt_aeskey(mut self) -> Self {
self.decrypt_type = CryptoDecryptType::DecryptAESKey;
self
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum DecryptDataResult {
Decrypted,
Pending,
}
impl DecryptDataResult {
pub fn as_str(&self) -> &str {
match *self {
Self::Decrypted => "decrypted",
Self::Pending => "pending",
}
}
}
impl ToString for DecryptDataResult {
fn to_string(&self) -> String {
self.as_str().to_owned()
}
}
impl FromStr for DecryptDataResult {
type Err = BuckyError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let ret = match value {
"decrypted" => Self::Decrypted,
"pending" => Self::Pending,
v @ _ => {
let msg = format!("unknown decrypt data result : {}", v);
error!("{}", msg);
return Err(BuckyError::from(msg));
}
};
Ok(ret)
}
}
pub struct CryptoDecryptDataOutputResponse {
pub result: DecryptDataResult,
pub data: Vec<u8>,
}
impl fmt::Display for CryptoDecryptDataOutputResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "result: {}, data: {}", self.result.as_str(), self.data.len())
}
}