use alloc::vec::Vec;
use super::oid::Oid;
use crate::ber::{Class, Cursor, Encoder, Tag, Tlv, universal};
use crate::common::{DecodeReason, Error, Result};
const TAG_SET: Tag = Tag::universal(17, true);
const TAG_FULLY_ENCODED_DATA: Tag = Tag::application_constructed(1);
pub const MODE_NORMAL: i64 = 1;
pub const RESULT_ACCEPTANCE: i64 = 0;
pub const RESULT_USER_REJECTION: i64 = 1;
pub const RESULT_PROVIDER_REJECTION: i64 = 2;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ContextDefinition<'a> {
pub id: u16,
pub abstract_syntax: Oid<'a>,
pub transfer_syntax: Oid<'a>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ContextResult<'a> {
pub result: i64,
pub transfer_syntax: Option<Oid<'a>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PdvValues<'a> {
SingleAsn1Type(&'a [u8]),
OctetAligned(&'a [u8]),
Arbitrary {
unused: u8,
bytes: &'a [u8],
},
}
impl<'a> PdvValues<'a> {
pub fn single(&self) -> Option<&'a [u8]> {
match self {
PdvValues::SingleAsn1Type(v) => Some(v),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Pdv<'a> {
pub context_id: u16,
pub values: PdvValues<'a>,
}
impl<'a> Pdv<'a> {
pub fn single(context_id: u16, value: &'a [u8]) -> Pdv<'a> {
Pdv { context_id, values: PdvValues::SingleAsn1Type(value) }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Ppdu<'a> {
Connect(Cp<'a>),
Accept(Cp<'a>),
Reject(Cp<'a>),
UserData(Vec<Pdv<'a>>),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Cp<'a> {
pub protocol_version: Option<(u8, &'a [u8])>,
pub calling_psel: Option<&'a [u8]>,
pub called_psel: Option<&'a [u8]>,
pub responding_psel: Option<&'a [u8]>,
pub contexts: Vec<ContextDefinition<'a>>,
pub results: Vec<ContextResult<'a>>,
pub requirements: Option<(u8, &'a [u8])>,
pub provider_reason: Option<i64>,
pub user_data: Vec<Pdv<'a>>,
}
impl<'a> Cp<'a> {
pub fn connect(calling_psel: &'a [u8], called_psel: &'a [u8], acse_context: u16, mms_context: u16, aarq: &'a [u8]) -> Cp<'a> {
Cp {
protocol_version: Some((7, &[0x80])),
calling_psel: Some(calling_psel),
called_psel: Some(called_psel),
contexts: alloc::vec![
ContextDefinition { id: acse_context, abstract_syntax: Oid::ACSE_ABSTRACT_SYNTAX, transfer_syntax: Oid::BER },
ContextDefinition { id: mms_context, abstract_syntax: Oid::MMS_ABSTRACT_SYNTAX, transfer_syntax: Oid::BER },
],
requirements: Some((6, &[0x00])),
user_data: alloc::vec![Pdv::single(acse_context, aarq)],
..Cp::default()
}
}
pub fn context_for(&self, abstract_syntax: Oid<'_>) -> Option<u16> {
self.contexts.iter().find(|c| c.abstract_syntax == abstract_syntax).map(|c| c.id)
}
pub fn all_accepted(&self) -> bool {
!self.results.is_empty() && self.results.iter().all(|r| r.result == RESULT_ACCEPTANCE)
}
}
fn oid_of<'a>(t: &Tlv<'a>) -> Result<Oid<'a>> {
if t.tag == Tag::universal(universal::OID, false) { Ok(Oid(t.value)) } else { Err(Error::decode(DecodeReason::UnexpectedTag, t.offset)) }
}
fn parse_pdvs<'a>(t: &Tlv<'a>) -> Result<Vec<Pdv<'a>>> {
let mut out = Vec::new();
for item in t.children() {
let mut c = item?.expect(Tag::universal(universal::SEQUENCE, true))?.children();
let mut next = c.next_required()?;
if next.tag == Tag::universal(universal::OID, false) {
next = c.next_required()?;
}
let context_id = u16::try_from(next.expect(Tag::universal(universal::INTEGER, false))?.integer_i64()?)
.map_err(|_| Error::decode(DecodeReason::BadValue, next.value_offset))?;
let v = c.next_required()?;
let values = match (v.tag.class, v.tag.number, v.tag.constructed) {
(Class::Context, 0, true) => PdvValues::SingleAsn1Type(v.value),
(Class::Context, 1, false) => PdvValues::OctetAligned(v.value),
(Class::Context, 2, false) => {
let (unused, bytes) = v.bit_string()?;
PdvValues::Arbitrary { unused, bytes }
}
_ => return Err(Error::decode(DecodeReason::UnexpectedTag, v.offset)),
};
out.push(Pdv { context_id, values });
}
Ok(out)
}
fn write_pdvs(pdvs: &[Pdv<'_>], e: &mut Encoder) -> Result<()> {
e.constructed(TAG_FULLY_ENCODED_DATA, |e| {
for p in pdvs {
e.constructed(Tag::universal(universal::SEQUENCE, true), |e| {
e.integer(Tag::universal(universal::INTEGER, false), i64::from(p.context_id))?;
match p.values {
PdvValues::SingleAsn1Type(v) => {
e.constructed(Tag::context_constructed(0), |e| {
e.raw(v);
Ok(())
})?;
}
PdvValues::OctetAligned(v) => {
e.primitive(Tag::context(1), v)?;
}
PdvValues::Arbitrary { unused, bytes } => {
e.bit_string(Tag::context(2), unused, bytes)?;
}
}
Ok(())
})?;
}
Ok(())
})?;
Ok(())
}
fn parse_normal_mode<'a>(field: &Tlv<'a>, cp: &mut Cp<'a>) -> Result<bool> {
let mut is_response = false;
for p in field.children() {
let p = p?;
match (p.tag.number, p.tag.constructed) {
(0, false) => cp.protocol_version = Some(p.bit_string()?),
(1, false) => cp.calling_psel = Some(p.value),
(2, false) => cp.called_psel = Some(p.value),
(3, false) => {
cp.responding_psel = Some(p.value);
is_response = true;
}
(4, true) => {
for item in p.children() {
let mut c = item?.expect(Tag::universal(universal::SEQUENCE, true))?.children();
let id = u16::try_from(c.next_tag(Tag::universal(universal::INTEGER, false))?.integer_i64()?)
.map_err(|_| Error::decode(DecodeReason::BadValue, p.offset))?;
let abstract_syntax = oid_of(&c.next_required()?)?;
let list = c.next_tag(Tag::universal(universal::SEQUENCE, true))?;
let transfer_syntax = oid_of(&list.children().next_required()?)?;
cp.contexts.push(ContextDefinition { id, abstract_syntax, transfer_syntax });
}
}
(5, true) => {
is_response = true;
for item in p.children() {
let mut c = item?.expect(Tag::universal(universal::SEQUENCE, true))?.children();
let result = c.next_tag(Tag::context(0))?.integer_i64()?;
let transfer_syntax = c.next_if_tag(Tag::context(1))?.map(|t| Oid(t.value));
cp.results.push(ContextResult { result, transfer_syntax });
}
}
(8, false) => cp.requirements = Some(p.bit_string()?),
(10, false) => cp.provider_reason = Some(p.integer_i64()?),
_ if p.tag.class == Class::Application && p.tag.number == 1 => cp.user_data = parse_pdvs(&p)?,
_ => {}
}
}
Ok(is_response)
}
fn write_normal_mode(cp: &Cp<'_>, e: &mut Encoder) -> Result<()> {
if let Some((unused, bytes)) = cp.protocol_version {
e.bit_string(Tag::context(0), unused, bytes)?;
}
if let Some(s) = cp.calling_psel {
e.primitive(Tag::context(1), s)?;
}
if let Some(s) = cp.called_psel {
e.primitive(Tag::context(2), s)?;
}
if let Some(s) = cp.responding_psel {
e.primitive(Tag::context(3), s)?;
}
if !cp.contexts.is_empty() {
e.constructed(Tag::context_constructed(4), |e| {
for c in &cp.contexts {
e.constructed(Tag::universal(universal::SEQUENCE, true), |e| {
e.integer(Tag::universal(universal::INTEGER, false), i64::from(c.id))?;
e.primitive(Tag::universal(universal::OID, false), c.abstract_syntax.0)?;
e.constructed(Tag::universal(universal::SEQUENCE, true), |e| {
e.primitive(Tag::universal(universal::OID, false), c.transfer_syntax.0)?;
Ok(())
})?;
Ok(())
})?;
}
Ok(())
})?;
}
if !cp.results.is_empty() {
e.constructed(Tag::context_constructed(5), |e| {
for r in &cp.results {
e.constructed(Tag::universal(universal::SEQUENCE, true), |e| {
e.integer(Tag::context(0), r.result)?;
if let Some(t) = r.transfer_syntax {
e.primitive(Tag::context(1), t.0)?;
}
Ok(())
})?;
}
Ok(())
})?;
}
if let Some((unused, bytes)) = cp.requirements {
e.bit_string(Tag::context(8), unused, bytes)?;
}
if let Some(reason) = cp.provider_reason {
e.integer(Tag::context(10), reason)?;
}
if !cp.user_data.is_empty() {
write_pdvs(&cp.user_data, e)?;
}
Ok(())
}
impl<'a> Ppdu<'a> {
pub fn parse(buf: &'a [u8], handshake: bool) -> Result<Ppdu<'a>> {
let top = Cursor::new(buf).next_required()?;
if !handshake {
return Ok(Ppdu::UserData(parse_pdvs(&top.expect(TAG_FULLY_ENCODED_DATA)?)?));
}
if top.tag == Tag::universal(universal::SEQUENCE, true) {
let mut cp = Cp::default();
parse_normal_mode(&top, &mut cp)?;
return Ok(Ppdu::Reject(cp));
}
let mut cp = Cp::default();
let mut mode = 0i64;
let mut is_accept = false;
for field in top.expect(TAG_SET)?.children() {
let field = field?;
match (field.tag.number, field.tag.constructed) {
(0, true) => {
let m = field.children().next_tag(Tag::context(0))?;
mode = m.integer_i64()?;
}
(2, true) => is_accept |= parse_normal_mode(&field, &mut cp)?,
_ => {}
}
}
if mode != MODE_NORMAL {
return Err(Error::decode(DecodeReason::BadValue, top.value_offset));
}
Ok(if is_accept { Ppdu::Accept(cp) } else { Ppdu::Connect(cp) })
}
pub fn write(&self, out: &mut Encoder) -> Result<()> {
match self {
Ppdu::UserData(pdvs) => write_pdvs(pdvs, out),
Ppdu::Reject(cp) => {
out.constructed(Tag::universal(universal::SEQUENCE, true), |e| write_normal_mode(cp, e))?;
Ok(())
}
Ppdu::Connect(cp) | Ppdu::Accept(cp) => {
out.constructed(TAG_SET, |e| {
e.constructed(Tag::context_constructed(0), |e| {
e.integer(Tag::context(0), MODE_NORMAL)?;
Ok(())
})?;
e.constructed(Tag::context_constructed(2), |e| write_normal_mode(cp, e))?;
Ok(())
})?;
Ok(())
}
}
}
pub fn to_vec(&self) -> Result<Vec<u8>> {
let mut e = Encoder::new();
self.write(&mut e)?;
Ok(e.into_vec())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn reference_cp() -> Vec<u8> {
let mut e = Encoder::new();
Ppdu::Connect(Cp::connect(&[0, 0, 0, 1], &[0, 0, 0, 2], 1, 3, &[0x60, 0x02, 0x80, 0x00])).write(&mut e).unwrap();
e.into_vec()
}
#[test]
fn the_connect_pdu_matches_the_reference_capture_field_for_field() {
let wire = reference_cp();
assert_eq!(wire.first(), Some(&0x31), "CP-type is a SET");
assert_eq!(&wire[2..7], &[0xA0, 0x03, 0x80, 0x01, 0x01], "mode-selector, normal mode");
assert_eq!(wire[7], 0xA2, "normal-mode-parameters");
assert_eq!(
&wire[9..23],
&[
0x80, 0x02, 0x07, 0x80, 0x81, 0x04, 0x00, 0x00, 0x00, 0x01, 0x82, 0x04, 0x00, 0x00, 0x00, 0x02, ][..14]
);
assert!(
wire.windows(17).any(|w| w == [0x30, 0x0F, 0x02, 0x01, 0x01, 0x06, 0x04, 0x52, 0x01, 0x00, 0x01, 0x30, 0x04, 0x06, 0x02, 0x51, 0x01]),
"context 1 is ACSE in BER, exactly as the capture writes it"
);
assert!(
wire.windows(18).any(|w| w == [0x30, 0x10, 0x02, 0x01, 0x03, 0x06, 0x05, 0x28, 0xCA, 0x22, 0x02, 0x01, 0x30, 0x04, 0x06, 0x02, 0x51, 0x01]),
"context 3 is MMS in BER"
);
assert!(wire.windows(4).any(|w| w == [0x88, 0x02, 0x06, 0x00]), "presentation-requirements");
let Ppdu::Connect(cp) = Ppdu::parse(&wire, true).unwrap() else { panic!("not a CP") };
assert_eq!(cp.protocol_version, Some((7, &[0x80][..])));
assert_eq!(cp.calling_psel, Some(&[0, 0, 0, 1][..]));
assert_eq!(cp.contexts.len(), 2);
assert_eq!(cp.context_for(Oid::MMS_ABSTRACT_SYNTAX), Some(3));
assert_eq!(cp.context_for(Oid::ACSE_ABSTRACT_SYNTAX), Some(1));
assert_eq!(cp.contexts[1].transfer_syntax, Oid::BER);
assert_eq!(cp.user_data.len(), 1);
assert_eq!(cp.user_data[0].context_id, 1);
assert_eq!(cp.user_data[0].values.single(), Some(&[0x60, 0x02, 0x80, 0x00][..]));
assert_eq!(Ppdu::Connect(cp).to_vec().unwrap(), wire, "re-encoding must reproduce the octets");
}
#[test]
fn the_accept_pdu_carries_a_result_list() {
let cpa = Cp {
protocol_version: Some((7, &[0x80])),
responding_psel: Some(&[0, 0, 0, 2]),
results: alloc::vec![
ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) },
ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) },
],
user_data: alloc::vec![Pdv::single(1, &[0x61, 0x02, 0x80, 0x00])],
..Cp::default()
};
let wire = Ppdu::Accept(cpa).to_vec().unwrap();
let Ppdu::Accept(back) = Ppdu::parse(&wire, true).unwrap() else { panic!("not a CPA") };
assert!(back.all_accepted());
assert_eq!(back.responding_psel, Some(&[0, 0, 0, 2][..]));
assert_eq!(back.results.len(), 2);
assert_eq!(back.user_data[0].values.single(), Some(&[0x61, 0x02, 0x80, 0x00][..]));
assert_eq!(Ppdu::Accept(back).to_vec().unwrap(), wire);
}
#[test]
fn a_data_pdu_is_a_bare_user_data() {
let wire = [0x61, 0x0F, 0x30, 0x0D, 0x02, 0x01, 0x03, 0xA0, 0x08, 0xA0, 0x06, 0x02, 0x02, 0x11, 0x4F, 0x82, 0x00];
let Ppdu::UserData(pdvs) = Ppdu::parse(&wire, false).unwrap() else { panic!("not user data") };
assert_eq!(pdvs.len(), 1);
assert_eq!(pdvs[0].context_id, 3);
assert_eq!(pdvs[0].values.single(), Some(&[0xA0, 0x06, 0x02, 0x02, 0x11, 0x4F, 0x82, 0x00][..]));
assert_eq!(Ppdu::UserData(pdvs).to_vec().unwrap(), wire, "byte for byte");
}
#[test]
fn a_pdv_may_name_its_transfer_syntax() {
let wire = [0x61, 0x0D, 0x30, 0x0B, 0x06, 0x02, 0x51, 0x01, 0x02, 0x01, 0x03, 0xA0, 0x02, 0x80, 0x00];
let Ppdu::UserData(pdvs) = Ppdu::parse(&wire, false).unwrap() else { panic!("not user data") };
assert_eq!(pdvs[0].context_id, 3);
assert_eq!(pdvs[0].values.single(), Some(&[0x80, 0x00][..]));
}
#[test]
fn a_refused_connection_is_decoded_rather_than_reported_as_a_decode_failure() {
let cpr = Cp {
protocol_version: Some((7, &[0x80])),
results: alloc::vec![ContextResult { result: RESULT_PROVIDER_REJECTION, transfer_syntax: None }],
provider_reason: Some(1),
..Cp::default()
};
let wire = Ppdu::Reject(cpr).to_vec().unwrap();
assert_eq!(wire.first(), Some(&0x30), "a CPR is a SEQUENCE, not a SET");
let Ppdu::Reject(back) = Ppdu::parse(&wire, true).unwrap() else { panic!("not a CPR") };
assert_eq!(back.provider_reason, Some(1));
assert_eq!(back.results[0].result, RESULT_PROVIDER_REJECTION);
assert!(!back.all_accepted());
assert_eq!(Ppdu::Reject(back).to_vec().unwrap(), wire);
let with_data = Cp { provider_reason: Some(2), user_data: alloc::vec![Pdv::single(1, &[0x64, 0x00])], ..Cp::default() };
let wire = Ppdu::Reject(with_data).to_vec().unwrap();
let reason_at = wire.windows(3).position(|w| w == [0x8A, 0x01, 0x02]).expect("provider-reason");
let data_at = wire.iter().position(|b| *b == 0x61).expect("user-data");
assert!(reason_at < data_at, "provider-reason must precede the user data");
}
#[test]
fn truncation_and_the_wrong_mode_are_errors() {
let wire = reference_cp();
for cut in 0..wire.len() {
let _ = Ppdu::parse(&wire[..cut], true);
}
let x410 = [0x31, 0x07, 0xA0, 0x03, 0x80, 0x01, 0x00, 0x00];
assert!(Ppdu::parse(&x410, true).is_err());
assert!(Ppdu::parse(&[], false).is_err());
}
}