dvb-ci 0.5.0

DVB Common Interface (EN 50221) — APDU/resource objects, session/transport PDUs, and a CA_PMT builder from a dvb-si PMT.
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
//! Copy Protection objects — ETSI TS 101 699 V1.1.1 §6.6, Tables 69-73
//! (PDF pp. 62-63). See `docs/ci_plus/copy-protection.md`.
//!
//! Resource ID `0x00041ii1` (`ii` = Module ID). Generic control of a host's
//! copy-protection function; the detailed semantics are CP-system-specific.
//!
//! - `cp_query` (`9F 80 00`, Table 69) — app → host: query status of a CP system.
//! - `cp_reply` (`9F 80 01`, Table 70) — host → app: status reply.
//! - `cp_command` (`9F 80 02`, Table 72) — app → host: opaque command bytes.
//! - `cp_response` (`9F 80 03`, Table 73) — host → app: opaque response bytes.

use crate::error::{Error, Result};
use crate::objects;
use crate::tag::ApduTag;
use dvb_common::{Parse, Serialize};

/// Resource-scoped `apdu_tag`s for Copy Protection (Tables 69-73).
pub mod tag {
    use crate::tag::ApduTag;
    /// `CopyProtectionQueryTag` = `9F 80 00`.
    pub const CP_QUERY: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x00);
    /// `CPReplyTag` = `9F 80 01`.
    pub const CP_REPLY: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x01);
    /// `CPCommandTag` = `9F 80 02`.
    pub const CP_COMMAND: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x02);
    /// `CPResponseTag` = `9F 80 03`.
    pub const CP_RESPONSE: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x03);
}

/// `Status` — copy-protection status (Table 71).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum CpStatus {
    /// `01` — Copy Protection Inactive.
    Inactive,
    /// `02` — Copy Protection Active.
    Active,
    /// `FF` — ID mismatch.
    IdMismatch,
    /// Any other value (reserved).
    Reserved(u8),
}

impl CpStatus {
    /// Decode the `Status` byte.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0x01 => Self::Inactive,
            0x02 => Self::Active,
            0xFF => Self::IdMismatch,
            other => Self::Reserved(other),
        }
    }
    /// Wire byte.
    #[must_use]
    pub const fn to_u8(self) -> u8 {
        match self {
            Self::Inactive => 0x01,
            Self::Active => 0x02,
            Self::IdMismatch => 0xFF,
            Self::Reserved(v) => v,
        }
    }
    /// Spec token, or `"reserved"`.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Inactive => "Copy Protection Inactive",
            Self::Active => "Copy Protection Active",
            Self::IdMismatch => "ID mismatch",
            Self::Reserved(_) => "reserved",
        }
    }
}
dvb_common::impl_spec_display!(CpStatus, Reserved);

/// `cp_query()` (Table 69): app → host.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CpQuery {
    /// 24-bit `CopyProtectionID` (an IEEE `company_id`).
    pub copy_protection_id: u32,
}

/// `cp_reply()` (Table 70): host → app.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CpReply {
    /// 24-bit `CopyProtectionID`.
    pub copy_protection_id: u32,
    /// `Status`.
    pub status: CpStatus,
}

/// `cp_command()` (Table 72): app → host. The `cp_command_byte`s are
/// **opaque, CP-system-specific** bytes carried verbatim.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CpCommand<'a> {
    /// 24-bit `CopyProtectionID`.
    pub copy_protection_id: u32,
    /// Opaque command bytes.
    #[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
    pub command_bytes: &'a [u8],
}

/// `cp_response()` (Table 73): host → app. Identical to [`CpCommand`] except for
/// the tag; the `cp_response_byte`s are opaque CP-system-specific bytes.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CpResponse<'a> {
    /// 24-bit `CopyProtectionID`.
    pub copy_protection_id: u32,
    /// Opaque response bytes.
    #[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
    pub response_bytes: &'a [u8],
}

/// Width of the `CopyProtectionID` field (24 bits).
const CP_ID_LEN: usize = 3;

fn read_cp_id(body: &[u8]) -> u32 {
    ((body[0] as u32) << 16) | ((body[1] as u32) << 8) | body[2] as u32
}

fn write_cp_id(id: u32, buf: &mut [u8]) {
    buf[0] = (id >> 16) as u8;
    buf[1] = (id >> 8) as u8;
    buf[2] = id as u8;
}

// --- cp_query ---

