Skip to main content

rustflexstack/facilities/decentralized_environmental_notification_service/
denm_ssp.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024 Fundació Privada Internet i Innovació Digital a Catalunya (i2CAT)
3
4//! DENM Service Specific Permissions (ETSI TS 103 831 V2.3.1 (2025-11),
5//! clause 6.2.2.2).
6//!
7//! The SSP of a DENM Authorization Ticket is a BitmapSsp: octet 0 is the SSP
8//! version, octets 1 to 4 hold one bit per cause code (Table 10). A DENM is
9//! consistent with the SSP when the bit of its cause code is set.
10//!
11//! Mirrors `flexstack/facilities/decentralized_environmental_notification_service/denm_ssp.py`.
12
13use super::denm_coder::{CauseCodeChoice, Denm};
14
15/// Octet index and bit mask of a cause code value (Table 10).
16pub fn cause_code_ssp_bit(cause_code: u8) -> Option<(usize, u8)> {
17    Some(match cause_code {
18        1 => (1, 0x80),
19        2 => (1, 0x40),
20        3 => (1, 0x20),
21        6 => (1, 0x10),
22        9 => (1, 0x08),
23        10 => (1, 0x04),
24        11 => (1, 0x02),
25        12 => (1, 0x01),
26        14 => (2, 0x80),
27        15 => (2, 0x40),
28        17 => (2, 0x20),
29        18 => (2, 0x10),
30        19 => (2, 0x08),
31        26 => (2, 0x04),
32        27 => (2, 0x02),
33        91 => (2, 0x01),
34        92 => (3, 0x80),
35        93 => (3, 0x40),
36        94 => (3, 0x20),
37        95 => (3, 0x10),
38        96 => (3, 0x08),
39        97 => (3, 0x04),
40        98 => (3, 0x02),
41        99 => (3, 0x01),
42        5 => (4, 0x80),
43        7 => (4, 0x40),
44        28 => (4, 0x20),
45        100 => (4, 0x10),
46        4 => (4, 0x08),
47        20 => (4, 0x04),
48        _ => return None,
49    })
50}
51
52/// Cause code value of a CauseCodeChoice, from the alternative name, which
53/// ends with the value (e.g. `accident2` -> 2), as in the Python stack.
54pub fn cause_code_of_choice(choice: &CauseCodeChoice) -> Option<u8> {
55    let debug = format!("{choice:?}");
56    let name = debug.split('(').next().unwrap_or("");
57    let digits: String = name
58        .chars()
59        .rev()
60        .take_while(|c| c.is_ascii_digit())
61        .collect::<Vec<_>>()
62        .into_iter()
63        .rev()
64        .collect();
65    digits.parse().ok()
66}
67
68/// Cause code value of a DENM; `None` for a DENM without situation container.
69pub fn cause_code_value(denm: &Denm) -> Option<u8> {
70    denm.denm
71        .situation
72        .as_ref()
73        .and_then(|s| cause_code_of_choice(&s.event_type.cc_and_scc))
74}
75
76/// True when the DENM SSP allows signing DENMs with `cause_code`. A cause
77/// code without a Table 10 bit, or whose octet lies beyond the SSP length, is
78/// not permitted.
79pub fn ssp_permits_cause_code(ssp: &[u8], cause_code: u8) -> bool {
80    match cause_code_ssp_bit(cause_code) {
81        Some((octet, mask)) => octet < ssp.len() && ssp[octet] & mask != 0,
82        None => false,
83    }
84}
85
86/// SSP check of the receiving ITS-S (clause 8.4.2): true when no SSP is
87/// available, when the DENM has no cause code (termination DENM), or when the
88/// SSP permits the cause code.
89pub fn ssp_consistent(ssp: Option<&[u8]>, denm: &Denm) -> bool {
90    match ssp {
91        None | Some([]) => true,
92        Some(ssp) => match cause_code_value(denm) {
93            None => true,
94            Some(cause) => ssp_permits_cause_code(ssp, cause),
95        },
96    }
97}
98
99/// SSP bits a DENM needs (Table 10), as the `permissions` of the signing
100/// request: the sign service only signs with an AT whose SSP grants them.
101/// Octet 0 (SSP version) is not requested. A DENM without cause code (a
102/// termination) or with a cause code without Table 10 bit requests none.
103pub fn permissions_bitmap(denm: &Denm) -> Vec<u8> {
104    match cause_code_value(denm).and_then(cause_code_ssp_bit) {
105        Some((octet, mask)) => {
106            let mut bitmap = vec![0u8; octet + 1];
107            bitmap[octet] = mask;
108            bitmap
109        }
110        None => vec![0],
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::super::denm_coder::{AccidentSubCauseCode, CauseCodeChoice};
117    use super::*;
118
119    #[test]
120    fn bits_of_table_10() {
121        let ssp = [2u8, 0x40, 0x00, 0x10, 0x00]; // accident(2), emergencyVehicleApproaching(95)
122        assert!(ssp_permits_cause_code(&ssp, 2));
123        assert!(ssp_permits_cause_code(&ssp, 95));
124        assert!(!ssp_permits_cause_code(&ssp, 1));
125        assert!(!ssp_permits_cause_code(&ssp, 99));
126    }
127
128    #[test]
129    fn release_2_code_needs_version_2_ssp() {
130        let version_1 = [1u8, 0xFF, 0xFF, 0xFF];
131        assert!(ssp_permits_cause_code(&version_1, 99));
132        assert!(!ssp_permits_cause_code(&version_1, 5)); // octet 4
133        assert!(ssp_permits_cause_code(&[1u8, 0xFF, 0xFF, 0xFF, 0x80], 5));
134    }
135
136    #[test]
137    fn cause_code_from_choice_name() {
138        let choice = CauseCodeChoice::accident2(AccidentSubCauseCode(3));
139        assert_eq!(cause_code_of_choice(&choice), Some(2));
140    }
141}