matc/
messages.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
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
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use rand::RngCore;
use core::fmt;
use std::io::{Read, Result, Write};

use crate::tlv::{self, TlvItem, TlvItemEnc, TlvItemValueEnc};

#[derive(Debug)]
pub struct MessageHeader {
    pub flags: u8,
    pub security_flags: u8,
    pub session_id: u16,
    pub message_counter: u32,
    pub source_node_id: Option<Vec<u8>>,
    pub destination_node_id: Option<Vec<u8>>,
}

impl MessageHeader {
    const FLAG_SRC_PRESENT: u8 = 4;
    const DSIZ_64: u8 = 1;
    const DSIZ_16: u8 = 2;
    pub fn encode(&self) -> Result<Vec<u8>> {
        let mut flags: u8 = 0;
        if self.source_node_id.as_ref().is_some_and(|x| x.len() == 8) {
            flags |= Self::FLAG_SRC_PRESENT;
        }
        if let Some(destination_node_id) = &self.destination_node_id {
            if destination_node_id.len() == 2 {
                flags |= Self::DSIZ_16
            } else if destination_node_id.len() == 8 {
                flags |= Self::DSIZ_64
            }
        }
        let mut out = Vec::with_capacity(1024);
        out.write_u8(flags)?;
        out.write_u16::<LittleEndian>(self.session_id)?;
        out.write_u8(self.security_flags)?;
        out.write_u32::<LittleEndian>(self.message_counter)?;
        if let Some(sn) = &self.source_node_id {
            if sn.len() == 8 {
                out.write_all(sn)?;
            }
        }
        if let Some(destination_node_id) = &self.destination_node_id {
            out.write_all(destination_node_id)?;
        }
        Ok(out)
    }
    pub fn decode(data: &[u8]) -> Result<(Self, Vec<u8>)> {
        let mut cursor = std::io::Cursor::new(data);
        let flags = cursor.read_u8()?;
        let session_id = cursor.read_u16::<LittleEndian>()?;
        let security_flags = cursor.read_u8()?;
        let message_counter = cursor.read_u32::<LittleEndian>()?;
        let source_node_id = if (flags & Self::FLAG_SRC_PRESENT) != 0 {
            let mut sn = vec![0; 8];
            cursor.read_exact(sn.as_mut())?;
            Some(sn)
        } else {
            None
        };
        let destination_node_id = if (flags & 3) != 0 {
            let dst_size = match flags & 3 {
                Self::DSIZ_64 => 8,
                Self::DSIZ_16 => 2,
                _ => 0,
            };
            if dst_size > 0 {
                let mut dn = vec![0; dst_size];
                cursor.read_exact(dn.as_mut())?;
                Some(dn)
            } else {
                None
            }
        } else {
            None
        };
        let mut rest = Vec::new();
        cursor.read_to_end(&mut rest)?;
        Ok((
            Self {
                flags,
                security_flags,
                session_id,
                message_counter,
                source_node_id,
                destination_node_id,
            },
            rest,
        ))
    }
}

/*#[derive(Debug)]
enum SecChannelOpcode {
    None = 0x0,
    Ack = 0x10,
    PbkdfReq = 0x20,
    PbkdfResp = 0x21,
    Pake1 = 0x22,
    Pake2 = 0x23,
    Pake3 = 0x24,
    Sigma1 = 0x30,
    Sigma2 = 0x31,
    Sigma3 = 0x32,
    Status = 0x40,
}*/

#[derive(Debug)]
pub struct ProtocolMessageHeader {
    exchange_flags: u8,
    pub opcode: u8,
    pub exchange_id: u16,
    pub protocol_id: u16,
    pub ack_counter: u32,
}

impl ProtocolMessageHeader {
    pub const FLAG_INITIATOR: u8 = 1;
    pub const FLAG_ACK: u8 = 2;
    pub const FLAG_RELIABILITY: u8 = 4;