impl<'a> Parse<'a> for CpQuery {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = objects::parse_apdu_header(bytes, tag::CP_QUERY, "cp_query")?;
        if body.len() < CP_ID_LEN {
            return Err(Error::BufferTooShort {
                need: CP_ID_LEN,
                have: body.len(),
                what: "cp_query",
            });
        }
        Ok(Self {
            copy_protection_id: read_cp_id(body),
        })
    }
}
impl Serialize for CpQuery {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        objects::apdu_len(CP_ID_LEN)
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let pos = objects::write_apdu_header(tag::CP_QUERY, CP_ID_LEN, buf)?;
        write_cp_id(self.copy_protection_id, &mut buf[pos..]);
        Ok(pos + CP_ID_LEN)
    }
}

// --- cp_reply ---

// CopyProtectionID(3) + Status(1).
const CP_REPLY_BODY: usize = CP_ID_LEN + 1;

impl<'a> Parse<'a> for CpReply {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = objects::parse_apdu_header(bytes, tag::CP_REPLY, "cp_reply")?;
        if body.len() < CP_REPLY_BODY {
            return Err(Error::BufferTooShort {
                need: CP_REPLY_BODY,
                have: body.len(),
                what: "cp_reply",
            });
        }
        Ok(Self {
            copy_protection_id: read_cp_id(body),
            status: CpStatus::from_u8(body[CP_ID_LEN]),
        })
    }
}
impl Serialize for CpReply {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        objects::apdu_len(CP_REPLY_BODY)
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let pos = objects::write_apdu_header(tag::CP_REPLY, CP_REPLY_BODY, buf)?;
        write_cp_id(self.copy_protection_id, &mut buf[pos..]);
        buf[pos + CP_ID_LEN] = self.status.to_u8();
        Ok(pos + CP_REPLY_BODY)
    }
}

// --- cp_command ---

impl<'a> Parse<'a> for CpCommand<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = objects::parse_apdu_header(bytes, tag::CP_COMMAND, "cp_command")?;
        if body.len() < CP_ID_LEN {
            return Err(Error::BufferTooShort {
                need: CP_ID_LEN,
                have: body.len(),
                what: "cp_command",
            });
        }
        Ok(Self {
            copy_protection_id: read_cp_id(body),
            command_bytes: &body[CP_ID_LEN..],
        })
    }
}
impl Serialize for CpCommand<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        objects::apdu_len(CP_ID_LEN + self.command_bytes.len())
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = CP_ID_LEN + self.command_bytes.len();
        let mut pos = objects::write_apdu_header(tag::CP_COMMAND, body_len, buf)?;
        write_cp_id(self.copy_protection_id, &mut buf[pos..]);
        pos += CP_ID_LEN;
        buf[pos..pos + self.command_bytes.len()].copy_from_slice(self.command_bytes);
        Ok(pos + self.command_bytes.len())
    }
}

// --- cp_response ---

impl<'a> Parse<'a> for CpResponse<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = objects::parse_apdu_header(bytes, tag::CP_RESPONSE, "cp_response")?;
        if body.len() < CP_ID_LEN {
            return Err(Error::BufferTooShort {
                need: CP_ID_LEN,
                have: body.len(),
                what: "cp_response",
            });
        }
        Ok(Self {
            copy_protection_id: read_cp_id(body),
            response_bytes: &body[CP_ID_LEN..],
        })
    }
}
impl Serialize for CpResponse<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        objects::apdu_len(CP_ID_LEN + self.response_bytes.len())
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = CP_ID_LEN + self.response_bytes.len();
        let mut pos = objects::write_apdu_header(tag::CP_RESPONSE, body_len, buf)?;
        write_cp_id(self.copy_protection_id, &mut buf[pos..]);
        pos += CP_ID_LEN;
        buf[pos..pos + self.response_bytes.len()].copy_from_slice(self.response_bytes);
        Ok(pos + self.response_bytes.len())
    }
}

