1use crate::non::*;
2use cyfs_base::*;
3
4use std::fmt;
5use std::str::FromStr;
6
7#[derive(Debug, Clone)]
8pub struct CryptoOutputRequestCommon {
9 pub req_path: Option<String>,
11
12 pub dec_id: Option<ObjectId>,
14
15 pub target: Option<ObjectId>,
17
18 pub flags: u32,
19}
20
21impl Default for CryptoOutputRequestCommon {
22 fn default() -> Self {
23 Self {
24 req_path: None,
25 dec_id: None,
26 target: None,
27 flags: 0,
28 }
29 }
30}
31
32impl fmt::Display for CryptoOutputRequestCommon {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "req_path: {:?}", self.req_path)?;
35
36 if let Some(dec_id) = &self.dec_id {
37 write!(f, ", dec_id: {}", dec_id)?;
38 }
39
40 if let Some(target) = &self.target {
41 write!(f, ", target: {}", target.to_string())?;
42 }
43
44 write!(f, ", flags: {}", self.flags)?;
45
46 Ok(())
47 }
48}
49
50pub const CRYPTO_REQUEST_FLAG_SIGN_BY_PEOPLE: u32 = 0x01 << 1;
54pub const CRYPTO_REQUEST_FLAG_SIGN_BY_DEVICE: u32 = 0x01 << 2;
55
56pub const CRYPTO_REQUEST_FLAG_SIGN_SET_DESC: u32 = 0x01 << 3;
58pub const CRYPTO_REQUEST_FLAG_SIGN_SET_BODY: u32 = 0x01 << 4;
59pub const CRYPTO_REQUEST_FLAG_SIGN_PUSH_DESC: u32 = 0x01 << 5;
60pub const CRYPTO_REQUEST_FLAG_SIGN_PUSH_BODY: u32 = 0x01 << 6;
61
62pub struct CryptoSignObjectOutputRequest {
63 pub common: CryptoOutputRequestCommon,
64
65 pub object: NONObjectInfo,
66
67 pub flags: u32,
68}
69
70impl CryptoSignObjectOutputRequest {
71 pub fn new(object_id: ObjectId, object_raw: Vec<u8>, flags: u32) -> Self {
72 Self {
73 common: CryptoOutputRequestCommon::default(),
74 object: NONObjectInfo::new(object_id, object_raw, None),
75 flags,
76 }
77 }
78}
79
80#[derive(Debug, Clone, Eq, PartialEq)]
81pub enum SignObjectResult {
82 Signed,
83 Pending,
84}
85
86impl ToString for SignObjectResult {
87 fn to_string(&self) -> String {
88 (match *self {
89 Self::Signed => "signed",
90 Self::Pending => "pending",
91 })
92 .to_owned()
93 }
94}
95
96impl FromStr for SignObjectResult {
97 type Err = BuckyError;
98
99 fn from_str(value: &str) -> Result<Self, Self::Err> {
100 let ret = match value {
101 "signed" => Self::Signed,
102 "pending" => Self::Pending,
103 v @ _ => {
104 let msg = format!("unknown sign object result : {}", v);
105 error!("{}", msg);
106
107 return Err(BuckyError::from(msg));
108 }
109 };
110
111 Ok(ret)
112 }
113}
114
115#[derive(Debug, Clone)]
116pub struct CryptoSignObjectOutputResponse {
117 pub result: SignObjectResult,
118
119 pub object: Option<NONObjectInfo>,
120}
121
122impl fmt::Display for CryptoSignObjectOutputResponse {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 write!(f, "result: {:?}", self.result)?;
125
126 if let Some(object) = &self.object {
127 write!(f, ", object: {:?}", object)?;
128 }
129 Ok(())
130 }
131}
132
133#[derive(Debug, Clone, Eq, PartialEq)]
137pub enum VerifySignType {
138 Desc,
139 Body,
140 Both,
141}
142
143impl VerifySignType {
144 pub fn desc(&self) -> bool {
145 match *self {
146 Self::Desc | Self::Both => true,
147 _ => false,
148 }
149 }
150
151 pub fn body(&self) -> bool {
152 match *self {
153 Self::Body | Self::Both => true,
154 _ => false,
155 }
156 }
157}
158impl ToString for VerifySignType {
159 fn to_string(&self) -> String {
160 (match *self {
161 Self::Desc => "desc",
162 Self::Body => "body",
163 Self::Both => "both",
164 })
165 .to_owned()
166 }
167}
168
169impl FromStr for VerifySignType {
170 type Err = BuckyError;
171
172 fn from_str(value: &str) -> Result<Self, Self::Err> {
173 let ret = match value {
174 "desc" => Self::Desc,
175 "body" => Self::Body,
176 "both" => Self::Both,
177 v @ _ => {
178 let msg = format!("unknown verify sign type : {}", v);
179 error!("{}", msg);
180
181 return Err(BuckyError::from(msg));
182 }
183 };
184
185 Ok(ret)
186 }
187}
188
189#[derive(Debug, Clone)]
191pub struct VerifySigns {
192 pub desc_signs: Option<Vec<u8>>,
193 pub body_signs: Option<Vec<u8>>,
194}
195
196#[derive(Debug, Clone)]
197pub enum VerifyObjectType {
198 Owner,
200
201 Own,
203
204 Object(NONSlimObjectInfo),
206
207 Sign(VerifySigns),
209}
210
211impl VerifyObjectType {
212 pub fn as_str(&self) -> &str {
213 match *self {
214 Self::Owner => "owner",
215 Self::Own => "own",
216 Self::Object(_) => "object",
217 Self::Sign(_) => "sign",
218 }
219 }
220}
221
222impl ToString for VerifyObjectType {
223 fn to_string(&self) -> String {
224 self.as_str().to_owned()
225 }
226}
227
228#[derive(Debug, Clone)]
229pub struct CryptoVerifyObjectOutputRequest {
230 pub common: CryptoOutputRequestCommon,
231
232 pub sign_type: VerifySignType,
234
235 pub object: NONObjectInfo,
237
238 pub sign_object: VerifyObjectType,
240}
241
242impl CryptoVerifyObjectOutputRequest {
243 pub fn new_verify_by_owner(sign_type: VerifySignType, object: NONObjectInfo) -> Self {
244 Self {
245 common: CryptoOutputRequestCommon::default(),
246 sign_type,
247 object,
248 sign_object: VerifyObjectType::Owner,
249 }
250 }
251
252 pub fn new_verify_by_own(object: NONObjectInfo) -> Self {
253 Self {
254 common: CryptoOutputRequestCommon::default(),
255 sign_type: VerifySignType::Body,
257 object,
258 sign_object: VerifyObjectType::Owner,
259 }
260 }
261
262 pub fn new_verify_by_object(
263 sign_type: VerifySignType,
264 object: NONObjectInfo,
265 sign_object: NONSlimObjectInfo,
266 ) -> Self {
267 Self {
268 common: CryptoOutputRequestCommon::default(),
269 sign_type,
270 object,
271 sign_object: VerifyObjectType::Object(sign_object),
272 }
273 }
274
275 pub fn new_verify_by_signs(
276 sign_type: VerifySignType,
277 object: NONObjectInfo,
278 signs: VerifySigns,
279 ) -> Self {
280 Self {
281 common: CryptoOutputRequestCommon::default(),
282 sign_type,
283 object,
284 sign_object: VerifyObjectType::Sign(signs),
285 }
286 }
287}
288
289#[derive(Debug, Clone)]
290pub struct VerifySignResult {
291 pub index: u8,
292 pub valid: bool,
293 pub sign_object_id: ObjectId,
294}
295
296#[derive(Debug, Clone)]
297pub struct VerifyObjectResult {
298 pub valid: bool,
299
300 pub desc_signs: Vec<VerifySignResult>,
301 pub body_signs: Vec<VerifySignResult>,
302}
303
304impl Default for VerifyObjectResult {
305 fn default() -> Self {
306 Self {
307 valid: false,
308 desc_signs: Vec::new(),
309 body_signs: Vec::new(),
310 }
311 }
312}
313
314#[derive(Debug, Clone)]
315pub struct CryptoVerifyObjectOutputResponse {
316 pub result: VerifyObjectResult,
317}
318
319impl fmt::Display for CryptoVerifyObjectOutputResponse {
320 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321 write!(f, "result: {:?}", self.result)
322 }
323}
324
325pub const CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER: u32 = 0x01 << 1;
328pub const CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE: u32 = 0x01 << 2;
329
330#[derive(Debug, Clone)]
331pub enum CryptoEncryptType {
332 EncryptData = 0,
333 GenAESKeyAndEncrypt = 1,
334}
335
336impl CryptoEncryptType {
337 pub fn as_str(&self) -> &str {
338 match *self {
339 Self::EncryptData => "encrypt_data",
340 Self::GenAESKeyAndEncrypt => "gen_aeskey_and_encrypt",
341 }
342 }
343}
344
345impl ToString for CryptoEncryptType {
346 fn to_string(&self) -> String {
347 self.as_str().to_owned()
348 }
349}
350
351impl FromStr for CryptoEncryptType {
352 type Err = BuckyError;
353
354 fn from_str(value: &str) -> Result<Self, Self::Err> {
355 let ret = match value {
356 "encrypt_data" => Self::EncryptData,
357 "gen_aeskey_and_encrypt" => Self::GenAESKeyAndEncrypt,
358 v @ _ => {
359 let msg = format!("unknown crypto encrypt type: {}", v);
360 error!("{}", msg);
361
362 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
363 }
364 };
365
366 Ok(ret)
367 }
368}
369
370#[derive(Debug, Clone)]
371pub struct CryptoEncryptDataOutputRequest {
372 pub common: CryptoOutputRequestCommon,
373
374 pub encrypt_type: CryptoEncryptType,
375
376 pub data: Option<Vec<u8>>,
377
378 pub flags: u32,
379}
380
381impl CryptoEncryptDataOutputRequest {
382 pub fn new() -> Self {
383 Self {
384 common: CryptoOutputRequestCommon::default(),
385 encrypt_type: CryptoEncryptType::EncryptData,
386 data: None,
387 flags: CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE,
388 }
389 }
390
391 pub fn by_owner(mut self) -> Self {
392 self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
393 self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
394 self
395 }
396
397 pub fn by_device(mut self) -> Self {
398 self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
399 self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
400 self
401 }
402
403 pub fn data(mut self, data: Vec<u8>) -> Self {
404 self.data = Some(data);
405 self
406 }
407
408 pub fn encrypt_data(mut self) -> Self {
409 self.encrypt_type = CryptoEncryptType::EncryptData;
410 self
411 }
412
413 pub fn gen_aeskey_and_encrypt(mut self) -> Self {
414 self.encrypt_type = CryptoEncryptType::GenAESKeyAndEncrypt;
415 self
416 }
417}
418pub struct CryptoEncryptDataOutputResponse {
419 pub aes_key: Option<AesKey>,
420
421 pub result: Vec<u8>,
422}
423
424impl fmt::Display for CryptoEncryptDataOutputResponse {
425 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
426 if let Some(aes_key) = &self.aes_key {
427 write!(f, "aes_key: {}", aes_key.len())?;
428 }
429 write!(f, ", result: {}", self.result.len())
430 }
431}
432
433#[derive(Debug, Clone)]
434pub enum CryptoDecryptType {
435 DecryptData = 0,
436 DecryptAESKey = 1,
437}
438
439impl CryptoDecryptType {
440 pub fn as_str(&self) -> &str {
441 match *self {
442 Self::DecryptData => "decrypt_data",
443 Self::DecryptAESKey => "decrypt_aeskey",
444 }
445 }
446}
447
448impl ToString for CryptoDecryptType {
449 fn to_string(&self) -> String {
450 self.as_str().to_owned()
451 }
452}
453
454impl FromStr for CryptoDecryptType {
455 type Err = BuckyError;
456
457 fn from_str(value: &str) -> Result<Self, Self::Err> {
458 let ret = match value {
459 "decrypt_data" => Self::DecryptData,
460 "decrypt_aeskey" => Self::DecryptAESKey,
461 v @ _ => {
462 let msg = format!("unknown crypto decrypt type: {}", v);
463 error!("{}", msg);
464
465 return Err(BuckyError::new(BuckyErrorCode::InvalidParam, msg));
466 }
467 };
468
469 Ok(ret)
470 }
471}
472
473#[derive(Debug, Clone)]
474pub struct CryptoDecryptDataOutputRequest {
475 pub common: CryptoOutputRequestCommon,
476
477 pub decrypt_type: CryptoDecryptType,
478
479 pub data: Vec<u8>,
480
481 pub flags: u32,
482}
483
484impl CryptoDecryptDataOutputRequest {
485 pub fn new(data: Vec<u8>) -> Self {
486 Self {
487 common: CryptoOutputRequestCommon::default(),
488 decrypt_type: CryptoDecryptType::DecryptData,
489 data,
490 flags: CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE,
491 }
492 }
493
494 pub fn by_owner(mut self) -> Self {
495 self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
496 self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
497 self
498 }
499
500 pub fn by_device(mut self) -> Self {
501 self.flags &= !CRYPTO_REQUEST_FLAG_CRYPT_BY_OWNER;
502 self.flags |= CRYPTO_REQUEST_FLAG_CRYPT_BY_DEVICE;
503 self
504 }
505
506 pub fn data(mut self, data: Vec<u8>) -> Self {
507 self.data = data;
508 self
509 }
510
511 pub fn decrypt_data(mut self) -> Self {
512 self.decrypt_type = CryptoDecryptType::DecryptData;
513 self
514 }
515
516 pub fn decrypt_aeskey(mut self) -> Self {
517 self.decrypt_type = CryptoDecryptType::DecryptAESKey;
518 self
519 }
520}
521
522#[derive(Debug, Clone, Eq, PartialEq)]
523pub enum DecryptDataResult {
524 Decrypted,
525 Pending,
526}
527
528impl DecryptDataResult {
529 pub fn as_str(&self) -> &str {
530 match *self {
531 Self::Decrypted => "decrypted",
532 Self::Pending => "pending",
533 }
534 }
535}
536
537impl ToString for DecryptDataResult {
538 fn to_string(&self) -> String {
539 self.as_str().to_owned()
540 }
541}
542
543impl FromStr for DecryptDataResult {
544 type Err = BuckyError;
545
546 fn from_str(value: &str) -> Result<Self, Self::Err> {
547 let ret = match value {
548 "decrypted" => Self::Decrypted,
549 "pending" => Self::Pending,
550 v @ _ => {
551 let msg = format!("unknown decrypt data result : {}", v);
552 error!("{}", msg);
553
554 return Err(BuckyError::from(msg));
555 }
556 };
557
558 Ok(ret)
559 }
560}
561
562
563pub struct CryptoDecryptDataOutputResponse {
564 pub result: DecryptDataResult,
565 pub data: Vec<u8>,
566}
567
568impl fmt::Display for CryptoDecryptDataOutputResponse {
569 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
570 write!(f, "result: {}, data: {}", self.result.as_str(), self.data.len())
571 }
572}