use jiff::civil::DateTime;
use crate::codec::defs::common::Kik1;
use crate::codec::defs::security::{
EncryptionAlgorithm2, HashAlgorithm2, KeyName3, SecurityIdentification2, SecurityProfile1,
SecurityTimestamp2, SignatureAlgorithm2, UserDefinedSignature1,
};
use crate::codec::defs::{HnHbk3, HnHbs1, HnSha2, HnShk4, HnVsd1, HnVsk3};
use crate::codec::{self, An, Bin, Code, Dat, Dig, Id, Num, RawSegment, Segment as _, Tim};
use crate::error::FinTSError;
use crate::protocol::{BankParams, Segment};
use crate::segments::verbatim;
const HNVSK_NUMBER: u16 = 998;
const HNVSD_NUMBER: u16 = 999;
const FIRST_INNER_NUMBER: u16 = 2;
pub(crate) struct SecurityContext<'a> {
pub dialog_id: &'a str,
pub message_number: u16,
pub blz: &'a str,
pub user_id: &'a str,
pub system_id: &'a str,
pub pin: &'a str,
pub tan: Option<&'a str>,
pub security_function: &'a str,
}
fn profile_version(security_function: &str) -> u64 {
match security_function.parse::<u16>() {
Ok(sf) if sf >= 998 => 1,
_ => 2,
}
}
fn profile(security_function: &str) -> SecurityProfile1 {
SecurityProfile1 {
method: Code::new("PIN").expect("constant"),
version: Num::new(profile_version(security_function)),
}
}
fn code(value: &str) -> Code {
Code::new(value).expect("constant code")
}
fn serialize_error(segment: &str) -> impl Fn(codec::CodecError) -> FinTSError + '_ {
move |e| FinTSError::Serialize(format!("{segment}: {e}"))
}
fn identification(system_id: &str) -> Result<SecurityIdentification2, FinTSError> {
Ok(SecurityIdentification2 {
party: code("1"),
cid: None,
party_id: if system_id.is_empty() {
None
} else {
Some(verbatim::<Id>(system_id, "system ID")?)
},
})
}
fn timestamp(at: DateTime) -> Result<SecurityTimestamp2, FinTSError> {
let invalid = |_| FinTSError::Serialize("security timestamp out of range".into());
Ok(SecurityTimestamp2 {
kind: code("1"),
date: Some(Dat::new(at.date()).map_err(invalid)?),
time: Some(Tim::new(at.time()).map_err(invalid)?),
})
}
fn key_name(ctx: &SecurityContext<'_>, key_type: &str) -> Result<KeyName3, FinTSError> {
Ok(KeyName3 {
bank: Kik1 {
country: Dig::new("280").expect("constant"),
bank_code: if ctx.blz.is_empty() {
None
} else {
Some(verbatim::<An>(ctx.blz, "bank code")?)
},
},
user_id: verbatim::<Id>(ctx.user_id, "user ID")?,
key_type: code(key_type),
key_number: Num::new(0),
key_version: Num::new(0),
})
}
pub(crate) fn hnshk(
ctx: &SecurityContext<'_>,
control_reference: u32,
at: DateTime,
) -> Result<HnShk4, FinTSError> {
Ok(HnShk4 {
profile: profile(ctx.security_function),
security_function: verbatim::<Code>(ctx.security_function, "security function")?,
control_reference: An::new(control_reference.to_string()).expect("digits"),
application_area: code("1"),
role: code("1"),
identification: identification(ctx.system_id)?,
reference_number: Num::new(1),
timestamp: timestamp(at)?,
hash_algorithm: HashAlgorithm2 {
usage: code("1"),
algorithm: code("999"),
parameter_name: code("1"),
parameter_value: None,
},
signature_algorithm: SignatureAlgorithm2 {
usage: code("6"),
algorithm: code("10"),
mode: code("16"),
},
key_name: key_name(ctx, "S")?,
certificate: None,
})
}
pub(crate) fn hnvsk(ctx: &SecurityContext<'_>, at: DateTime) -> Result<HnVsk3, FinTSError> {
Ok(HnVsk3 {
profile: profile(ctx.security_function),
security_function: code("998"),
role: code("1"),
identification: identification(ctx.system_id)?,
timestamp: timestamp(at)?,
encryption_algorithm: EncryptionAlgorithm2 {
usage: code("2"),
mode: code("2"),
algorithm: code("13"),
key_value: Bin(vec![0u8; 8]),
key_parameter_name: code("5"),
iv_parameter_name: code("1"),
iv_value: None,
},
key_name: key_name(ctx, "V")?,
compression: code("0"),
certificate: None,
})
}
pub(crate) fn hnsha(
ctx: &SecurityContext<'_>,
control_reference: u32,
) -> Result<HnSha2, FinTSError> {
let credential = |what: &str| {
let what = what.to_string();
move |_| {
FinTSError::Serialize(format!(
"HNSHA: {what} cannot be sent (ISO 8859-1 text, no line breaks)"
))
}
};
Ok(HnSha2 {
control_reference: An::new(control_reference.to_string()).expect("digits"),
validation_result: None,
user_signature: Some(UserDefinedSignature1 {
pin: verbatim::<An>(ctx.pin, "PIN").map_err(credential("PIN"))?,
tan: ctx
.tan
.filter(|t| !t.is_empty())
.map(|t| verbatim::<An>(t, "TAN").map_err(credential("TAN")))
.transpose()?,
}),
})
}
pub(crate) fn build_from_segments(
ctx: &SecurityContext<'_>,
segments: &[Segment],
params: &BankParams,
at: DateTime,
) -> Result<Vec<u8>, FinTSError> {
let business: Vec<RawSegment> = segments
.iter()
.zip(FIRST_INNER_NUMBER + 1..)
.map(|(s, number)| s.encode(params, number))
.collect::<Result<_, _>>()?;
let control_reference = rand::random_range(1_000_000u32..10_000_000u32);
assemble(ctx, &business, at, control_reference)
}
pub(crate) fn assemble(
ctx: &SecurityContext<'_>,
business: &[RawSegment],
at: DateTime,
control_reference: u32,
) -> Result<Vec<u8>, FinTSError> {
let count = u16::try_from(business.len())
.map_err(|_| FinTSError::Serialize("too many segments in one message".into()))?;
let hnsha_number = FIRST_INNER_NUMBER + 1 + count;
let trailer_number = hnsha_number + 1;
let mut inner = Vec::new();
inner.extend(
codec::to_wire(
&hnshk(ctx, control_reference, at)?,
FIRST_INNER_NUMBER,
None,
)
.map_err(serialize_error("HNSHK"))?,
);
for seg in business {
inner.extend(codec::wire::serialize_segment(seg).map_err(serialize_error("segment"))?);
}
inner.extend(
codec::to_wire(&hnsha(ctx, control_reference)?, hnsha_number, None)
.map_err(|_| FinTSError::Serialize("HNSHA cannot be encoded".into()))?,
);
let mut body = Vec::new();
body.extend(
codec::to_wire(&hnvsk(ctx, at)?, HNVSK_NUMBER, None).map_err(serialize_error("HNVSK"))?,
);
body.extend(
codec::to_wire(&HnVsd1 { data: Bin(inner) }, HNVSD_NUMBER, None)
.map_err(serialize_error("HNVSD"))?,
);
body.extend(
codec::to_wire(
&HnHbs1 {
message_number: Num::new(u64::from(ctx.message_number)),
},
trailer_number,
None,
)
.map_err(serialize_error("HNHBS"))?,
);
let header = |size: u64| -> Result<Vec<u8>, FinTSError> {
let hnhbk = HnHbk3 {
message_size: Dig::zero_padded(size, 12),
hbci_version: Num::new(300),
dialog_id: verbatim::<Id>(ctx.dialog_id, "dialog ID")?,
message_number: Num::new(u64::from(ctx.message_number)),
reference_message: None,
};
codec::to_wire(&hnhbk, 1, None).map_err(serialize_error(HnHbk3::CODE))
};
let total = header(0)?.len() + body.len();
let mut message = header(total as u64)?;
if message.len() + body.len() != total {
return Err(FinTSError::Serialize(
"HNHBK size changed after size update".into(),
));
}
message.extend(body);
Ok(message)
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx<'a>(security_function: &'a str, tan: Option<&'a str>) -> SecurityContext<'a> {
SecurityContext {
dialog_id: "0",
message_number: 1,
blz: "12030000",
user_id: "testuser",
system_id: "0",
pin: "12345",
tan,
security_function,
}
}
fn at() -> DateTime {
jiff::civil::date(2026, 1, 15).at(10, 15, 0, 0)
}
fn hkidn() -> RawSegment {
codec::wire::tokenize(b"HKIDN:3:2+280:12030000+testuser+0+1'")
.unwrap()
.remove(0)
}
fn text(bytes: &[u8]) -> String {
bytes.iter().map(|&b| char::from(b)).collect()
}
#[test]
fn two_step_message_wire_format() {
let msg = assemble(&ctx("921", None), &[hkidn()], at(), 4711).unwrap();
let inner = "HNSHK:2:4+PIN:2+921+4711+1+1+1::0+1+1:20260115:101500+1:999:1+6:10:16+280:12030000:testuser:S:0:0'\
HKIDN:3:2+280:12030000+testuser+0+1'\
HNSHA:4:2+4711++12345'";
let body = format!(
"HNVSK:998:3+PIN:2+998+1+1::0+1:20260115:101500+2:2:13:@8@{}:5:1+280:12030000:testuser:V:0:0+0'\
HNVSD:999:1+@{}@{inner}'HNHBS:5:1+1'",
"\0".repeat(8),
inner.len()
);
let header = format!("HNHBK:1:3+{:012}+300+0+1'", 31 + body.len());
assert_eq!(text(&msg), format!("{header}{body}"));
}
#[test]
fn one_step_message_uses_profile_1_and_tan_follows_the_pin() {
let msg = text(&assemble(&ctx("999", Some("654321")), &[hkidn()], at(), 4711).unwrap());
assert!(msg.contains("HNVSK:998:3+PIN:1+998+1+1::0+"), "{msg}");
assert!(msg.contains("HNSHK:2:4+PIN:1+999+4711+"), "{msg}");
assert!(msg.contains("HNSHA:4:2+4711++12345:654321'"), "{msg}");
let msg = text(&assemble(&ctx("999", Some("")), &[hkidn()], at(), 4711).unwrap());
assert!(msg.contains("HNSHA:4:2+4711++12345'"), "{msg}");
}
#[test]
fn order_tan_message_wire_format() {
let hktan = codec::wire::tokenize(b"HKTAN:3:6+2++++TASK0001+N'")
.unwrap()
.remove(0);
let msg = text(&assemble(&ctx("921", Some("123456")), &[hktan], at(), 4711).unwrap());
assert!(msg.contains("HNSHK:2:4+PIN:2+921+4711+"), "{msg}");
assert!(
msg.contains("'HKTAN:3:6+2++++TASK0001+N'HNSHA:4:2+4711++12345:123456'"),
"{msg}"
);
}
#[test]
fn outer_segments_and_numbers() {
let msg = assemble(&ctx("999", None), &[hkidn()], at(), 4711).unwrap();
let outer: Vec<String> = codec::wire::tokenize(&msg)
.unwrap()
.iter()
.map(|s| format!("{}:{}", s.segment_type(), s.segment_number()))
.collect();
assert_eq!(outer, ["HNHBK:1", "HNVSK:998", "HNVSD:999", "HNHBS:5"]);
}
#[test]
fn test_message_size_in_header() {
let msg = assemble(&ctx("999", None), &[hkidn()], at(), 4711).unwrap();
let (_, hnhbk) =
codec::decode_segment::<HnHbk3>(&codec::wire::tokenize(&msg).unwrap()[0]).unwrap();
assert_eq!(hnhbk.message_size.value(), Some(msg.len() as u64));
}
#[test]
fn credentials_stay_out_of_errors() {
let mut c = ctx("999", Some("12\n34"));
let err = assemble(&c, &[hkidn()], at(), 4711).unwrap_err();
assert!(!err.to_string().contains("12\n34"), "{err}");
c.tan = None;
c.pin = "pin\u{20ac}";
let err = assemble(&c, &[hkidn()], at(), 4711).unwrap_err();
assert!(!err.to_string().contains('\u{20ac}'), "{err}");
}
#[test]
fn non_numeric_security_function_does_not_panic() {
let msg = text(&assemble(&ctx("X", None), &[hkidn()], at(), 4711).unwrap());
assert!(msg.contains("HNSHK:2:4+PIN:2+X+"), "{msg}");
}
}