/// Resource-scoped dispatch over the Copy Protection objects.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum CopyProtectionApdu<'a> {
    /// `cp_query` (`9F 80 00`).
    CpQuery(CpQuery),
    /// `cp_reply` (`9F 80 01`).
    CpReply(CpReply),
    /// `cp_command` (`9F 80 02`).
    CpCommand(CpCommand<'a>),
    /// `cp_response` (`9F 80 03`).
    CpResponse(CpResponse<'a>),
}

impl<'a> CopyProtectionApdu<'a> {
    /// Parse a Copy Protection APDU, dispatching on the `apdu_tag`.
    pub fn parse(body: &'a [u8]) -> Result<Self> {
        if body.len() < 3 {
            return Err(Error::BufferTooShort {
                need: 3,
                have: body.len(),
                what: "copy_protection apdu_tag",
            });
        }
        let t = ApduTag::from_bytes(body[0], body[1], body[2]);
        match t {
            tag::CP_QUERY => Ok(Self::CpQuery(CpQuery::parse(body)?)),
            tag::CP_REPLY => Ok(Self::CpReply(CpReply::parse(body)?)),
            tag::CP_COMMAND => Ok(Self::CpCommand(CpCommand::parse(body)?)),
            tag::CP_RESPONSE => Ok(Self::CpResponse(CpResponse::parse(body)?)),
            _ => Err(Error::UnexpectedApduTag {
                got: t.as_u24(),
                expected: tag::CP_QUERY.as_u24(),
                what: "copy_protection",
            }),
        }
    }
}

impl Serialize for CopyProtectionApdu<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        match self {
            Self::CpQuery(o) => o.serialized_len(),
            Self::CpReply(o) => o.serialized_len(),
            Self::CpCommand(o) => o.serialized_len(),
            Self::CpResponse(o) => o.serialized_len(),
        }
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        match self {
            Self::CpQuery(o) => o.serialize_into(buf),
            Self::CpReply(o) => o.serialize_into(buf),
            Self::CpCommand(o) => o.serialize_into(buf),
            Self::CpResponse(o) => o.serialize_into(buf),
        }
    }
}

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

    #[test]
    fn cp_query_round_trips_and_bites() {
        let q = CpQuery {
            copy_protection_id: 0xAABBCC,
        };
        let bytes = q.to_bytes();
        assert_eq!(bytes, [0x9F, 0x80, 0x00, 0x03, 0xAA, 0xBB, 0xCC]);
        assert_eq!(CpQuery::parse(&bytes).unwrap(), q);
        let other = CpQuery {
            copy_protection_id: 0xAABBCD,
        };
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn cp_reply_round_trips_and_bites() {
        let r = CpReply {
            copy_protection_id: 0x010203,
            status: CpStatus::Active,
        };
        let bytes = r.to_bytes();
        assert_eq!(bytes, [0x9F, 0x80, 0x01, 0x04, 0x01, 0x02, 0x03, 0x02]);
        assert_eq!(CpReply::parse(&bytes).unwrap(), r);
        assert_eq!(r.status.name(), "Copy Protection Active");
        let mut other = r;
        other.status = CpStatus::IdMismatch;
        assert_ne!(bytes, other.to_bytes());
        assert_eq!(other.to_bytes()[7], 0xFF);
    }

    #[test]
    fn cp_command_round_trips_and_bites() {
        let c = CpCommand {
            copy_protection_id: 0x112233,
            command_bytes: &[0xDE, 0xAD, 0xBE, 0xEF],
        };
        let bytes = c.to_bytes();
        // tag(3) + len(1) + id(3) + 4 = 11; body len = 7 = 0x07.
        assert_eq!(
            bytes,
            [0x9F, 0x80, 0x02, 0x07, 0x11, 0x22, 0x33, 0xDE, 0xAD, 0xBE, 0xEF]
        );
        assert_eq!(CpCommand::parse(&bytes).unwrap(), c);
        let other = CpCommand {
            copy_protection_id: 0x112233,
            command_bytes: &[0xDE, 0xAD, 0xBE, 0x00],
        };
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn cp_response_round_trips() {
        let r = CpResponse {
            copy_protection_id: 0x445566,
            response_bytes: &[0x01, 0x02],
        };
        let bytes = r.to_bytes();
        assert_eq!(
            bytes,
            [0x9F, 0x80, 0x03, 0x05, 0x44, 0x55, 0x66, 0x01, 0x02]
        );
        assert_eq!(CpResponse::parse(&bytes).unwrap(), r);
    }

    #[test]
    fn dispatch_routes_each_tag() {
        let q = CpQuery {
            copy_protection_id: 0,
        }
        .to_bytes();
        assert!(matches!(
            CopyProtectionApdu::parse(&q).unwrap(),
            CopyProtectionApdu::CpQuery(_)
        ));
        let resp = CpResponse {
            copy_protection_id: 0x1,
            response_bytes: &[0xFF],
        }
        .to_bytes();
        let parsed = CopyProtectionApdu::parse(&resp).unwrap();
        assert!(matches!(parsed, CopyProtectionApdu::CpResponse(_)));
        assert_eq!(parsed.to_bytes(), resp);
    }
}