use crate::error::{Error, Result};
use crate::objects;
use broadcast_core::len::{need_in, need_out};
use broadcast_core::{Parse, Serialize};
pub mod tag {
use crate::tag::ApduTag;
pub const DOWNLOAD_ENQ: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x00);
pub const DOWNLOAD_REPLY: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x01);
pub const USER_AUTH_INITIATE: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x02);
pub const USER_AUTH_RESULT: ApduTag = ApduTag::from_bytes(0x9F, 0x80, 0x03);
}
pub const BINARY_ID_LEN: usize = 7;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct BinaryId {
pub specifier: u32,
pub model: u16,
pub version: u16,
}
const BINARY_ID_SPECIFIER_BITS: u32 = 24;
const BINARY_ID_SPECIFIER_LEN: usize = 3;
const BINARY_ID_SPECIFIER_OFFSET: usize = 0;
const BINARY_ID_MODEL_OFFSET: usize = BINARY_ID_SPECIFIER_LEN;
const BINARY_ID_VERSION_OFFSET: usize = BINARY_ID_MODEL_OFFSET + 2;
const DSMCC_HEADER_LEN: usize = 12;
const PROTOCOL_DISCRIMINATOR_OFFSET: usize = 0;
const DSMCC_TYPE_OFFSET: usize = 1;
const MESSAGE_ID_OFFSET: usize = 2;
const TRANSACTION_ID_OFFSET: usize = 4;
const HEADER_RESERVED_OFFSET: usize = 8;
const ADAPTATION_LENGTH_OFFSET: usize = 9;
const MESSAGE_LENGTH_OFFSET: usize = 10;
const DSMCC_RESERVED_BYTE: u8 = 0xFF;
const ADAPTATION_LENGTH_BITS: u32 = 8;
const MESSAGE_LENGTH_BITS: u32 = 16;
const INFO_REQUEST_FIXED: usize = core::mem::size_of::<u32>() + core::mem::size_of::<u16>();
const INFO_RESPONSE_DOWNLOAD_ID_OFFSET: usize = 0;
const INFO_RESPONSE_BLOCK_SIZE_OFFSET: usize = 4;
const INFO_RESPONSE_WINDOW_SIZE_OFFSET: usize = 6;
const INFO_RESPONSE_ACK_PERIOD_OFFSET: usize = 7;
const INFO_RESPONSE_TC_WINDOW_OFFSET: usize = 8;
const INFO_RESPONSE_TC_SCENARIO_OFFSET: usize = 12;
const INFO_RESPONSE_FIXED: usize = 4 + 2 + 1 + 1 + 4 + 4;
const CANCEL_DOWNLOAD_ID_OFFSET: usize = 0;
const CANCEL_MODULE_ID_OFFSET: usize = 4;
const CANCEL_BLOCK_NUMBER_OFFSET: usize = 6;
const CANCEL_REASON_OFFSET: usize = 8;
const CANCEL_FIXED: usize = 4 + 2 + 2 + 1;
const DATA_REQUEST_REASON_OFFSET: usize = 4;
const DATA_REQUEST_FIXED: usize = 5;
const DATA_BLOCK_VERSION_OFFSET: usize = 2;
const DATA_BLOCK_BLOCK_NUMBER_OFFSET: usize = 4;
const DATA_BLOCK_FIXED: usize = 6;
const MODULE_INFO_FIXED: usize = 8;
const BLOCK_LENGTH_LEN: usize = core::mem::size_of::<u16>();
impl BinaryId {
fn read(b: &[u8]) -> Result<Self> {
Ok(Self {
specifier: (u32::from(b[BINARY_ID_SPECIFIER_OFFSET]) << MESSAGE_LENGTH_BITS)
| (u32::from(b[BINARY_ID_SPECIFIER_OFFSET + 1]) << ADAPTATION_LENGTH_BITS)
| u32::from(b[BINARY_ID_SPECIFIER_OFFSET + BINARY_ID_SPECIFIER_LEN - 1]),
model: broadcast_core::wire::Reader::at(b, BINARY_ID_MODEL_OFFSET).u16("model")?,
version: broadcast_core::wire::Reader::at(b, BINARY_ID_VERSION_OFFSET)
.u16("version")?,
})
}
fn write(self, buf: &mut [u8]) -> Result<()> {
objects::fit_bits(
u64::from(self.specifier),
BINARY_ID_SPECIFIER_BITS,
"binary_id specifier",
)?;
let be = self.specifier.to_be_bytes();
buf[..BINARY_ID_SPECIFIER_LEN].copy_from_slice(&be[1..]);
buf[BINARY_ID_MODEL_OFFSET..BINARY_ID_VERSION_OFFSET]
.copy_from_slice(&self.model.to_be_bytes());
buf[BINARY_ID_VERSION_OFFSET..BINARY_ID_VERSION_OFFSET + core::mem::size_of::<u16>()]
.copy_from_slice(&self.version.to_be_bytes());
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadEnquiry<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub dsmcc_message: &'a [u8],
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadReply<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub dsmcc_message: &'a [u8],
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct UserAuthInitiate<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub binary_id: BinaryId,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub data: &'a [u8],
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct UserAuthResult<'a> {
#[cfg_attr(feature = "serde", serde(skip))]
pub length_form: crate::length::LengthForm,
pub binary_id: BinaryId,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub result: &'a [u8],
}
crate::dispatch::declare_opaque_apdu!(
DownloadEnquiry,
dsmcc_message,
tag::DOWNLOAD_ENQ,
"download_enq"
);
crate::dispatch::declare_opaque_apdu!(
DownloadReply,
dsmcc_message,
tag::DOWNLOAD_REPLY,
"download_reply"
);
macro_rules! user_auth_object {
($ty:ident, $tag:expr, $what:literal, $field:ident) => {
impl<'a> Parse<'a> for $ty<'a> {
type Error = Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (length_form, body) = objects::parse_apdu_header(bytes, $tag, $what)?;
need_in(body, BINARY_ID_LEN, $what)?;
Ok(Self {
length_form,
binary_id: BinaryId::read(body)?,
$field: &body[BINARY_ID_LEN..],
})
}
}
impl Serialize for $ty<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
objects::apdu_len(BINARY_ID_LEN + self.$field.len(), self.length_form)
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let body_len = BINARY_ID_LEN + self.$field.len();
let mut pos = objects::write_apdu_header($tag, body_len, self.length_form, buf)?;
self.binary_id.write(&mut buf[pos..])?;
pos += BINARY_ID_LEN;
buf[pos..pos + self.$field.len()].copy_from_slice(self.$field);
Ok(pos + self.$field.len())
}
}
};
}
user_auth_object!(
UserAuthInitiate,
tag::USER_AUTH_INITIATE,
"user_authorization_initiate",
data
);
user_auth_object!(
UserAuthResult,
tag::USER_AUTH_RESULT,
"user_authorization_result",
result
);
pub const DSMCC_PROTOCOL_DISCRIMINATOR: u8 = 0x11;
pub const DSMCC_TYPE_DOWNLOAD: u8 = 0x03;
pub const MSG_ID_DOWNLOAD_INFO_REQUEST: u16 = 0x1001;
pub const MSG_ID_DOWNLOAD_INFO_RESPONSE: u16 = 0x1002;
pub const MSG_ID_DOWNLOAD_DATA_BLOCK: u16 = 0x1003;
pub const MSG_ID_DOWNLOAD_DATA_REQUEST: u16 = 0x1004;
pub const MSG_ID_DOWNLOAD_CANCEL: u16 = 0x1005;
fn parse_dsmcc_header<'a>(
body: &'a [u8],
expected_message_id: u16,
what: &'static str,
) -> Result<(u32, &'a [u8], &'a [u8])> {
const HDR: usize = DSMCC_HEADER_LEN;
if body.len() < HDR {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(HDR, body.len(), what),
));
}
if body[PROTOCOL_DISCRIMINATOR_OFFSET] != DSMCC_PROTOCOL_DISCRIMINATOR {
return Err(Error::InvalidObject {
what,
reason: "protocolDiscriminator is not MPEG-2 DSM-CC (0x11)",
});
}
if body[DSMCC_TYPE_OFFSET] != DSMCC_TYPE_DOWNLOAD {
return Err(Error::InvalidObject {
what,
reason: "dsmccType is not U-N Download (0x03)",
});
}
let message_id = broadcast_core::wire::Reader::at(body, MESSAGE_ID_OFFSET).u16("message_id")?;
if message_id != expected_message_id {
return Err(Error::InvalidObject {
what,
reason: "messageId does not match this message type",
});
}
let id = u32::from_be_bytes([
body[TRANSACTION_ID_OFFSET],
body[TRANSACTION_ID_OFFSET + 1],
body[TRANSACTION_ID_OFFSET + core::mem::size_of::<u16>()],
body[TRANSACTION_ID_OFFSET + core::mem::size_of::<u16>() + 1],
]);
let adaptation_length = body[ADAPTATION_LENGTH_OFFSET] as usize;
if body.len() < HDR + adaptation_length {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(HDR + adaptation_length, body.len(), what),
));
}
let declared_message_length = broadcast_core::wire::Reader::at(body, MESSAGE_LENGTH_OFFSET)
.u16("message_length")? as usize;
let actual_message_length = body.len() - HDR;
if declared_message_length != actual_message_length {
return Err(Error::LengthMismatch(
broadcast_core::error::LengthMismatch::new(
what,
declared_message_length,
actual_message_length,
),
));
}
let adaptation = &body[HDR..HDR + adaptation_length];
let rest = &body[HDR + adaptation_length..];
Ok((id, adaptation, rest))
}
fn write_dsmcc_header(
buf: &mut [u8],
message_id: u16,
id: u32,
adaptation: &[u8],
message_length: usize,
) -> Result<usize> {
objects::fit_bits(
adaptation.len() as u64,
ADAPTATION_LENGTH_BITS,
"DSM-CC adaptationLength",
)?;
objects::fit_bits(
message_length as u64,
MESSAGE_LENGTH_BITS,
"DSM-CC messageLength",
)?;
buf[PROTOCOL_DISCRIMINATOR_OFFSET] = DSMCC_PROTOCOL_DISCRIMINATOR;
buf[DSMCC_TYPE_OFFSET] = DSMCC_TYPE_DOWNLOAD;
buf[MESSAGE_ID_OFFSET..TRANSACTION_ID_OFFSET].copy_from_slice(&message_id.to_be_bytes());
buf[TRANSACTION_ID_OFFSET..HEADER_RESERVED_OFFSET].copy_from_slice(&id.to_be_bytes());
buf[HEADER_RESERVED_OFFSET] = DSMCC_RESERVED_BYTE; buf[ADAPTATION_LENGTH_OFFSET] = adaptation.len() as u8;
buf[MESSAGE_LENGTH_OFFSET..DSMCC_HEADER_LEN]
.copy_from_slice(&(message_length as u16).to_be_bytes());
buf[DSMCC_HEADER_LEN..DSMCC_HEADER_LEN + adaptation.len()].copy_from_slice(adaptation);
Ok(DSMCC_HEADER_LEN + adaptation.len())
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadInfoRequest<'a> {
pub transaction_id: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub adaptation: &'a [u8],
pub buffer_size: u32,
pub maximum_block_size: u16,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub compatibility_descriptor: &'a [u8],
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub private_data: &'a [u8],
}
impl<'a> Parse<'a> for DownloadInfoRequest<'a> {
type Error = Error;
fn parse(body: &'a [u8]) -> Result<Self> {
let what = "DownloadInfoRequest";
let (transaction_id, adaptation, rest) =
parse_dsmcc_header(body, MSG_ID_DOWNLOAD_INFO_REQUEST, what)?;
if rest.len() < INFO_REQUEST_FIXED + BLOCK_LENGTH_LEN {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
INFO_REQUEST_FIXED + BLOCK_LENGTH_LEN,
rest.len(),
what,
),
));
}
let buffer_size = u32::from_be_bytes([
rest[0],
rest[1],
rest[core::mem::size_of::<u16>()],
rest[core::mem::size_of::<u16>() + 1],
]);
let maximum_block_size =
broadcast_core::wire::Reader::at(rest, core::mem::size_of::<u32>())
.u16("maximum_block_size")?;
let compat_len =
broadcast_core::wire::Reader::at(rest, INFO_REQUEST_FIXED).u16("compat_len")? as usize;
let compat_start = INFO_REQUEST_FIXED;
let compat_block_end = compat_start + BLOCK_LENGTH_LEN + compat_len;
if rest.len() < compat_block_end + BLOCK_LENGTH_LEN {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
compat_block_end + BLOCK_LENGTH_LEN,
rest.len(),
what,
),
));
}
let compatibility_descriptor = &rest[compat_start..compat_block_end];
let priv_len =
broadcast_core::wire::Reader::at(rest, compat_block_end).u16("priv_len")? as usize;
let priv_start = compat_block_end + BLOCK_LENGTH_LEN;
let priv_end = priv_start + priv_len;
if rest.len() < priv_end {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(priv_end, rest.len(), what),
));
}
Ok(Self {
transaction_id,
adaptation,
buffer_size,
maximum_block_size,
compatibility_descriptor,
private_data: &rest[priv_start..priv_end],
})
}
}
impl Serialize for DownloadInfoRequest<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
DSMCC_HEADER_LEN
+ self.adaptation.len()
+ INFO_REQUEST_FIXED
+ self.compatibility_descriptor.len()
+ BLOCK_LENGTH_LEN
+ self.private_data.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let total = self.serialized_len();
need_out(buf, total, "DownloadInfoRequest")?;
let message_length = total - DSMCC_HEADER_LEN;
objects::fit_bits(
self.private_data.len() as u64,
MESSAGE_LENGTH_BITS,
"DownloadInfoRequest privateDataLength",
)?;
let mut pos = write_dsmcc_header(
buf,
MSG_ID_DOWNLOAD_INFO_REQUEST,
self.transaction_id,
self.adaptation,
message_length,
)?;
buf[pos..pos + core::mem::size_of::<u32>()]
.copy_from_slice(&self.buffer_size.to_be_bytes());
pos += core::mem::size_of::<u32>();
buf[pos..pos + core::mem::size_of::<u16>()]
.copy_from_slice(&self.maximum_block_size.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos..pos + self.compatibility_descriptor.len()]
.copy_from_slice(self.compatibility_descriptor);
pos += self.compatibility_descriptor.len();
buf[pos..pos + BLOCK_LENGTH_LEN]
.copy_from_slice(&(self.private_data.len() as u16).to_be_bytes());
pos += BLOCK_LENGTH_LEN;
buf[pos..pos + self.private_data.len()].copy_from_slice(self.private_data);
Ok(pos + self.private_data.len())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadInfoResponse<'a> {
pub transaction_id: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub adaptation: &'a [u8],
pub download_id: u32,
pub block_size: u16,
pub window_size: u8,
pub ack_period: u8,
pub tc_download_window: u32,
pub tc_download_scenario: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub compatibility_descriptor: &'a [u8],
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub modules: &'a [u8],
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub private_data: &'a [u8],
}
impl<'a> Parse<'a> for DownloadInfoResponse<'a> {
type Error = Error;
fn parse(body: &'a [u8]) -> Result<Self> {
let what = "DownloadInfoResponse";
let (transaction_id, adaptation, rest) =
parse_dsmcc_header(body, MSG_ID_DOWNLOAD_INFO_RESPONSE, what)?;
const FIXED: usize = INFO_RESPONSE_FIXED + BLOCK_LENGTH_LEN;
if rest.len() < FIXED {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(FIXED, rest.len(), what),
));
}
let download_id = u32::from_be_bytes([
rest[INFO_RESPONSE_DOWNLOAD_ID_OFFSET],
rest[INFO_RESPONSE_DOWNLOAD_ID_OFFSET + 1],
rest[INFO_RESPONSE_DOWNLOAD_ID_OFFSET + core::mem::size_of::<u16>()],
rest[INFO_RESPONSE_DOWNLOAD_ID_OFFSET + core::mem::size_of::<u16>() + 1],
]);
let block_size = broadcast_core::wire::Reader::at(rest, INFO_RESPONSE_BLOCK_SIZE_OFFSET)
.u16("block_size")?;
let window_size = rest[INFO_RESPONSE_WINDOW_SIZE_OFFSET];
let ack_period = rest[INFO_RESPONSE_ACK_PERIOD_OFFSET];
let tc_download_window = u32::from_be_bytes([
rest[INFO_RESPONSE_TC_WINDOW_OFFSET],
rest[INFO_RESPONSE_TC_WINDOW_OFFSET + 1],
rest[INFO_RESPONSE_TC_WINDOW_OFFSET + core::mem::size_of::<u16>()],
rest[INFO_RESPONSE_TC_WINDOW_OFFSET + core::mem::size_of::<u16>() + 1],
]);
let tc_download_scenario = u32::from_be_bytes([
rest[INFO_RESPONSE_TC_SCENARIO_OFFSET],
rest[INFO_RESPONSE_TC_SCENARIO_OFFSET + 1],
rest[INFO_RESPONSE_TC_SCENARIO_OFFSET + core::mem::size_of::<u16>()],
rest[INFO_RESPONSE_TC_SCENARIO_OFFSET + core::mem::size_of::<u16>() + 1],
]);
let compat_len_off = INFO_RESPONSE_FIXED;
let compat_len =
broadcast_core::wire::Reader::at(rest, compat_len_off).u16("compat_len")? as usize;
let compat_block_end = compat_len_off + BLOCK_LENGTH_LEN + compat_len;
if rest.len() < compat_block_end + BLOCK_LENGTH_LEN {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
compat_block_end + BLOCK_LENGTH_LEN,
rest.len(),
what,
),
));
}
let compatibility_descriptor = &rest[compat_len_off..compat_block_end];
let number_of_modules = broadcast_core::wire::Reader::at(rest, compat_block_end)
.u16("number_of_modules")? as usize;
let mut mpos = compat_block_end + BLOCK_LENGTH_LEN;
for _ in 0..number_of_modules {
if rest.len() < mpos + MODULE_INFO_FIXED {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
mpos + MODULE_INFO_FIXED,
rest.len(),
what,
),
));
}
let module_info_len = rest[mpos + MODULE_INFO_FIXED - 1] as usize;
mpos += MODULE_INFO_FIXED + module_info_len;
if rest.len() < mpos {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(mpos, rest.len(), what),
));
}
}
let modules = &rest[compat_block_end..mpos];
if rest.len() < mpos + BLOCK_LENGTH_LEN {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(
mpos + BLOCK_LENGTH_LEN,
rest.len(),
what,
),
));
}
let priv_len = broadcast_core::wire::Reader::at(rest, mpos).u16("priv_len")? as usize;
let priv_start = mpos + BLOCK_LENGTH_LEN;
let priv_end = priv_start + priv_len;
if rest.len() < priv_end {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(priv_end, rest.len(), what),
));
}
Ok(Self {
transaction_id,
adaptation,
download_id,
block_size,
window_size,
ack_period,
tc_download_window,
tc_download_scenario,
compatibility_descriptor,
modules,
private_data: &rest[priv_start..priv_end],
})
}
}
impl Serialize for DownloadInfoResponse<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
DSMCC_HEADER_LEN
+ self.adaptation.len()
+ INFO_RESPONSE_FIXED
+ self.compatibility_descriptor.len()
+ self.modules.len()
+ BLOCK_LENGTH_LEN
+ self.private_data.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let total = self.serialized_len();
need_out(buf, total, "DownloadInfoResponse")?;
let message_length = total - DSMCC_HEADER_LEN;
objects::fit_bits(
self.private_data.len() as u64,
MESSAGE_LENGTH_BITS,
"DownloadInfoResponse privateDataLength",
)?;
let mut pos = write_dsmcc_header(
buf,
MSG_ID_DOWNLOAD_INFO_RESPONSE,
self.transaction_id,
self.adaptation,
message_length,
)?;
buf[pos..pos + core::mem::size_of::<u32>()]
.copy_from_slice(&self.download_id.to_be_bytes());
pos += core::mem::size_of::<u32>();
buf[pos..pos + core::mem::size_of::<u16>()].copy_from_slice(&self.block_size.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos] = self.window_size;
buf[pos + 1] = self.ack_period;
pos += core::mem::size_of::<u16>();
buf[pos..pos + core::mem::size_of::<u32>()]
.copy_from_slice(&self.tc_download_window.to_be_bytes());
pos += core::mem::size_of::<u32>();
buf[pos..pos + core::mem::size_of::<u32>()]
.copy_from_slice(&self.tc_download_scenario.to_be_bytes());
pos += core::mem::size_of::<u32>();
buf[pos..pos + self.compatibility_descriptor.len()]
.copy_from_slice(self.compatibility_descriptor);
pos += self.compatibility_descriptor.len();
buf[pos..pos + self.modules.len()].copy_from_slice(self.modules);
pos += self.modules.len();
buf[pos..pos + BLOCK_LENGTH_LEN]
.copy_from_slice(&(self.private_data.len() as u16).to_be_bytes());
pos += BLOCK_LENGTH_LEN;
buf[pos..pos + self.private_data.len()].copy_from_slice(self.private_data);
Ok(pos + self.private_data.len())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadCancel<'a> {
pub transaction_id: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub adaptation: &'a [u8],
pub download_id: u32,
pub module_id: u16,
pub block_number: u16,
pub download_cancel_reason: u8,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub private_data: &'a [u8],
}
impl<'a> Parse<'a> for DownloadCancel<'a> {
type Error = Error;
fn parse(body: &'a [u8]) -> Result<Self> {
let what = "DownloadCancel";
let (transaction_id, adaptation, rest) =
parse_dsmcc_header(body, MSG_ID_DOWNLOAD_CANCEL, what)?;
const FIXED: usize = CANCEL_FIXED + BLOCK_LENGTH_LEN;
if rest.len() < FIXED {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(FIXED, rest.len(), what),
));
}
let download_id = u32::from_be_bytes([
rest[CANCEL_DOWNLOAD_ID_OFFSET],
rest[CANCEL_DOWNLOAD_ID_OFFSET + 1],
rest[CANCEL_DOWNLOAD_ID_OFFSET + core::mem::size_of::<u16>()],
rest[CANCEL_DOWNLOAD_ID_OFFSET + core::mem::size_of::<u16>() + 1],
]);
let module_id =
broadcast_core::wire::Reader::at(rest, CANCEL_MODULE_ID_OFFSET).u16("module_id")?;
let block_number = broadcast_core::wire::Reader::at(rest, CANCEL_BLOCK_NUMBER_OFFSET)
.u16("block_number")?;
let download_cancel_reason = rest[CANCEL_REASON_OFFSET];
let priv_len =
broadcast_core::wire::Reader::at(rest, CANCEL_FIXED).u16("priv_len")? as usize;
let priv_start = CANCEL_FIXED + BLOCK_LENGTH_LEN;
let priv_end = priv_start + priv_len;
if rest.len() < priv_end {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(priv_end, rest.len(), what),
));
}
Ok(Self {
transaction_id,
adaptation,
download_id,
module_id,
block_number,
download_cancel_reason,
private_data: &rest[priv_start..priv_end],
})
}
}
impl Serialize for DownloadCancel<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
DSMCC_HEADER_LEN
+ self.adaptation.len()
+ CANCEL_FIXED
+ BLOCK_LENGTH_LEN
+ self.private_data.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let total = self.serialized_len();
need_out(buf, total, "DownloadCancel")?;
let message_length = total - DSMCC_HEADER_LEN;
objects::fit_bits(
self.private_data.len() as u64,
MESSAGE_LENGTH_BITS,
"DownloadCancel privateDataLength",
)?;
let mut pos = write_dsmcc_header(
buf,
MSG_ID_DOWNLOAD_CANCEL,
self.transaction_id,
self.adaptation,
message_length,
)?;
buf[pos..pos + core::mem::size_of::<u32>()]
.copy_from_slice(&self.download_id.to_be_bytes());
pos += core::mem::size_of::<u32>();
buf[pos..pos + core::mem::size_of::<u16>()].copy_from_slice(&self.module_id.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos..pos + core::mem::size_of::<u16>()]
.copy_from_slice(&self.block_number.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos] = self.download_cancel_reason;
pos += 1;
buf[pos..pos + BLOCK_LENGTH_LEN]
.copy_from_slice(&(self.private_data.len() as u16).to_be_bytes());
pos += BLOCK_LENGTH_LEN;
buf[pos..pos + self.private_data.len()].copy_from_slice(self.private_data);
Ok(pos + self.private_data.len())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadDataRequest<'a> {
pub download_id: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub adaptation: &'a [u8],
pub module_id: u16,
pub block_number: u16,
pub download_reason: u8,
}
impl<'a> Parse<'a> for DownloadDataRequest<'a> {
type Error = Error;
fn parse(body: &'a [u8]) -> Result<Self> {
let what = "DownloadDataRequest";
let (download_id, adaptation, rest) =
parse_dsmcc_header(body, MSG_ID_DOWNLOAD_DATA_REQUEST, what)?;
const FIXED: usize = 5;
if rest.len() < FIXED {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(FIXED, rest.len(), what),
));
}
Ok(Self {
download_id,
adaptation,
module_id: broadcast_core::wire::Reader::at(rest, 0).u16("module_id")?,
block_number: broadcast_core::wire::Reader::at(rest, core::mem::size_of::<u16>())
.u16("block_number")?,
download_reason: rest[DATA_REQUEST_REASON_OFFSET],
})
}
}
impl Serialize for DownloadDataRequest<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
DSMCC_HEADER_LEN + self.adaptation.len() + DATA_REQUEST_FIXED
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let total = self.serialized_len();
need_out(buf, total, "DownloadDataRequest")?;
let message_length = total - DSMCC_HEADER_LEN;
let mut pos = write_dsmcc_header(
buf,
MSG_ID_DOWNLOAD_DATA_REQUEST,
self.download_id,
self.adaptation,
message_length,
)?;
buf[pos..pos + core::mem::size_of::<u16>()].copy_from_slice(&self.module_id.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos..pos + core::mem::size_of::<u16>()]
.copy_from_slice(&self.block_number.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos] = self.download_reason;
Ok(pos + 1)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct DownloadDataBlock<'a> {
pub download_id: u32,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub adaptation: &'a [u8],
pub module_id: u16,
pub module_version: u8,
pub block_number: u16,
#[cfg_attr(feature = "serde", serde(borrow, with = "crate::objects::bytes_serde"))]
pub block_data: &'a [u8],
}
impl<'a> Parse<'a> for DownloadDataBlock<'a> {
type Error = Error;
fn parse(body: &'a [u8]) -> Result<Self> {
let what = "DownloadDataBlock";
let (download_id, adaptation, rest) =
parse_dsmcc_header(body, MSG_ID_DOWNLOAD_DATA_BLOCK, what)?;
const FIXED: usize = 6;
if rest.len() < FIXED {
return Err(Error::BufferTooShort(
broadcast_core::error::BufferTooShort::new(FIXED, rest.len(), what),
));
}
Ok(Self {
download_id,
adaptation,
module_id: broadcast_core::wire::Reader::at(rest, 0).u16("module_id")?,
module_version: rest[DATA_BLOCK_VERSION_OFFSET],
block_number: broadcast_core::wire::Reader::at(rest, DATA_BLOCK_BLOCK_NUMBER_OFFSET)
.u16("block_number")?,
block_data: &rest[FIXED..],
})
}
}
impl Serialize for DownloadDataBlock<'_> {
type Error = Error;
fn serialized_len(&self) -> usize {
DSMCC_HEADER_LEN + self.adaptation.len() + DATA_BLOCK_FIXED + self.block_data.len()
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
let total = self.serialized_len();
need_out(buf, total, "DownloadDataBlock")?;
let message_length = total - DSMCC_HEADER_LEN;
let mut pos = write_dsmcc_header(
buf,
MSG_ID_DOWNLOAD_DATA_BLOCK,
self.download_id,
self.adaptation,
message_length,
)?;
buf[pos..pos + core::mem::size_of::<u16>()].copy_from_slice(&self.module_id.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos] = self.module_version;
buf[pos + 1] = DSMCC_RESERVED_BYTE; pos += core::mem::size_of::<u16>();
buf[pos..pos + core::mem::size_of::<u16>()]
.copy_from_slice(&self.block_number.to_be_bytes());
pos += core::mem::size_of::<u16>();
buf[pos..pos + self.block_data.len()].copy_from_slice(self.block_data);
Ok(pos + self.block_data.len())
}
}
crate::dispatch::declare_resource_apdus! {
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum DownloadApdu<'a> ("download") {
DownloadEnquiry(DownloadEnquiry<'a>) = tag::DOWNLOAD_ENQ,
DownloadReply(DownloadReply<'a>) = tag::DOWNLOAD_REPLY,
UserAuthInitiate(UserAuthInitiate<'a>) = tag::USER_AUTH_INITIATE,
UserAuthResult(UserAuthResult<'a>) = tag::USER_AUTH_RESULT,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn download_enquiry_round_trips_and_bites() {
let enq = DownloadEnquiry {
length_form: crate::length::LengthForm::Minimal,
dsmcc_message: &[0x11, 0x03, 0x10, 0x01],
};
let bytes = enq.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x80, 0x00, 0x04, 0x11, 0x03, 0x10, 0x01]);
assert_eq!(DownloadEnquiry::parse(&bytes).unwrap(), enq);
let other = DownloadEnquiry {
length_form: crate::length::LengthForm::Minimal,
dsmcc_message: &[0x11, 0x03, 0x10, 0x02],
};
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn download_reply_round_trips() {
let rep = DownloadReply {
length_form: crate::length::LengthForm::Minimal,
dsmcc_message: &[0xAA],
};
let bytes = rep.try_to_bytes().unwrap();
assert_eq!(bytes, [0x9F, 0x80, 0x01, 0x01, 0xAA]);
assert_eq!(DownloadReply::parse(&bytes).unwrap(), rep);
}
#[test]
fn binary_id_specifier_over_24_bits_is_rejected_not_truncated() {
let uai = UserAuthInitiate {
length_form: crate::length::LengthForm::Minimal,
binary_id: BinaryId {
specifier: 0x01_00_1B_67, model: 0,
version: 0,
},
data: &[],
};
let mut buf = [0u8; 32];
assert!(matches!(
uai.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn user_auth_initiate_round_trips_and_bites() {
let uai = UserAuthInitiate {
length_form: crate::length::LengthForm::Minimal,
binary_id: BinaryId {
specifier: 0x00_1B_67,
model: 0x1234,
version: 0x0005,
},
data: &[0xCA, 0xFE],
};
let bytes = uai.try_to_bytes().unwrap();
assert_eq!(
bytes,
[
0x9F, 0x80, 0x02, 0x09, 0x00, 0x1B, 0x67, 0x12, 0x34, 0x00, 0x05, 0xCA, 0xFE
]
);
assert_eq!(UserAuthInitiate::parse(&bytes).unwrap(), uai);
let mut other = uai.clone();
other.binary_id.version = 0x0006;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn user_auth_result_round_trips() {
let uar = UserAuthResult {
length_form: crate::length::LengthForm::Minimal,
binary_id: BinaryId {
specifier: 0xAABBCC & 0x00FF_FFFF,
model: 1,
version: 2,
},
result: &[0x01],
};
let bytes = uar.try_to_bytes().unwrap();
assert_eq!(
bytes,
[
0x9F, 0x80, 0x03, 0x08, 0xAA, 0xBB, 0xCC, 0x00, 0x01, 0x00, 0x02, 0x01
]
);
assert_eq!(UserAuthResult::parse(&bytes).unwrap(), uar);
}
#[test]
fn download_info_request_round_trips_and_bites() {
let compat = [0x00, 0x04, 0x00, 0x00, 0xAA, 0xBB];
let req = DownloadInfoRequest {
transaction_id: 0x0000_0001,
adaptation: &[],
buffer_size: 0x0001_0000,
maximum_block_size: 0x0200,
compatibility_descriptor: &compat,
private_data: &[],
};
let bytes = req.try_to_bytes().unwrap();
assert_eq!(DownloadInfoRequest::parse(&bytes).unwrap(), req);
assert_eq!(bytes[0], 0x11);
assert_eq!(bytes[1], 0x03);
assert_eq!(&bytes[2..4], &[0x10, 0x01]);
let mut other = req.clone();
other.buffer_size = 0x0002_0000;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn dsmcc_header_rejects_wrong_message_id() {
let cancel = DownloadCancel {
transaction_id: 1,
adaptation: &[],
download_id: 2,
module_id: 3,
block_number: 4,
download_cancel_reason: 5,
private_data: &[],
};
let cancel_bytes = cancel.try_to_bytes().unwrap();
assert!(matches!(
DownloadInfoRequest::parse(&cancel_bytes),
Err(Error::InvalidObject { .. })
));
let data_req = DownloadDataRequest {
download_id: 1,
adaptation: &[],
module_id: 2,
block_number: 3,
download_reason: 4,
};
let data_req_bytes = data_req.try_to_bytes().unwrap();
assert!(matches!(
DownloadDataBlock::parse(&data_req_bytes),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn dsmcc_header_rejects_wrong_protocol_discriminator_and_type() {
let req = DownloadInfoRequest {
transaction_id: 1,
adaptation: &[],
buffer_size: 0,
maximum_block_size: 0,
compatibility_descriptor: &[0x00, 0x00],
private_data: &[0x00, 0x00],
};
let mut bytes = req.try_to_bytes().unwrap();
bytes[0] = 0x12; assert!(matches!(
DownloadInfoRequest::parse(&bytes),
Err(Error::InvalidObject { .. })
));
let mut bytes = req.try_to_bytes().unwrap();
bytes[1] = 0x04; assert!(matches!(
DownloadInfoRequest::parse(&bytes),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn dsmcc_header_rejects_wrong_message_length() {
let req = DownloadInfoRequest {
transaction_id: 1,
adaptation: &[],
buffer_size: 0,
maximum_block_size: 0,
compatibility_descriptor: &[0x00, 0x00],
private_data: &[0x00, 0x00],
};
let mut bytes = req.try_to_bytes().unwrap();
bytes[11] ^= 0xFF;
assert!(matches!(
DownloadInfoRequest::parse(&bytes),
Err(Error::LengthMismatch(..))
));
}
#[test]
fn download_info_request_with_adaptation() {
let compat = [0x00, 0x02, 0x00, 0x00];
let req = DownloadInfoRequest {
transaction_id: 0x12,
adaptation: &[0x01, 0x02, 0x03],
buffer_size: 1,
maximum_block_size: 2,
compatibility_descriptor: &compat,
private_data: &[],
};
let bytes = req.try_to_bytes().unwrap();
assert_eq!(bytes[9], 0x03); assert_eq!(DownloadInfoRequest::parse(&bytes).unwrap(), req);
}
#[test]
fn download_info_response_multi_module_round_trips_and_bites() {
let compat = [0x00, 0x02, 0x00, 0x00];
let modules = [
0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01, 0xFF, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, ];
let resp = DownloadInfoResponse {
transaction_id: 1,
adaptation: &[],
download_id: 0xDEAD_BEEF,
block_size: 0x0100,
window_size: 4,
ack_period: 2,
tc_download_window: 1000,
tc_download_scenario: 2000,
compatibility_descriptor: &compat,
modules: &modules,
private_data: &[],
};
let bytes = resp.try_to_bytes().unwrap();
assert_eq!(DownloadInfoResponse::parse(&bytes).unwrap(), resp);
let mut other = resp.clone();
other.window_size = 5;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn download_cancel_round_trips() {
let cancel = DownloadCancel {
transaction_id: 0x10,
adaptation: &[],
download_id: 0x01,
module_id: 0x02,
block_number: 0x03,
download_cancel_reason: 0x05,
private_data: &[],
};
let bytes = cancel.try_to_bytes().unwrap();
assert_eq!(DownloadCancel::parse(&bytes).unwrap(), cancel);
assert_eq!(&bytes[2..4], &[0x10, 0x05]); }
#[test]
fn download_data_request_round_trips_and_bites() {
let req = DownloadDataRequest {
download_id: 0xDEAD_BEEF,
adaptation: &[],
module_id: 0x0001,
block_number: 0x0002,
download_reason: 0x00,
};
let bytes = req.try_to_bytes().unwrap();
assert_eq!(DownloadDataRequest::parse(&bytes).unwrap(), req);
assert_eq!(&bytes[2..4], &[0x10, 0x04]);
assert_eq!(&bytes[4..8], &0xDEAD_BEEFu32.to_be_bytes());
let mut other = req.clone();
other.block_number = 0x0003;
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn download_data_block_round_trips_and_bites() {
let block = DownloadDataBlock {
download_id: 0x0000_0001,
adaptation: &[],
module_id: 0x0001,
module_version: 0x02,
block_number: 0x0003,
block_data: &[0xFE, 0xED, 0xFA, 0xCE],
};
let bytes = block.try_to_bytes().unwrap();
assert_eq!(DownloadDataBlock::parse(&bytes).unwrap(), block);
assert_eq!(&bytes[2..4], &[0x10, 0x03]); let mut other = block.clone();
other.block_data = &[0xFE, 0xED, 0xFA, 0xCF];
assert_ne!(bytes, other.try_to_bytes().unwrap());
}
#[test]
fn enquiry_round_trips_a_real_dsmcc_message() {
let inner = DownloadDataRequest {
download_id: 0x1234_5678,
adaptation: &[],
module_id: 1,
block_number: 1,
download_reason: 0,
};
let inner_bytes = inner.try_to_bytes().unwrap();
let enq = DownloadEnquiry {
length_form: crate::length::LengthForm::Minimal,
dsmcc_message: &inner_bytes,
};
let outer = enq.try_to_bytes().unwrap();
let parsed = DownloadEnquiry::parse(&outer).unwrap();
assert_eq!(parsed, enq);
assert_eq!(
DownloadDataRequest::parse(parsed.dsmcc_message).unwrap(),
inner
);
}
#[test]
fn dispatch_routes_each_tag() {
let enq = DownloadEnquiry {
length_form: crate::length::LengthForm::Minimal,
dsmcc_message: &[0x11],
}
.try_to_bytes()
.unwrap();
assert!(matches!(
DownloadApdu::parse(&enq).unwrap(),
DownloadApdu::DownloadEnquiry(_)
));
let uar = UserAuthResult {
length_form: crate::length::LengthForm::Minimal,
binary_id: BinaryId::default(),
result: &[0x01],
}
.try_to_bytes()
.unwrap();
let parsed = DownloadApdu::parse(&uar).unwrap();
assert!(matches!(parsed, DownloadApdu::UserAuthResult(_)));
assert_eq!(parsed.try_to_bytes().unwrap(), uar);
}
#[test]
fn oversized_block_data_wraps_message_length_before_fix_rejected_after() {
let big = alloc::vec![0u8; 65_536];
let block = DownloadDataBlock {
download_id: 1,
adaptation: &[],
module_id: 1,
module_version: 0,
block_number: 0,
block_data: &big,
};
let mut buf = alloc::vec![0u8; 65_600];
assert!(matches!(
block.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn max_message_length_still_serializes_and_round_trips() {
let data = alloc::vec![0xABu8; 0xFFFF - 12 - 6];
let block = DownloadDataBlock {
download_id: 1,
adaptation: &[],
module_id: 1,
module_version: 0,
block_number: 0,
block_data: &data,
};
let bytes = block.try_to_bytes().unwrap();
assert_eq!(DownloadDataBlock::parse(&bytes).unwrap(), block);
}
#[test]
fn oversized_adaptation_is_rejected_not_wrapped() {
let big = alloc::vec![0xCDu8; 256];
let compat = [0x00, 0x02, 0x00, 0x00];
let req = DownloadInfoRequest {
transaction_id: 1,
adaptation: &big,
buffer_size: 1,
maximum_block_size: 2,
compatibility_descriptor: &compat,
private_data: &[],
};
let mut buf = alloc::vec![0u8; 512];
assert!(matches!(
req.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
#[test]
fn oversized_private_data_is_rejected_not_wrapped() {
let big = alloc::vec![0u8; 65_536];
let compat = [0x00, 0x02, 0x00, 0x00];
let req = DownloadInfoRequest {
transaction_id: 1,
adaptation: &[],
buffer_size: 1,
maximum_block_size: 2,
compatibility_descriptor: &compat,
private_data: &big,
};
let mut buf = alloc::vec![0u8; 65_600];
assert!(matches!(
req.serialize_into(&mut buf),
Err(Error::InvalidObject { .. })
));
}
}