osdp 0.3.0

Pure-Rust, no_std-friendly implementation of the SIA Open Supervised Device Protocol (OSDP) v2.2
Documentation
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
//! Packet codec โ€” encode/decode the OSDP wire framing.
//!
//! # Spec: ยง5.9 Tables 1โ€“3
//!
//! Layout:
//!
//! ```text
//! +------+------+---------+---------+------+========+======+----+======+
//! | SOM  | ADDR | LEN_LSB | LEN_MSB | CTRL |  SCB?  | CMND | DAT| MAC? |  cksum/CRC
//! +------+------+---------+---------+------+========+======+----+======+
//! ```

use crate::SOM;
use crate::error::Error;
use crate::packet::checksum::checksum8;
use crate::packet::crc::crc16;
use crate::packet::header::{Address, ControlByte};
use crate::packet::scb::ScbView;
use crate::packet::trailer::Trailer;

/// Length of the fixed OSDP header (SOM, ADDR, LEN_LSB, LEN_MSB, CTRL).
pub const HEADER_LEN: usize = 5;

/// MAC trailer length on the wire (the first 4 bytes of the computed MAC).
pub const MAC_LEN: usize = 4;

/// View of a parsed OSDP packet, borrowed from the input slice.
///
/// The codec is *zero-copy*: payload, SCB data, and MAC slices all point into
/// the input buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParsedPacket<'a> {
    /// Source/destination address (with reply flag if from PD).
    pub addr: Address,
    /// Decoded CTRL byte.
    pub ctrl: ControlByte,
    /// Security Control Block, if `CTRL.HAS_SCB`.
    pub scb: Option<ScbView<'a>>,
    /// Command or reply code byte.
    pub code: u8,
    /// Application data following the code byte (excluding any MAC trailer
    /// and the CRC/checksum).
    pub data: &'a [u8],
    /// 4-byte MAC, if SCS_15..18.
    pub mac: Option<[u8; MAC_LEN]>,
    /// Trailer kind.
    pub trailer: Trailer,
}

