Skip to main content

rustflexstack/security/
sn_sap.rs

1//! SN-SAP — Security Network Service Access Point.
2//!
3//! Data types for the SN-SIGN and SN-VERIFY primitives as specified in
4//! ETSI TS 102 723-8 V1.1.1 (2016-04).
5
6/// SN-SIGN.request
7#[derive(Debug, Clone)]
8pub struct SNSignRequest {
9    pub tbs_message: Vec<u8>,
10    pub its_aid: u64,
11    pub permissions: Vec<u8>,
12    /// Optional 3-D location for DENM headerInfo.generationLocation.
13    pub generation_location: Option<GenerationLocation>,
14}
15
16impl SNSignRequest {
17    pub fn new(
18        tbs_message: Vec<u8>,
19        its_aid: u64,
20        permissions: Vec<u8>,
21        generation_location: Option<GenerationLocation>,
22    ) -> Self {
23        Self {
24            tbs_message,
25            its_aid,
26            permissions,
27            generation_location,
28        }
29    }
30
31    pub fn tbs_message_length(&self) -> usize {
32        self.tbs_message.len()
33    }
34
35    pub fn permissions_length(&self) -> usize {
36        self.permissions.len()
37    }
38}
39
40/// 3-D location embedded in signed message headers.
41#[derive(Debug, Clone)]
42pub struct GenerationLocation {
43    pub latitude: i32,
44    pub longitude: i32,
45    pub elevation: u16,
46}
47
48/// SN-SIGN.confirm
49#[derive(Debug, Clone)]
50pub struct SNSignConfirm {
51    pub sec_message: Vec<u8>,
52}
53
54impl SNSignConfirm {
55    pub fn new(sec_message: Vec<u8>) -> Self {
56        Self { sec_message }
57    }
58
59    pub fn sec_message_length(&self) -> usize {
60        self.sec_message.len()
61    }
62}
63
64/// SN-VERIFY.request
65#[derive(Debug, Clone)]
66pub struct SNVerifyRequest {
67    pub message: Vec<u8>,
68}
69
70/// Verification outcome codes (Table 5, §5.2.2.3).
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72#[repr(u8)]
73pub enum ReportVerify {
74    Success,
75    FalseSignature,
76    InvalidCertificate,
77    RevokedCertificate,
78    InconsistentChain,
79    InvalidTimestamp,
80    DuplicateMessage,
81    InvalidMobilityData,
82    UnsignedMessage,
83    SignerCertificateNotFound,
84    UnsupportedSignerIdentifierType,
85    IncompatibleProtocol,
86}
87
88impl ReportVerify {
89    /// Numeric value of the report (Table 5), as carried in the GN/BTP indications.
90    pub fn code(&self) -> u8 {
91        *self as u8
92    }
93}
94
95/// SN-VERIFY.confirm
96#[derive(Debug, Clone)]
97pub struct SNVerifyConfirm {
98    pub report: ReportVerify,
99    pub certificate_id: Vec<u8>,
100    pub its_aid: u64,
101    pub permissions: Vec<u8>,
102    /// The verified plain-text payload extracted from the signed message.
103    pub plain_message: Vec<u8>,
104}
105
106/// Security profile selector (mirrors the Python SecurityProfile enum).
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum SecurityProfile {
109    /// Parameter not set: with itsGnSecurity ENABLED the default profile signs
110    /// (ETSI TS 103 836-4-1 V2.2.1, NOTE of table 37).
111    NoSecurity = 0,
112    CooperativeAwarenessMessage = 1,
113    DecentralizedEnvironmentalNotificationMessage = 2,
114    VruAwarenessMessage = 3,
115    /// Unsecured packet (NH = Common Header, table 18) even with itsGnSecurity
116    /// ENABLED: the CRL / CTL broadcast, whose SM-PDU is signed by its issuer
117    /// (ETSI TS 103 601 V2.1.1 clause 5.4.2).
118    Unsecured = 4,
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn security_profile_values() {
127        assert_eq!(SecurityProfile::NoSecurity as u8, 0);
128        assert_eq!(SecurityProfile::CooperativeAwarenessMessage as u8, 1);
129        assert_eq!(
130            SecurityProfile::DecentralizedEnvironmentalNotificationMessage as u8,
131            2
132        );
133        assert_eq!(SecurityProfile::VruAwarenessMessage as u8, 3);
134    }
135
136    #[test]
137    fn security_profile_equality() {
138        assert_eq!(SecurityProfile::NoSecurity, SecurityProfile::NoSecurity);
139        assert_ne!(
140            SecurityProfile::NoSecurity,
141            SecurityProfile::CooperativeAwarenessMessage
142        );
143    }
144
145    #[test]
146    fn report_verify_variants() {
147        let variants = vec![
148            ReportVerify::Success,
149            ReportVerify::FalseSignature,
150            ReportVerify::InvalidCertificate,
151            ReportVerify::RevokedCertificate,
152            ReportVerify::InconsistentChain,
153            ReportVerify::InvalidTimestamp,
154            ReportVerify::DuplicateMessage,
155            ReportVerify::InvalidMobilityData,
156            ReportVerify::UnsignedMessage,
157            ReportVerify::SignerCertificateNotFound,
158            ReportVerify::UnsupportedSignerIdentifierType,
159            ReportVerify::IncompatibleProtocol,
160        ];
161        assert_eq!(variants.len(), 12);
162    }
163
164    #[test]
165    fn report_verify_equality() {
166        assert_eq!(ReportVerify::Success, ReportVerify::Success);
167        assert_ne!(ReportVerify::Success, ReportVerify::FalseSignature);
168    }
169
170    #[test]
171    fn sn_sign_request_construction() {
172        let req = SNSignRequest {
173            tbs_message: vec![1, 2, 3],
174            its_aid: 36,
175            permissions: vec![0xFF],
176            generation_location: Some(GenerationLocation {
177                latitude: 415520000,
178                longitude: 21340000,
179                elevation: 1200,
180            }),
181        };
182        assert_eq!(req.its_aid, 36);
183        assert!(req.generation_location.is_some());
184    }
185
186    #[test]
187    fn sn_sign_request_no_location() {
188        let req = SNSignRequest {
189            tbs_message: vec![],
190            its_aid: 37,
191            permissions: vec![],
192            generation_location: None,
193        };
194        assert!(req.generation_location.is_none());
195    }
196
197    #[test]
198    fn sn_verify_confirm_construction() {
199        let confirm = SNVerifyConfirm {
200            report: ReportVerify::Success,
201            certificate_id: vec![1, 2, 3, 4, 5, 6, 7, 8],
202            its_aid: 36,
203            permissions: vec![0xFF],
204            plain_message: vec![0xCA, 0xFE],
205        };
206        assert_eq!(confirm.report, ReportVerify::Success);
207        assert_eq!(confirm.plain_message.len(), 2);
208    }
209
210    #[test]
211    fn generation_location_fields() {
212        let loc = GenerationLocation {
213            latitude: 415520000,
214            longitude: 21340000,
215            elevation: 0xF000,
216        };
217        assert_eq!(loc.latitude, 415520000);
218        assert_eq!(loc.longitude, 21340000);
219        assert_eq!(loc.elevation, 0xF000);
220    }
221}