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
use crate::non::*;
use cyfs_base::*;

use std::fmt;
use std::str::FromStr;

#[derive(Debug, Clone)]
pub struct CryptoOutputRequestCommon {
    // 请求路径,可为空
    pub req_path: Option<String>,

    // 来源DEC
    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(())
    }
}

//// sign
///
// 可以选择使用people签名还是device签名
pub const CRYPTO_REQUEST_FLAG_SIGN_BY_PEOPLE: u32 = 0x01 << 1;
pub const CRYPTO_REQUEST_FLAG_SIGN_BY_DEVICE: u32 = 0x01 << 2;

// (desc, body) * (set, push),优先使用set > push
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(())
    }
}

//// verify
///

#[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的有效签名
    Owner,

    // 自校验
    Own,

    // 校验是否有指定object的有效签名
    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(),
            // 自校验只需要校验body即可
            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)
    }
}