impl<'a> ParsedPacket<'a> {
    /// Parse a complete packet from `buf`.
    ///
    /// Returns the parsed packet view and the number of bytes consumed
    /// (which equals the value of the LEN field).
    ///
    /// The parser is total โ€” every malformed input returns a typed
    /// [`Error`] rather than panicking.
    ///
    /// # Example
    ///
    /// ```
    /// use osdp::packet::ParsedPacket;
    /// // osdp_POLL frame addressed to PD 0x05, SQN=0, CRC-16/KERMIT trailer.
    /// let bytes = [0x53, 0x05, 0x08, 0x00, 0x04, 0x60, 0xBC, 0x89];
    /// let (parsed, used) = ParsedPacket::parse(&bytes)?;
    /// assert_eq!(used, bytes.len());
    /// assert_eq!(parsed.code, 0x60); // osdp_POLL
    /// assert!(parsed.data.is_empty());
    /// # Ok::<(), osdp::Error>(())
    /// ```
    pub fn parse(buf: &'a [u8]) -> Result<(Self, usize), Error> {
        if buf.len() < HEADER_LEN {
            return Err(Error::Truncated {
                have: buf.len(),
                need: HEADER_LEN,
            });
        }
        if buf[0] != SOM {
            return Err(Error::BadSom(buf[0]));
        }

        let addr = Address::from_raw(buf[1]);
        let len = u16::from_le_bytes([buf[2], buf[3]]) as usize;
        let ctrl = ControlByte::decode(buf[4])?;

        if buf.len() < len {
            return Err(Error::Truncated {
                have: buf.len(),
                need: len,
            });
        }
        let frame = &buf[..len];

        let trailer_len = if ctrl.use_crc() { 2 } else { 1 };
        if frame.len() < HEADER_LEN + 1 + trailer_len {
            return Err(Error::BadLength {
                declared: len,
                actual: frame.len(),
            });
        }

        let mut cursor = HEADER_LEN;

        let scb = if ctrl.has_scb() {
            let view = ScbView::parse(&frame[cursor..])?;
            cursor += view.wire_len();
            Some(view)
        } else {
            None
        };

        if cursor + 1 + trailer_len > frame.len() {
            return Err(Error::BadLength {
                declared: len,
                actual: frame.len(),
            });
        }
        let code = frame[cursor];
        cursor += 1;

        let mac_present = scb.map(|s| s.ty.has_mac()).unwrap_or(false);
        let mac_room = if mac_present { MAC_LEN } else { 0 };

        let payload_end =
            frame
                .len()
                .checked_sub(trailer_len + mac_room)
                .ok_or(Error::BadLength {
                    declared: len,
                    actual: frame.len(),
                })?;
        if payload_end < cursor {
            return Err(Error::BadLength {
                declared: len,
                actual: frame.len(),
            });
        }
        let data = &frame[cursor..payload_end];

        let mac = if mac_present {
            let m = &frame[payload_end..payload_end + MAC_LEN];
            let mut arr = [0u8; MAC_LEN];
            arr.copy_from_slice(m);
            Some(arr)
        } else {
            None
        };

        let trailer = if ctrl.use_crc() {
            let got = u16::from_le_bytes([frame[frame.len() - 2], frame[frame.len() - 1]]);
            let want = crc16(&frame[..frame.len() - 2]);
            if got != want {
                return Err(Error::BadCrc { got, want });
            }
            Trailer::Crc(got)
        } else {
            let got = frame[frame.len() - 1];
            let want = checksum8(&frame[..frame.len() - 1]);
            if got != want {
                return Err(Error::BadChecksum { got, want });
            }
            Trailer::Checksum(got)
        };

        Ok((
            Self {
                addr,
                ctrl,
                scb,
                code,
                data,
                mac,
                trailer,
            },
            len,
        ))
    }
}

#[cfg(feature = "alloc")]
mod alloc_impls {
    use super::*;
    use crate::packet::scb::Scb;
    use alloc::vec::Vec;

    /// Builder for a single OSDP packet.
    ///
    /// Caller fills in the high-level fields and calls
    /// [`PacketBuilder::encode`] (for non-secure packets) or
    /// [`PacketBuilder::encode_with_mac`] (for `SCS_15..=SCS_18`) to produce
    /// the bytes ready for transmission.
    #[derive(Debug, Clone)]
    pub struct PacketBuilder {
        /// Address byte (with reply flag if PD โ†’ ACU).
        pub addr: Address,
        /// CTRL byte (SQN + flags).
        pub ctrl: ControlByte,
        /// Security Control Block (None if `CTRL.HAS_SCB` is clear).
        pub scb: Option<Scb>,
        /// Command or reply code byte.
        pub code: u8,
        /// DATA payload (already encrypted, if SCS_17/18).
        pub data: Vec<u8>,
    }

    impl PacketBuilder {
        /// Build a packet with neither SCB nor MAC.
        pub fn plain(addr: Address, ctrl: ControlByte, code: u8, data: Vec<u8>) -> Self {
            Self {
                addr,
                ctrl,
                scb: None,
                code,
                data,
            }
        }

        /// Encode the packet without any MAC.
        ///
        /// If the CTRL byte has [`crate::packet::CtrlFlags::HAS_SCB`] set
        /// but the SCB type *would* require a MAC, the caller must use
        /// [`Self::encode_with_mac`] instead.
        pub fn encode(&self) -> Result<Vec<u8>, Error> {
            self.encode_inner(None::<fn(&[u8]) -> [u8; super::MAC_LEN]>)
        }