    pub const OPCODE_ACK: u8 = 0x10;
    pub const OPCODE_PBKDF_REQ: u8 = 0x20;
    pub const OPCODE_PBKDF_RESP: u8 = 0x21;
    pub const OPCODE_PASE_PAKE1: u8 = 0x22;
    pub const OPCODE_PASE_PAKE2: u8 = 0x23;
    pub const OPCODE_PASE_PAKE3: u8 = 0x24;
    pub const OPCODE_CASE_SIGMA1: u8 = 0x30;
    pub const OPCODE_CASE_SIGMA2: u8 = 0x31;
    pub const OPCODE_CASE_SIGMA3: u8 = 0x32;
    pub const OPCODE_STATUS: u8 = 0x40;

    pub const INTERACTION_OPCODE_STATUS_RESP: u8 = 0x1;
    pub const INTERACTION_OPCODE_READ_REQ: u8 = 0x2;
    pub const INTERACTION_OPCODE_REPORT_DATA: u8 = 0x5;
    pub const INTERACTION_OPCODE_INVOKE_REQ: u8 = 0x8;

    pub const PROTOCOL_ID_SECURE_CHANNEL: u16 = 0;
    pub const PROTOCOL_ID_INTERACTION: u16 = 1;

    pub fn encode(&self) -> Result<Vec<u8>> {
        let mut out = Vec::with_capacity(1024);
        out.write_u8(self.exchange_flags)?;
        out.write_u8(self.opcode)?;
        out.write_u16::<LittleEndian>(self.exchange_id)?;
        out.write_u16::<LittleEndian>(self.protocol_id)?;
        if (self.exchange_flags & Self::FLAG_ACK) != 0 {
            out.write_u32::<LittleEndian>(self.ack_counter)?;
        }
        Ok(out)
    }
    pub fn decode(data: &[u8]) -> Result<(Self, Vec<u8>)> {
        let mut cursor = std::io::Cursor::new(data);
        let exchange_flags = cursor.read_u8()?;
        let opcode = cursor.read_u8()?;
        let exchange_id = cursor.read_u16::<LittleEndian>()?;
        let protocol_id = cursor.read_u16::<LittleEndian>()?;
        let mut ack_counter = 0;
        if (exchange_flags & Self::FLAG_ACK) != 0 {
            ack_counter = cursor.read_u32::<LittleEndian>()?;
        }
        let mut rest = Vec::new();
        cursor.read_to_end(&mut rest)?;
        Ok((
            Self {
                exchange_flags,
                opcode,
                exchange_id,
                protocol_id,
                ack_counter,
            },
            rest,
        ))
    }
}

#[derive(Debug, Clone, Copy)]
pub struct StatusReportInfo {
    general_code: u16,
    protocol_id: u32,
    protocol_code: u16,
}
impl StatusReportInfo {
    fn parse(data: &[u8]) -> Result<Self> {
        let mut cursor = std::io::Cursor::new(data);
        let general_code = cursor.read_u16::<LittleEndian>()?;
        let protocol_id = cursor.read_u32::<LittleEndian>()?;
        let protocol_code = cursor.read_u16::<LittleEndian>()?;
        Ok(Self {
            general_code,
            protocol_id,
            protocol_code,
        })
    }
    pub fn is_ok(&self) -> bool {
        self.general_code == 0 && self.protocol_id == 0 && self.protocol_code == 0
    }
}

pub struct Message {
    pub message_header: MessageHeader,
    pub protocol_header: ProtocolMessageHeader,
    pub payload: Vec<u8>,
    pub tlv: TlvItem,
    pub status_report_info: Option<StatusReportInfo>,
}

impl fmt::Debug for Message {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Message")
            .field("message_header", &self.message_header)
            .field("protocol_header", &self.protocol_header)
            .field("payload", &hex::encode(&self.payload))
            .field("tlv", &self.tlv)
            .field("status_report_info", &self.status_report_info)
            .finish()
    }
}

