use alloc::vec::Vec;
use super::oid::Oid;
use crate::ber::{Class, Cursor, Encoder, Tag, universal};
use crate::common::{DecodeReason, Error, Result};
pub const TAG_AARQ: Tag = Tag::application_constructed(0);
pub const TAG_AARE: Tag = Tag::application_constructed(1);
pub const TAG_RLRQ: Tag = Tag::application_constructed(2);
pub const TAG_RLRE: Tag = Tag::application_constructed(3);
pub const TAG_ABRT: Tag = Tag::application_constructed(4);
const TAG_EXTERNAL: Tag = Tag::universal(8, true);
pub const RESULT_ACCEPTED: i64 = 0;
pub const RESULT_REJECTED_PERMANENT: i64 = 1;
pub const RESULT_REJECTED_TRANSIENT: i64 = 2;
pub const DIAGNOSTIC_SERVICE_USER_NULL: &[u8] = &[0xA1, 0x03, 0x02, 0x01, 0x00];
pub const fn diagnostic_service_user(reason: u8) -> [u8; 5] {
[0xA1, 0x03, 0x02, 0x01, reason]
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Apdu<'a> {
Associate(Associate<'a>),
AssociateResponse(Associate<'a>),
Release(Option<i64>),
ReleaseResponse(Option<i64>),
Abort(Option<i64>),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Associate<'a> {
pub protocol_version: Option<(u8, &'a [u8])>,
pub context_name: Option<Oid<'a>>,
pub result: Option<i64>,
pub result_diagnostic: Option<&'a [u8]>,
pub called_ap_title: Option<&'a [u8]>,
pub called_ae_qualifier: Option<&'a [u8]>,
pub responding_ap_title: Option<&'a [u8]>,
pub responding_ae_qualifier: Option<&'a [u8]>,
pub calling_ap_title: Option<&'a [u8]>,
pub calling_ae_qualifier: Option<&'a [u8]>,
pub sender_requirements: Option<(u8, &'a [u8])>,
pub mechanism_name: Option<Oid<'a>>,
pub authentication_value: Option<&'a [u8]>,
pub user_information: Option<UserInformation<'a>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UserInformation<'a> {
pub direct_reference: Option<Oid<'a>>,
pub indirect_reference: Option<i64>,
pub value: &'a [u8],
}
impl<'a> Associate<'a> {
pub fn request(called_ap_title: Option<&'a [u8]>, calling_ap_title: Option<&'a [u8]>, mms_context: i64, initiate: &'a [u8]) -> Associate<'a> {
Associate {
protocol_version: Some((7, &[0x80])),
context_name: Some(Oid::MMS_APPLICATION_CONTEXT),
called_ap_title,
calling_ap_title,
user_information: Some(UserInformation { direct_reference: Some(Oid::BER), indirect_reference: Some(mms_context), value: initiate }),
..Associate::default()
}
}
pub fn accepted(&self) -> bool {
self.result == Some(RESULT_ACCEPTED)
}
pub fn mms_pdu(&self) -> Option<&'a [u8]> {
self.user_information.map(|u| u.value)
}
}
fn parse_external<'a>(t: &crate::ber::Tlv<'a>) -> Result<UserInformation<'a>> {
let mut c = t.expect(TAG_EXTERNAL)?.children();
let mut out = UserInformation { direct_reference: None, indirect_reference: None, value: &[] };
let mut next = c.next_required()?;
if next.tag == Tag::universal(universal::OID, false) {
out.direct_reference = Some(Oid(next.value));
next = c.next_required()?;
}
if next.tag == Tag::universal(universal::INTEGER, false) {
out.indirect_reference = Some(next.integer_i64()?);
next = c.next_required()?;
}
out.value = match (next.tag.class, next.tag.number) {
(Class::Context, 0 | 1) => next.value,
_ => return Err(Error::decode(DecodeReason::UnexpectedTag, next.offset)),
};
Ok(out)
}
impl<'a> Apdu<'a> {
pub fn parse(buf: &'a [u8]) -> Result<Apdu<'a>> {
let top = Cursor::new(buf).next_required()?;
if top.tag.class != Class::Application {
return Err(Error::decode(DecodeReason::UnexpectedTag, top.offset));
}
match top.tag.number {
0 | 1 => {
let mut a = Associate::default();
for field in top.children() {
let f = field?;
if f.tag.class != Class::Context {
continue;
}
match f.tag.number {
0 => a.protocol_version = Some(f.bit_string()?),
1 => a.context_name = Some(Oid(f.children().next_required()?.value)),
2 if top.tag.number == 0 => a.called_ap_title = Some(f.value),
2 => a.result = Some(f.children().next_required()?.integer_i64()?),
3 if top.tag.number == 0 => a.called_ae_qualifier = Some(f.value),
3 => a.result_diagnostic = Some(f.value),
4 if top.tag.number == 1 => a.responding_ap_title = Some(f.value),
5 if top.tag.number == 1 => a.responding_ae_qualifier = Some(f.value),
6 => a.calling_ap_title = Some(f.value),
7 => a.calling_ae_qualifier = Some(f.value),
8 if top.tag.number == 1 => a.sender_requirements = Some(f.bit_string()?),
10 if top.tag.number == 0 => a.sender_requirements = Some(f.bit_string()?),
9 if top.tag.number == 1 => a.mechanism_name = Some(Oid(f.value)),
11 if top.tag.number == 0 => a.mechanism_name = Some(Oid(f.value)),
10 if top.tag.number == 1 => a.authentication_value = Some(f.value),
12 if top.tag.number == 0 => a.authentication_value = Some(f.value),
30 => a.user_information = Some(parse_external(&f.children().next_required()?)?),
_ => {}
}
}
Ok(if top.tag.number == 0 { Apdu::Associate(a) } else { Apdu::AssociateResponse(a) })
}
2..=4 => {
let mut value = None;
for field in top.children() {
let f = field?;
if f.tag.class == Class::Context && f.tag.number == 0 {
value = Some(f.integer_i64()?);
}
}
Ok(match top.tag.number {
2 => Apdu::Release(value),
3 => Apdu::ReleaseResponse(value),
_ => Apdu::Abort(value),
})
}
_ => Err(Error::decode(DecodeReason::UnexpectedTag, top.offset)),
}
}
#[allow(clippy::too_many_lines)] pub fn write(&self, out: &mut Encoder) -> Result<()> {
match self {
Apdu::Associate(a) | Apdu::AssociateResponse(a) => {
let request = matches!(self, Apdu::Associate(_));
let tag = if request { TAG_AARQ } else { TAG_AARE };
out.constructed(tag, |e| {
if let Some((unused, bytes)) = a.protocol_version {
e.bit_string(Tag::context(0), unused, bytes)?;
}
if let Some(oid) = a.context_name {
e.constructed(Tag::context_constructed(1), |e| {
e.primitive(Tag::universal(universal::OID, false), oid.0)?;
Ok(())
})?;
}
if request {
for (n, v) in [(2, a.called_ap_title), (3, a.called_ae_qualifier)] {
if let Some(v) = v {
e.primitive(Tag::context_constructed(n), v)?;
}
}
} else {
if let Some(r) = a.result {
e.constructed(Tag::context_constructed(2), |e| {
e.integer(Tag::universal(universal::INTEGER, false), r)?;
Ok(())
})?;
}
match (a.result_diagnostic, a.result) {
(Some(d), _) => {
e.primitive(Tag::context_constructed(3), d)?;
}
(None, Some(_)) => {
e.primitive(Tag::context_constructed(3), DIAGNOSTIC_SERVICE_USER_NULL)?;
}
(None, None) => {}
}
for (n, v) in [(4, a.responding_ap_title), (5, a.responding_ae_qualifier)] {
if let Some(v) = v {
e.primitive(Tag::context_constructed(n), v)?;
}
}
}
for (n, v) in [(6, a.calling_ap_title), (7, a.calling_ae_qualifier)] {
if let Some(v) = v {
e.primitive(Tag::context_constructed(n), v)?;
}
}
let (req, mech, auth) = if request { (10, 11, 12) } else { (8, 9, 10) };
if let Some((unused, bytes)) = a.sender_requirements {
e.bit_string(Tag::context(req), unused, bytes)?;
}
if let Some(oid) = a.mechanism_name {
e.primitive(Tag::context(mech), oid.0)?;
}
if let Some(v) = a.authentication_value {
e.primitive(Tag::context_constructed(auth), v)?;
}
if let Some(u) = a.user_information {
e.constructed(Tag::context_constructed(30), |e| {
e.constructed(TAG_EXTERNAL, |e| {
if let Some(oid) = u.direct_reference {
e.primitive(Tag::universal(universal::OID, false), oid.0)?;
}
if let Some(i) = u.indirect_reference {
e.integer(Tag::universal(universal::INTEGER, false), i)?;
}
e.constructed(Tag::context_constructed(0), |e| {
e.raw(u.value);
Ok(())
})?;
Ok(())
})?;
Ok(())
})?;
}
Ok(())
})?;
Ok(())
}
Apdu::Release(v) | Apdu::ReleaseResponse(v) | Apdu::Abort(v) => {
let tag = match self {
Apdu::Release(_) => TAG_RLRQ,
Apdu::ReleaseResponse(_) => TAG_RLRE,
_ => TAG_ABRT,
};
out.constructed(tag, |e| {
if let Some(v) = v {
e.integer(Tag::context(0), *v)?;
}
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::*;
#[test]
fn an_aare_that_carries_a_result_carries_a_diagnostic_too() {
let initiate = [0xA9u8, 0x00];
let aare = Associate {
protocol_version: Some((7, &[0x80])),
context_name: Some(Oid::MMS_APPLICATION_CONTEXT),
result: Some(RESULT_ACCEPTED),
user_information: Some(UserInformation { direct_reference: Some(Oid::BER), indirect_reference: Some(3), value: &initiate }),
..Associate::default()
};
let bytes = Apdu::AssociateResponse(aare).to_vec().expect("encode");
let mut c = Cursor::new(&bytes).next_required().expect("aare").children();
assert_eq!(c.next_required().expect("protocol-version").tag, Tag::context(0));
assert_eq!(c.next_required().expect("aSO-context-name").tag, Tag::context_constructed(1));
assert_eq!(c.next_required().expect("result").tag, Tag::context_constructed(2));
let diagnostic = c.next_required().expect("result-source-diagnostic");
assert_eq!(diagnostic.tag, Tag::context_constructed(3));
assert_eq!(diagnostic.value, DIAGNOSTIC_SERVICE_USER_NULL, "service-user null(0)");
assert_eq!(c.next_required().expect("user-information").tag, Tag::context_constructed(30));
assert_eq!(diagnostic_service_user(13), [0xA1, 0x03, 0x02, 0x01, 13], "authentication-failure");
let Apdu::AssociateResponse(back) = Apdu::parse(&bytes).expect("decode") else { panic!("not an AARE") };
assert_eq!(back.result_diagnostic, Some(DIAGNOSTIC_SERVICE_USER_NULL));
assert_eq!(Apdu::AssociateResponse(back).to_vec().expect("re-encode"), bytes);
}
const REFERENCE_AARQ: &[u8] = &[
0x60, 0x32, 0x80, 0x02, 0x07, 0x80, 0xA1, 0x07, 0x06, 0x05, 0x28, 0xCA, 0x22, 0x01, 0x01, 0xA2, 0x04, 0x06, 0x02, 0x29, 0x02, 0xA3, 0x03, 0x02, 0x01, 0x02, 0xA6, 0x04, 0x06, 0x02, 0x29, 0x01, 0xA7, 0x03, 0x02, 0x01, 0x01, 0xBE, 0x0D, 0x28, 0x0B, 0x06, 0x02, 0x51, 0x01, 0x02, 0x01, 0x03, 0xA0, 0x02, 0xA8, 0x00, ];
#[test]
fn the_reference_associate_request_round_trips_byte_for_byte() {
let Apdu::Associate(a) = Apdu::parse(REFERENCE_AARQ).unwrap() else { panic!("not an AARQ") };
assert_eq!(a.protocol_version, Some((7, &[0x80][..])));
assert_eq!(a.context_name, Some(Oid::MMS_APPLICATION_CONTEXT_9506));
assert_eq!(a.called_ap_title, Some(&[0x06, 0x02, 0x29, 0x02][..]));
assert_eq!(a.calling_ae_qualifier, Some(&[0x02, 0x01, 0x01][..]));
let u = a.user_information.unwrap();
assert_eq!(u.direct_reference, Some(Oid::BER));
assert_eq!(u.indirect_reference, Some(3));
assert_eq!(a.mms_pdu(), Some(&[0xA8, 0x00][..]));
assert_eq!(Apdu::Associate(a).to_vec().unwrap(), REFERENCE_AARQ);
}
#[test]
fn an_associate_response_carries_the_result_and_the_external_may_omit_its_syntax() {
let wire: &[u8] = &[
0x61, 0x24, 0x80, 0x02, 0x07, 0x80, 0xA1, 0x07, 0x06, 0x05, 0x28, 0xCA, 0x22, 0x01, 0x01, 0xA2, 0x03, 0x02, 0x01, 0x00, 0xA3, 0x05, 0xA1, 0x03, 0x02, 0x01, 0x00, 0xBE, 0x09, 0x28, 0x07, 0x02, 0x01, 0x03, 0xA0, 0x02, 0xA9, 0x00,
];
let Apdu::AssociateResponse(a) = Apdu::parse(wire).unwrap() else { panic!("not an AARE") };
assert!(a.accepted());
assert_eq!(a.result_diagnostic, Some(&[0xA1, 0x03, 0x02, 0x01, 0x00][..]));
let u = a.user_information.unwrap();
assert_eq!((u.direct_reference, u.indirect_reference), (None, Some(3)));
assert_eq!(a.mms_pdu(), Some(&[0xA9, 0x00][..]));
assert_eq!(Apdu::AssociateResponse(a).to_vec().unwrap(), wire);
}
#[test]
fn the_password_fields_sit_at_different_tags_in_the_request_and_the_response() {
let mut a = Associate::request(None, None, 3, &[0xA8, 0x00]);
a.sender_requirements = Some((7, &[0x80]));
a.mechanism_name = Some(Oid::PASSWORD_MECHANISM);
a.authentication_value = Some(&[0x80, 0x04, b'p', b'a', b's', b's']);
let wire = Apdu::Associate(a).to_vec().unwrap();
assert!(wire.windows(2).any(|w| w == [0x8A, 0x02]), "sender-acse-requirements is [10] in an AARQ");
assert!(wire.windows(2).any(|w| w == [0x8B, 0x03]), "mechanism-name is [11]");
assert!(wire.windows(2).any(|w| w == [0xAC, 0x06]), "calling-authentication-value is [12]");
let Apdu::Associate(back) = Apdu::parse(&wire).unwrap() else { panic!() };
assert_eq!(back.mechanism_name, Some(Oid::PASSWORD_MECHANISM));
assert_eq!(back.authentication_value, Some(&[0x80, 0x04, b'p', b'a', b's', b's'][..]));
let mut r = Associate { result: Some(RESULT_ACCEPTED), ..Associate::default() };
r.sender_requirements = Some((7, &[0x80]));
r.mechanism_name = Some(Oid::PASSWORD_MECHANISM);
let wire = Apdu::AssociateResponse(r).to_vec().unwrap();
assert!(wire.windows(2).any(|w| w == [0x88, 0x02]), "responder-acse-requirements is [8] in an AARE");
assert!(wire.windows(2).any(|w| w == [0x89, 0x03]), "mechanism-name is [9]");
}
#[test]
fn release_and_abort_round_trip() {
for (apdu, first) in [(Apdu::Release(Some(0)), 0x62u8), (Apdu::ReleaseResponse(Some(0)), 0x63), (Apdu::Abort(Some(1)), 0x64)] {
let wire = apdu.to_vec().unwrap();
assert_eq!(wire.first(), Some(&first));
assert_eq!(Apdu::parse(&wire).unwrap(), apdu);
}
assert_eq!(Apdu::parse(&[0x62, 0x00]).unwrap(), Apdu::Release(None));
}
#[test]
fn truncation_is_an_error_not_a_panic() {
for cut in 0..REFERENCE_AARQ.len() {
let _ = Apdu::parse(&REFERENCE_AARQ[..cut]);
}
assert!(Apdu::parse(&[]).is_err());
assert!(Apdu::parse(&[0x30, 0x00]).is_err(), "a SEQUENCE is not an ACSE APDU");
assert!(Apdu::parse(&[0x6F, 0x00]).is_err(), "an unknown application tag");
}
}