        /// Encode the packet, computing the MAC trailer with `mac_fn`.
        ///
        /// `mac_fn` receives the bytes from SOM through end-of-DATA (i.e.
        /// what the wire MAC is computed over) and returns the first 4 bytes
        /// of the resulting MAC.
        pub fn encode_with_mac<F>(&self, mac_fn: F) -> Result<Vec<u8>, Error>
        where
            F: FnOnce(&[u8]) -> [u8; super::MAC_LEN],
        {
            self.encode_inner(Some(mac_fn))
        }

        fn encode_inner<F>(&self, mac_fn: Option<F>) -> Result<Vec<u8>, Error>
        where
            F: FnOnce(&[u8]) -> [u8; super::MAC_LEN],
        {
            // Validate SCB <-> CTRL coherence.
            let scb_present = self.ctrl.has_scb();
            if scb_present != self.scb.is_some() {
                return Err(Error::BadControlByte(self.ctrl.encode()));
            }
            let mac_required = self.scb.as_ref().map(|s| s.ty.has_mac()).unwrap_or(false);
            if mac_required && mac_fn.is_none() {
                return Err(Error::BadMac);
            }

            let trailer_len = if self.ctrl.use_crc() { 2 } else { 1 };
            let mac_len = if mac_required { super::MAC_LEN } else { 0 };
            let scb_len = self.scb.as_ref().map(|s| s.data.len() + 2).unwrap_or(0);
            let total = HEADER_LEN + scb_len + 1 + self.data.len() + mac_len + trailer_len;

            if total > u16::MAX as usize {
                return Err(Error::BufferOverflow {
                    need: total,
                    have: u16::MAX as usize,
                });
            }

            let mut out = Vec::with_capacity(total);
            out.push(SOM);
            out.push(self.addr.as_byte());
            let len_bytes = (total as u16).to_le_bytes();
            out.push(len_bytes[0]);
            out.push(len_bytes[1]);
            out.push(self.ctrl.encode());

            if let Some(scb) = &self.scb {
                scb.encode(&mut out);
            }

            out.push(self.code);
            out.extend_from_slice(&self.data);

            if let Some(f) = mac_fn {
                let mac = f(&out);
                out.extend_from_slice(&mac);
            }

            if self.ctrl.use_crc() {
                let crc = crc16(&out);
                out.extend_from_slice(&crc.to_le_bytes());
            } else {
                out.push(checksum8(&out));
            }

            debug_assert_eq!(out.len(), total);
            Ok(out)
        }
    }
}