impl Message {
    pub fn decode(data: &[u8]) -> Result<Self> {
        let (message_header, rest) = MessageHeader::decode(data)?;
        let (protocol_header, rest) = ProtocolMessageHeader::decode(&rest)?;
        if (protocol_header.protocol_id == ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL)
            && (protocol_header.opcode == ProtocolMessageHeader::OPCODE_STATUS)
        {
            let status_report_info = StatusReportInfo::parse(&rest)?;
            return Ok(Self {
                message_header,
                protocol_header,
                payload: rest,
                tlv: TlvItem {
                    tag: 0,
                    value: tlv::TlvItemValue::Invalid(),
                },
                status_report_info: Some(status_report_info),
            });
        }
        let tlv = tlv::decode_tlv(&rest)?;
        Ok(Self {
            message_header,
            protocol_header,
            payload: rest,
            tlv,
            status_report_info: None,
        })
    }
}

pub fn ack(exchange: u16, ack: i64) -> Result<Vec<u8>> {
    let mut flags = ProtocolMessageHeader::FLAG_INITIATOR;
    flags |= ProtocolMessageHeader::FLAG_ACK;
    ProtocolMessageHeader {
        exchange_flags: flags,
        opcode: ProtocolMessageHeader::OPCODE_ACK,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: ack as u32,
    }
    .encode()
}

pub fn pbkdf_req(exchange: u16) -> Result<Vec<u8>> {
    let mut b = ProtocolMessageHeader {
        exchange_flags: ProtocolMessageHeader::FLAG_INITIATOR
            | ProtocolMessageHeader::FLAG_RELIABILITY,
        opcode: ProtocolMessageHeader::OPCODE_PBKDF_REQ,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: 0,
    }
    .encode()?;
    let mut tlv = tlv::TlvBuffer::new();
    tlv.write_anon_struct()?;
    let mut initiator_random = [0u8; 32];
    rand::thread_rng().fill_bytes(&mut initiator_random);
    tlv.write_octetstring(0x1, &initiator_random)?;
    tlv.write_uint16(2, 1)?;
    tlv.write_uint8(3, 0)?;
    tlv.write_bool(4, false)?;
    tlv.write_struct_end()?;
    b.write_all(&tlv.data)?;
    Ok(b)
}

pub fn pake1(exchange: u16, key: &[u8], ack: i64) -> Result<Vec<u8>> {
    let mut flags = ProtocolMessageHeader::FLAG_INITIATOR | ProtocolMessageHeader::FLAG_RELIABILITY;
    if ack >= 0 {
        flags |= ProtocolMessageHeader::FLAG_ACK
    }
    let mut b = ProtocolMessageHeader {
        exchange_flags: flags,
        opcode: ProtocolMessageHeader::OPCODE_PASE_PAKE1,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: ack as u32,
    }
    .encode()?;

    let tlv = TlvItemEnc {
        tag: 0,
        value: TlvItemValueEnc::StructAnon(vec![TlvItemEnc {
            tag: 1,
            value: TlvItemValueEnc::OctetString(key.to_owned()),
        }]),
    }
    .encode()?;
    b.write_all(&tlv)?;

    Ok(b)
}

pub fn pake3(exchange: u16, key: &[u8], ack: i64) -> Result<Vec<u8>> {
    let mut flags = 0x5;
    if ack >= 0 {
        flags |= 2
    }
    let mut b = ProtocolMessageHeader {
        exchange_flags: flags,
        opcode: ProtocolMessageHeader::OPCODE_PASE_PAKE3,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: ack as u32,
    }
    .encode()?;
    let mut tlv = tlv::TlvBuffer::new();
    tlv.write_anon_struct()?;
    tlv.write_octetstring(0x1, key)?;
    tlv.write_struct_end()?;

    b.write_all(&tlv.data)?;
    Ok(b)
}

