use super::{push_length, read_length, ServiceError};
pub const GENERAL_GLO_CIPHERING_TAG: u8 = 0xDB;
pub const GENERAL_DED_CIPHERING_TAG: u8 = 0xDC;
pub const GENERAL_CIPHERING_TAG: u8 = 0xDD;
pub const GENERAL_SIGNING_TAG: u8 = 0xDF;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeyInfo {
None,
AgreedKey {
key_parameters: Vec<u8>,
key_ciphered_data: Vec<u8>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneralGloDedCiphering {
pub dedicated: bool,
pub system_title: Vec<u8>,
pub ciphered_service: Vec<u8>,
}
impl GeneralGloDedCiphering {
pub fn encode(&self) -> Vec<u8> {
let tag = if self.dedicated { GENERAL_DED_CIPHERING_TAG } else { GENERAL_GLO_CIPHERING_TAG };
let mut buf = vec![tag];
push_octet_string(&self.system_title, &mut buf);
push_octet_string(&self.ciphered_service, &mut buf);
buf
}
pub fn decode(bytes: &[u8]) -> Result<GeneralGloDedCiphering, ServiceError> {
let dedicated = match bytes.first() {
Some(&GENERAL_GLO_CIPHERING_TAG) => false,
Some(&GENERAL_DED_CIPHERING_TAG) => true,
Some(&other) => return Err(ServiceError::UnexpectedTag(other)),
None => return Err(ServiceError::Truncated),
};
let (system_title, n) = take_octet_string(&bytes[1..])?;
let (ciphered_service, _) = take_octet_string(&bytes[1 + n..])?;
Ok(GeneralGloDedCiphering { dedicated, system_title, ciphered_service })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneralCiphering {
pub transaction_id: Vec<u8>,
pub originator_system_title: Vec<u8>,
pub recipient_system_title: Vec<u8>,
pub date_time: Vec<u8>,
pub other_information: Vec<u8>,
pub key_info: KeyInfo,
pub ciphered_content: Vec<u8>,
}
impl GeneralCiphering {
pub fn encode(&self) -> Vec<u8> {
let mut buf = vec![GENERAL_CIPHERING_TAG];
push_octet_string(&self.transaction_id, &mut buf);
push_octet_string(&self.originator_system_title, &mut buf);
push_octet_string(&self.recipient_system_title, &mut buf);
push_octet_string(&self.date_time, &mut buf);
push_octet_string(&self.other_information, &mut buf);
match &self.key_info {
KeyInfo::None => buf.push(0x00),
KeyInfo::AgreedKey { key_parameters, key_ciphered_data } => {
buf.push(0x01); buf.push(0x02); push_octet_string(key_parameters, &mut buf);
push_octet_string(key_ciphered_data, &mut buf);
}
}
push_octet_string(&self.ciphered_content, &mut buf);
buf
}
pub fn decode(bytes: &[u8]) -> Result<GeneralCiphering, ServiceError> {
if bytes.first() != Some(&GENERAL_CIPHERING_TAG) {
return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
}
let mut pos = 1;
let (transaction_id, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (originator_system_title, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (recipient_system_title, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (date_time, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (other_information, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let key_info = match bytes.get(pos) {
Some(0x00) => {
pos += 1;
KeyInfo::None
}
Some(0x01) => {
match bytes.get(pos + 1) {
Some(0x02) => {
pos += 2;
let (key_parameters, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (key_ciphered_data, n) = take_octet_string(&bytes[pos..])?;
pos += n;
KeyInfo::AgreedKey { key_parameters, key_ciphered_data }
}
Some(_) => return Err(ServiceError::InvalidData), None => return Err(ServiceError::Truncated),
}
}
Some(&other) => return Err(ServiceError::UnexpectedType(other)),
None => return Err(ServiceError::Truncated),
};
let (ciphered_content, _) = take_octet_string(&bytes[pos..])?;
Ok(GeneralCiphering {
transaction_id,
originator_system_title,
recipient_system_title,
date_time,
other_information,
key_info,
ciphered_content,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneralSigning {
pub transaction_id: Vec<u8>,
pub originator_system_title: Vec<u8>,
pub recipient_system_title: Vec<u8>,
pub date_time: Vec<u8>,
pub other_information: Vec<u8>,
pub content: Vec<u8>,
pub signature: Vec<u8>,
}
impl GeneralSigning {
pub fn encode(&self) -> Vec<u8> {
let mut buf = vec![GENERAL_SIGNING_TAG];
push_octet_string(&self.transaction_id, &mut buf);
push_octet_string(&self.originator_system_title, &mut buf);
push_octet_string(&self.recipient_system_title, &mut buf);
push_octet_string(&self.date_time, &mut buf);
push_octet_string(&self.other_information, &mut buf);
push_octet_string(&self.content, &mut buf);
push_octet_string(&self.signature, &mut buf);
buf
}
pub fn decode(bytes: &[u8]) -> Result<GeneralSigning, ServiceError> {
if bytes.first() != Some(&GENERAL_SIGNING_TAG) {
return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
}
let mut pos = 1;
let (transaction_id, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (originator_system_title, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (recipient_system_title, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (date_time, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (other_information, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (content, n) = take_octet_string(&bytes[pos..])?;
pos += n;
let (signature, _) = take_octet_string(&bytes[pos..])?;
Ok(GeneralSigning {
transaction_id,
originator_system_title,
recipient_system_title,
date_time,
other_information,
content,
signature,
})
}
}
fn push_octet_string(bytes: &[u8], buf: &mut Vec<u8>) {
push_length(bytes.len(), buf);
buf.extend_from_slice(bytes);
}
fn take_octet_string(bytes: &[u8]) -> Result<(Vec<u8>, usize), ServiceError> {
let (len, header) = read_length(bytes)?;
let slice = bytes.get(header..header + len).ok_or(ServiceError::Truncated)?;
Ok((slice.to_vec(), header + len))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn general_glo_ciphering_round_trips() {
let apdu = GeneralGloDedCiphering {
dedicated: false,
system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x01],
ciphered_service: vec![0x30, 0x00, 0x00, 0x00, 0x01, 0xAA, 0xBB],
};
let bytes = apdu.encode();
assert_eq!(bytes[..2], [0xDB, 0x08]);
assert_eq!(bytes[10], 0x07);
assert_eq!(GeneralGloDedCiphering::decode(&bytes).unwrap(), apdu);
}
#[test]
fn general_ded_ciphering_tag() {
let apdu = GeneralGloDedCiphering {
dedicated: true,
system_title: vec![0x01; 8],
ciphered_service: vec![0x30, 0x00, 0x00, 0x00, 0x01],
};
assert_eq!(apdu.encode()[0], GENERAL_DED_CIPHERING_TAG);
assert_eq!(GeneralGloDedCiphering::decode(&apdu.encode()).unwrap(), apdu);
}
#[test]
fn general_ciphering_round_trips() {
let apdu = GeneralCiphering {
transaction_id: vec![0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45],
originator_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x01],
recipient_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0xBC, 0x61, 0x4E],
date_time: Vec::new(),
other_information: Vec::new(),
key_info: KeyInfo::None,
ciphered_content: vec![0x30, 0x00, 0x00, 0x00, 0x01, 0xCA, 0xFE],
};
let bytes = apdu.encode();
assert_eq!(bytes[0], 0xDD);
assert_eq!(GeneralCiphering::decode(&bytes).unwrap(), apdu);
}
#[test]
fn general_ciphering_with_agreed_key_round_trips() {
let apdu = GeneralCiphering {
transaction_id: vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
originator_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0xBC, 0x61, 0x4E],
recipient_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x01],
date_time: Vec::new(),
other_information: Vec::new(),
key_info: KeyInfo::AgreedKey {
key_parameters: vec![0x01], key_ciphered_data: vec![0xAA; 128], },
ciphered_content: vec![0x30, 0x00, 0x00, 0x00, 0x01, 0xCA, 0xFE],
};
let bytes = apdu.encode();
let ki = &bytes[30..];
assert_eq!(ki[..6], [0x01, 0x02, 0x01, 0x01, 0x81, 0x80]);
assert_eq!(GeneralCiphering::decode(&bytes).unwrap(), apdu);
}
#[test]
fn general_signing_round_trips() {
let apdu = GeneralSigning {
transaction_id: vec![0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45],
originator_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x01],
recipient_system_title: vec![0x4D, 0x4D, 0x4D, 0x00, 0x00, 0xBC, 0x61, 0x4E],
date_time: Vec::new(),
other_information: Vec::new(),
content: vec![0xC4, 0x01, 0xC1, 0x00, 0x12, 0x12, 0x34],
signature: vec![0xAB; 64],
};
let bytes = apdu.encode();
assert_eq!(bytes[0], 0xDF);
assert_eq!(GeneralSigning::decode(&bytes).unwrap(), apdu);
}
#[test]
fn general_ciphering_rejects_key_info() {
let mut bytes = GeneralCiphering {
transaction_id: vec![0; 8],
originator_system_title: vec![0; 8],
recipient_system_title: Vec::new(),
date_time: Vec::new(),
other_information: Vec::new(),
key_info: KeyInfo::None,
ciphered_content: vec![0x30],
}
.encode();
let flag = bytes.len() - 3;
bytes[flag] = 0x01; bytes.insert(flag + 1, 0x01); assert_eq!(GeneralCiphering::decode(&bytes), Err(ServiceError::InvalidData));
}
}