#[cfg(feature = "alloc")]
pub use alloc_impls::PacketBuilder;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::packet::header::{CtrlFlags, Sqn};
    use crate::packet::scb::{Scb, ScsType};
    use alloc::vec::Vec;

    fn make_poll(addr: u8, sqn: u8) -> Vec<u8> {
        PacketBuilder::plain(
            Address::pd(addr).unwrap(),
            ControlByte::new(Sqn::new(sqn).unwrap(), CtrlFlags::USE_CRC),
            0x60, // POLL
            Vec::new(),
        )
        .encode()
        .unwrap()
    }

    #[test]
    fn roundtrip_poll() {
        let bytes = make_poll(0x05, 1);
        let (parsed, used) = ParsedPacket::parse(&bytes).unwrap();
        assert_eq!(used, bytes.len());
        assert_eq!(parsed.addr.pd_addr(), 0x05);
        assert_eq!(parsed.ctrl.sqn.value(), 1);
        assert!(parsed.ctrl.use_crc());
        assert_eq!(parsed.code, 0x60);
        assert!(parsed.data.is_empty());
        assert!(parsed.scb.is_none());
        assert!(parsed.mac.is_none());
    }

    #[test]
    fn roundtrip_with_data_checksum() {
        let bytes = PacketBuilder::plain(
            Address::pd(0x01).unwrap(),
            ControlByte::new(Sqn::new(2).unwrap(), CtrlFlags::empty()),
            0x6E, // COMSET
            alloc::vec![0x00, 0x80, 0x25, 0x00, 0x00],
        )
        .encode()
        .unwrap();

        let (parsed, used) = ParsedPacket::parse(&bytes).unwrap();
        assert_eq!(used, bytes.len());
        assert_eq!(parsed.code, 0x6E);
        assert_eq!(parsed.data, &[0x00, 0x80, 0x25, 0x00, 0x00]);
        assert!(matches!(parsed.trailer, Trailer::Checksum(_)));
    }

    #[test]
    fn roundtrip_with_scb_no_mac() {
        let scb = Scb::new(ScsType::Scs11, [0x00, 1, 2, 3, 4, 5, 6, 7, 8]);
        let bytes = PacketBuilder {
            addr: Address::pd(0x01).unwrap(),
            ctrl: ControlByte::new(
                Sqn::new(0).unwrap(),
                CtrlFlags::USE_CRC | CtrlFlags::HAS_SCB,
            ),
            scb: Some(scb.clone()),
            code: 0x76,
            data: Vec::new(),
        }
        .encode()
        .unwrap();

        let (parsed, _) = ParsedPacket::parse(&bytes).unwrap();
        let view = parsed.scb.unwrap();
        assert_eq!(view.ty, ScsType::Scs11);
        assert_eq!(view.data, scb.data.as_slice());
        assert!(parsed.mac.is_none());
    }

    #[test]
    fn roundtrip_with_scb_and_mac() {
        let mac_value = [0xDE, 0xAD, 0xBE, 0xEF];
        let scb = Scb::new(ScsType::Scs15, []);
        let bytes = PacketBuilder {
            addr: Address::pd(0x02).unwrap(),
            ctrl: ControlByte::new(
                Sqn::new(2).unwrap(),
                CtrlFlags::USE_CRC | CtrlFlags::HAS_SCB,
            ),
            scb: Some(scb),
            code: 0x60,
            data: alloc::vec![0xAA, 0xBB],
        }
        .encode_with_mac(|_| mac_value)
        .unwrap();

        let (parsed, _) = ParsedPacket::parse(&bytes).unwrap();
        assert_eq!(parsed.mac.unwrap(), mac_value);
        assert_eq!(parsed.data, &[0xAA, 0xBB]);
    }

    #[test]
    fn rejects_bad_som() {
        let mut bytes = make_poll(1, 1);
        bytes[0] = 0x52;
        assert!(matches!(
            ParsedPacket::parse(&bytes),
            Err(Error::BadSom(0x52))
        ));
    }

    #[test]
    fn rejects_truncated() {
        let bytes = [0x53u8, 0x01];
        assert!(matches!(
            ParsedPacket::parse(&bytes),
            Err(Error::Truncated { .. })
        ));
    }

    #[test]
    fn rejects_bad_crc() {
        let mut bytes = make_poll(1, 1);
        let n = bytes.len();
        bytes[n - 1] ^= 0xFF;
        assert!(matches!(
            ParsedPacket::parse(&bytes),
            Err(Error::BadCrc { .. })
        ));
    }

    #[test]
    fn rejects_bad_checksum() {
        let mut bytes = PacketBuilder::plain(
            Address::pd(0x01).unwrap(),
            ControlByte::new(Sqn::new(1).unwrap(), CtrlFlags::empty()),
            0x60,
            Vec::new(),
        )
        .encode()
        .unwrap();
        let n = bytes.len();
        bytes[n - 1] ^= 0xFF;
        assert!(matches!(
            ParsedPacket::parse(&bytes),
            Err(Error::BadChecksum { .. })
        ));
    }

    #[test]
    fn parser_total_on_random_garbage() {
        // Phase 1 invariant: parser is total. Should never panic.
        for seed in 0u64..256 {
            let mut x = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15);
            let mut buf = [0u8; 64];
            for b in buf.iter_mut() {
                x = x
                    .wrapping_mul(6364136223846793005)
                    .wrapping_add(1442695040888963407);
                *b = (x >> 33) as u8;
            }
            let _ = ParsedPacket::parse(&buf);
        }
    }
}