libsignal_service/
envelope.rs

1pub use crate::proto::Envelope;
2use libsignal_core::Pni;
3use libsignal_protocol::ServiceId;
4
5impl Envelope {
6    pub fn is_unidentified_sender(&self) -> bool {
7        self.r#type() == crate::proto::envelope::Type::UnidentifiedSender
8    }
9
10    pub fn is_prekey_signal_message(&self) -> bool {
11        self.r#type() == crate::proto::envelope::Type::PrekeyBundle
12    }
13
14    pub fn is_receipt(&self) -> bool {
15        self.r#type() == crate::proto::envelope::Type::ServerDeliveryReceipt
16    }
17
18    pub fn is_signal_message(&self) -> bool {
19        self.r#type() == crate::proto::envelope::Type::Ciphertext
20    }
21
22    pub fn is_urgent(&self) -> bool {
23        // SignalServiceEnvelopeEntity: return urgent == null || urgent;
24        self.urgent.unwrap_or(true)
25    }
26
27    pub fn is_story(&self) -> bool {
28        self.story.unwrap_or(false)
29    }
30
31    #[deprecated = "use parse_source_service_id"]
32    pub fn source_address(&self) -> ServiceId {
33        match self.source_service_id.as_deref() {
34            Some(service_id) => {
35                ServiceId::parse_from_service_id_string(service_id)
36                    .expect("invalid source ProtocolAddress UUID or prefix")
37            },
38            None => panic!("source_service_id is set"),
39        }
40    }
41
42    #[deprecated = "use parse_destination_service_id"]
43    pub fn destination_address(&self) -> ServiceId {
44        match self.destination_service_id.as_deref() {
45            Some(service_id) => ServiceId::parse_from_service_id_string(
46                service_id,
47            )
48            .expect("invalid destination ProtocolAddress UUID or prefix"),
49            None => panic!("destination_address is set"),
50        }
51    }
52
53    pub fn parse_destination_service_id(&self) -> Option<ServiceId> {
54        crate::utils::parse_service_id_with_fallback(
55            self.destination_service_id_binary.as_deref(),
56            self.destination_service_id.as_deref(),
57        )
58    }
59
60    pub fn parse_source_service_id(&self) -> Option<ServiceId> {
61        crate::utils::parse_service_id_with_fallback(
62            self.source_service_id_binary.as_deref(),
63            self.source_service_id.as_deref(),
64        )
65    }
66
67    pub fn parse_server_guid(&self) -> Option<uuid::Uuid> {
68        crate::utils::parse_uuid_with_fallback(
69            self.server_guid_binary.as_deref(),
70            self.server_guid.as_deref(),
71        )
72    }
73
74    pub fn parse_updated_pni(&self) -> Option<Pni> {
75        crate::utils::parse_pni_with_fallback(
76            self.updated_pni_binary.as_deref(),
77            self.updated_pni.as_deref(),
78            // XXX: this might have to be `false`, but can't find a ref (and it's for the string
79            // fallback anyway...
80            true,
81        )
82    }
83}
84
85pub(crate) const CIPHER_KEY_SIZE: usize = 32;
86
87pub(crate) const VERSION_OFFSET: usize = 0;
88pub(crate) const VERSION_LENGTH: usize = 1;
89pub(crate) const IV_OFFSET: usize = VERSION_OFFSET + VERSION_LENGTH;
90pub(crate) const IV_LENGTH: usize = 16;