pub fn sigma1(exchange: u16, payload: &[u8]) -> Result<Vec<u8>> {
    let mut b = ProtocolMessageHeader {
        exchange_flags: 5,
        opcode: ProtocolMessageHeader::OPCODE_CASE_SIGMA1,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: 0,
    }
    .encode()?;
    b.write_all(payload)?;
    Ok(b)
}

pub fn sigma3(exchange: u16, payload: &[u8]) -> Result<Vec<u8>> {
    let mut b = ProtocolMessageHeader {
        exchange_flags: 5,
        opcode: ProtocolMessageHeader::OPCODE_CASE_SIGMA3,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_SECURE_CHANNEL,
        ack_counter: 0,
    }
    .encode()?;
    b.write_all(payload)?;
    Ok(b)
}

pub fn im_invoke_request(
    endpoint: u16,
    cluster: u32,
    command: u32,
    exchange_id: u16,
    payload: &[u8],
    timed: bool,
) -> Result<Vec<u8>> {
    let b = ProtocolMessageHeader {
        exchange_flags: 5,
        opcode: ProtocolMessageHeader::INTERACTION_OPCODE_INVOKE_REQ,
        exchange_id,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_INTERACTION,
        ack_counter: 0,
    }
    .encode()?;

    let mut tlv = tlv::TlvBuffer::from_vec(b);
    tlv.write_anon_struct()?;
    tlv.write_bool(0x0, false)?;
    tlv.write_bool(0x1, timed)?; // timed
    tlv.write_array(2)?;
    tlv.write_anon_struct()?;
    tlv.write_list(0)?;
    tlv.write_uint16(0, endpoint)?;
    tlv.write_uint32(1, cluster)?;
    tlv.write_uint32(2, command)?;
    tlv.write_struct_end()?;
    tlv.write_struct(1)?;
    tlv.write_raw(payload)?;
    tlv.write_struct_end()?;
    tlv.write_struct_end()?;
    tlv.write_struct_end()?;
    tlv.write_uint8(0xff, 10)?;
    tlv.write_struct_end()?;
    Ok(tlv.data)
}

pub fn im_read_request(endpoint: u16, cluster: u32, attr: u32, exchange: u16) -> Result<Vec<u8>> {
    let b = ProtocolMessageHeader {
        exchange_flags: 5,
        opcode: ProtocolMessageHeader::INTERACTION_OPCODE_READ_REQ,
        exchange_id: exchange,
        protocol_id: ProtocolMessageHeader::PROTOCOL_ID_INTERACTION,
        ack_counter: 0,
    }
    .encode()?;

    let mut tlv = tlv::TlvBuffer::from_vec(b);
    tlv.write_anon_struct()?;
    tlv.write_array(0)?;
    tlv.write_anon_list()?;
    tlv.write_uint16(2, endpoint)?;
    tlv.write_uint32(3, cluster)?;
    tlv.write_uint32(4, attr)?;
    tlv.write_struct_end()?;
    tlv.write_struct_end()?;
    tlv.write_bool(3, true)?;
    tlv.write_uint8(0xff, 10)?;
    tlv.write_struct_end()?;
    Ok(tlv.data)
}

#[cfg(test)]
mod tests {
    use super::Message;

    #[test]
    pub fn test_1() {
        let msg = "04000000a5a0b90d3320764c7d52ef86052060d5000015300120cabe444262d4e5dd568c755ed77e0829b9983c4d62b480b579811ec383eb69c625020837240300280418";
        let msg = hex::decode(msg).unwrap();
        let m = Message::decode(&msg).unwrap();
        println!("{:?}", m);

        let msg = "04000000000000000000000000000000012001000000153001203052998af1897150086e6c84003c074df93a796b4f68a9221ee4e40325014aaf25020100240300280418";
        let msg = hex::decode(msg).unwrap();
        let m = Message::decode(&msg).unwrap();
        println!("{:?}", m);
    }
}