Skip to content

Commit

Permalink
Update base64 dependency
Browse files Browse the repository at this point in the history
We could in theory support this code with base64 = 0.20 too
  • Loading branch information
rubdos committed Jan 9, 2024
1 parent 036de0d commit fe3c224
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 39 deletions.
1 change: 0 additions & 1 deletion libsignal-service-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ rand = "0.8"

thiserror = "1.0"
async-trait = "0.1"
base64 = "0.13"

phonenumber = "0.3"

Expand Down
2 changes: 1 addition & 1 deletion libsignal-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ aes-gcm = "0.10"
cbc = "0.1"
ctr = "0.9"
async-trait = "0.1"
base64 = "0.13"
base64 = "0.21"
bincode = "1.3"
bytes = "1"
chrono = { version = "0.4", features = ["serde", "clock"], default-features = false }
Expand Down
31 changes: 19 additions & 12 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::prelude::*;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::time::SystemTime;
Expand Down Expand Up @@ -293,7 +294,7 @@ impl<Service: PushService> AccountManager<Service> {
&[],
HttpAuthOverride::NoOverride,
&ProvisioningMessage {
body: base64::encode(body),
body: BASE64_STANDARD.encode(body),
},
)
.await
Expand Down Expand Up @@ -322,7 +323,8 @@ impl<Service: PushService> AccountManager<Service> {
let ephemeral_id = query.get("uuid").ok_or(LinkError::InvalidUuid)?;
let pub_key =
query.get("pub_key").ok_or(LinkError::InvalidPublicKey)?;
let pub_key = base64::decode(&**pub_key)
let pub_key = BASE64_STANDARD
.decode(&**pub_key)
.map_err(|_e| LinkError::InvalidPublicKey)?;
let pub_key = PublicKey::deserialize(&pub_key)
.map_err(|_e| LinkError::InvalidPublicKey)?;
Expand Down Expand Up @@ -653,19 +655,24 @@ mod tests {

#[test]
fn decrypt_device_name() -> anyhow::Result<()> {
let ephemeral_private_key = PrivateKey::deserialize(&base64::decode(
"0CgxHjwwblXjvX8sD5wZDWdYToMRf+CZSlgaUrxCGVo=",
)?)?;
let ephemeral_public_key = PublicKey::deserialize(&base64::decode(
"BcZS+Lt6yAKbEpXnRX+I5wHqesuvu93Q2V+fjidwW8R6",
)?)?;
let ephemeral_private_key = PrivateKey::deserialize(
&BASE64_STANDARD
.decode("0CgxHjwwblXjvX8sD5wZDWdYToMRf+CZSlgaUrxCGVo=")?,
)?;
let ephemeral_public_key = PublicKey::deserialize(
&BASE64_STANDARD
.decode("BcZS+Lt6yAKbEpXnRX+I5wHqesuvu93Q2V+fjidwW8R6")?,
)?;

let device_name = DeviceName {
ephemeral_public: Some(ephemeral_public_key.serialize().to_vec()),
synthetic_iv: Some(base64::decode("86gekHGmltnnZ9QARhiFcg==")?),
ciphertext: Some(base64::decode(
"MtJ9/9KBWLBVAxfZJD4pLKzP4q+iodRJeCc+/A==",
)?),
synthetic_iv: Some(
BASE64_STANDARD.decode("86gekHGmltnnZ9QARhiFcg==")?,
),
ciphertext: Some(
BASE64_STANDARD
.decode("MtJ9/9KBWLBVAxfZJD4pLKzP4q+iodRJeCc+/A==")?,
),
};

let decrypted_device_name =
Expand Down
5 changes: 3 additions & 2 deletions libsignal-service/src/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{convert::TryFrom, fmt, time::SystemTime};

use aes::cipher::block_padding::{Iso7816, RawPadding};
use base64::prelude::*;
use libsignal_protocol::{
group_decrypt, message_decrypt_prekey, message_decrypt_signal,
message_encrypt, process_sender_key_distribution_message,
Expand Down Expand Up @@ -357,7 +358,7 @@ where
r#type: Type::UnidentifiedSender as u32,
destination_device_id: address.device_id().into(),
destination_registration_id,
content: base64::encode(message),
content: BASE64_STANDARD.encode(message),
})
} else {
let message = message_encrypt(
Expand All @@ -372,7 +373,7 @@ where
let destination_registration_id =
session_record.remote_registration_id()?;

let body = base64::encode(message.serialize());
let body = BASE64_STANDARD.encode(message.serialize());

use crate::proto::envelope::Type;
let message_type = match message.message_type() {
Expand Down
9 changes: 5 additions & 4 deletions libsignal-service/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::HashMap, str::FromStr};

use base64::prelude::*;
use libsignal_protocol::PublicKey;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -128,8 +129,8 @@ impl From<&SignalServers> for ServiceConfiguration {
"https://api-staging.directory.signal.org".parse().unwrap(),
certificate_authority: include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/certs/staging-root-ca.pem")).to_string(),
unidentified_sender_trust_root:
PublicKey::deserialize(&base64::decode("BbqY1DzohE4NUZoVF+L18oUPrK3kILllLEJh2UnPSsEx").unwrap()).unwrap(),
zkgroup_server_public_params: bincode::deserialize(&base64::decode("ABSY21VckQcbSXVNCGRYJcfWHiAMZmpTtTELcDmxgdFbtp/bWsSxZdMKzfCp8rvIs8ocCU3B37fT3r4Mi5qAemeGeR2X+/YmOGR5ofui7tD5mDQfstAI9i+4WpMtIe8KC3wU5w3Inq3uNWVmoGtpKndsNfwJrCg0Hd9zmObhypUnSkfYn2ooMOOnBpfdanRtrvetZUayDMSC5iSRcXKpdlukrpzzsCIvEwjwQlJYVPOQPj4V0F4UXXBdHSLK05uoPBCQG8G9rYIGedYsClJXnbrgGYG3eMTG5hnx4X4ntARBgELuMWWUEEfSK0mjXg+/2lPmWcTZWR9nkqgQQP0tbzuiPm74H2wMO4u1Wafe+UwyIlIT9L7KLS19Aw8r4sPrXZSSsOZ6s7M1+rTJN0bI5CKY2PX29y5Ok3jSWufIKcgKOnWoP67d5b2du2ZVJjpjfibNIHbT/cegy/sBLoFwtHogVYUewANUAXIaMPyCLRArsKhfJ5wBtTminG/PAvuBdJ70Z/bXVPf8TVsR292zQ65xwvWTejROW6AZX6aqucUj").unwrap()).unwrap(),
PublicKey::deserialize(&BASE64_STANDARD.decode("BbqY1DzohE4NUZoVF+L18oUPrK3kILllLEJh2UnPSsEx").unwrap()).unwrap(),
zkgroup_server_public_params: bincode::deserialize(&BASE64_STANDARD.decode("ABSY21VckQcbSXVNCGRYJcfWHiAMZmpTtTELcDmxgdFbtp/bWsSxZdMKzfCp8rvIs8ocCU3B37fT3r4Mi5qAemeGeR2X+/YmOGR5ofui7tD5mDQfstAI9i+4WpMtIe8KC3wU5w3Inq3uNWVmoGtpKndsNfwJrCg0Hd9zmObhypUnSkfYn2ooMOOnBpfdanRtrvetZUayDMSC5iSRcXKpdlukrpzzsCIvEwjwQlJYVPOQPj4V0F4UXXBdHSLK05uoPBCQG8G9rYIGedYsClJXnbrgGYG3eMTG5hnx4X4ntARBgELuMWWUEEfSK0mjXg+/2lPmWcTZWR9nkqgQQP0tbzuiPm74H2wMO4u1Wafe+UwyIlIT9L7KLS19Aw8r4sPrXZSSsOZ6s7M1+rTJN0bI5CKY2PX29y5Ok3jSWufIKcgKOnWoP67d5b2du2ZVJjpjfibNIHbT/cegy/sBLoFwtHogVYUewANUAXIaMPyCLRArsKhfJ5wBtTminG/PAvuBdJ70Z/bXVPf8TVsR292zQ65xwvWTejROW6AZX6aqucUj").unwrap()).unwrap(),
},
// configuration with the Signal API production endpoints
// https://github.com/signalapp/Signal-Desktop/blob/master/config/production.json
Expand All @@ -146,9 +147,9 @@ impl From<&SignalServers> for ServiceConfiguration {
contact_discovery_url: "https://api.directory.signal.org".parse().unwrap(),
certificate_authority: include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/certs/production-root-ca.pem")).to_string(),
unidentified_sender_trust_root:
PublicKey::deserialize(&base64::decode("BXu6QIKVz5MA8gstzfOgRQGqyLqOwNKHL6INkv3IHWMF").unwrap()).unwrap(),
PublicKey::deserialize(&BASE64_STANDARD.decode("BXu6QIKVz5MA8gstzfOgRQGqyLqOwNKHL6INkv3IHWMF").unwrap()).unwrap(),
zkgroup_server_public_params: bincode::deserialize(
&base64::decode("AMhf5ywVwITZMsff/eCyudZx9JDmkkkbV6PInzG4p8x3VqVJSFiMvnvl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MLWcQ/gv0E4aONNqs4P").unwrap()).unwrap(),
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion libsignal-service/src/groups_v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
push_service::{HttpAuth, HttpAuthOverride, ServiceIds},
};

use base64::prelude::*;
use bytes::Bytes;
use chrono::{Days, NaiveDate, NaiveTime, Utc};
use futures::AsyncReadExt;
Expand Down Expand Up @@ -40,7 +41,7 @@ impl CredentialResponse {
self.credentials
.into_iter()
.map(|c| {
let bytes = base64::decode(c.credential)?;
let bytes = BASE64_STANDARD.decode(c.credential)?;
let data = bincode::deserialize(&bytes)?;
Ok((c.redemption_time, data))
})
Expand Down
7 changes: 4 additions & 3 deletions libsignal-service/src/groups_v2/operations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::TryInto;

use base64::prelude::*;
use bytes::Bytes;
use libsignal_protocol::{Aci, ServiceId};
use prost::Message;
Expand Down Expand Up @@ -402,9 +403,9 @@ impl GroupOperations {

let modify_invite_link_password =
actions.modify_invite_link_password.into_iter().map(|m| {
Ok(GroupChange::InviteLinkPassword(base64::encode(
m.invite_link_password,
)))
Ok(GroupChange::InviteLinkPassword(
BASE64_STANDARD.encode(m.invite_link_password),
))
});

let modify_announcements_only = actions
Expand Down
3 changes: 2 additions & 1 deletion libsignal-service/src/provisioning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{array::TryFromSliceError, borrow::Cow};

pub use cipher::ProvisioningCipher;

use base64::prelude::*;
use derivative::Derivative;
use futures::StreamExt;
use futures::{channel::mpsc::Sender, pin_mut, SinkExt};
Expand Down Expand Up @@ -221,7 +222,7 @@ pub async fn link_device<
let pni_signed_pre_key =
generate_signed_pre_key(pni_store, csprng, &pni_key_pair).await?;

let encrypted_device_name = base64::encode(
let encrypted_device_name = BASE64_STANDARD.encode(
encrypt_device_name(csprng, device_name, &aci_public_key)?
.encode_to_vec(),
);
Expand Down
3 changes: 2 additions & 1 deletion libsignal-service/src/provisioning/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::prelude::*;
use bytes::Bytes;
use futures::{
channel::{
Expand Down Expand Up @@ -99,7 +100,7 @@ impl ProvisioningPipe {
.append_pair("uuid", &uuid.uuid.unwrap())
.append_pair(
"pub_key",
&base64::encode(
&BASE64_STANDARD.encode(
self.provisioning_cipher.public_key().serialize(),
),
);
Expand Down
37 changes: 26 additions & 11 deletions libsignal-service/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
pub mod serde_base64 {
use base64::prelude::*;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<T, S>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: AsRef<[u8]>,
S: Serializer,
{
serializer.serialize_str(&base64::encode(bytes.as_ref()))
serializer.serialize_str(&BASE64_STANDARD.encode(bytes.as_ref()))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
Expand All @@ -15,12 +16,15 @@ pub mod serde_base64 {
{
use serde::de::Error;
String::deserialize(deserializer).and_then(|string| {
base64::decode(string).map_err(|err| Error::custom(err.to_string()))
BASE64_STANDARD
.decode(string)
.map_err(|err| Error::custom(err.to_string()))
})
}
}

pub mod serde_optional_base64 {
use base64::prelude::*;
use serde::{Deserialize, Deserializer, Serializer};

use super::serde_base64;
Expand All @@ -47,7 +51,8 @@ pub mod serde_optional_base64 {
{
use serde::de::Error;
match Option::<String>::deserialize(deserializer)? {
Some(s) => base64::decode(s)
Some(s) => BASE64_STANDARD
.decode(s)
.map_err(|err| Error::custom(err.to_string()))
.map(Some),
None => Ok(None),
Expand All @@ -56,6 +61,7 @@ pub mod serde_optional_base64 {
}

pub mod serde_public_key {
use base64::prelude::*;
use libsignal_protocol::PublicKey;
use serde::{Deserialize, Deserializer, Serializer};

Expand All @@ -67,22 +73,24 @@ pub mod serde_public_key {
S: Serializer,
{
let public_key = public_key.serialize();
serializer.serialize_str(&base64::encode(&public_key))
serializer.serialize_str(&BASE64_STANDARD.encode(&public_key))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<PublicKey, D::Error>
where
D: Deserializer<'de>,
{
PublicKey::deserialize(
&base64::decode(String::deserialize(deserializer)?)
&BASE64_STANDARD
.decode(String::deserialize(deserializer)?)
.map_err(serde::de::Error::custom)?,
)
.map_err(serde::de::Error::custom)
}
}

pub mod serde_optional_public_key {
use base64::prelude::*;
use libsignal_protocol::PublicKey;
use serde::{Deserialize, Deserializer, Serializer};

Expand Down Expand Up @@ -112,7 +120,8 @@ pub mod serde_optional_public_key {
match Option::<String>::deserialize(deserializer)? {
Some(public_key) => Ok(Some(
PublicKey::deserialize(
&base64::decode(public_key)
&BASE64_STANDARD
.decode(public_key)
.map_err(serde::de::Error::custom)?,
)
.map_err(serde::de::Error::custom)?,
Expand All @@ -123,6 +132,7 @@ pub mod serde_optional_public_key {
}

pub mod serde_private_key {
use base64::prelude::*;
use libsignal_protocol::PrivateKey;
use serde::{Deserialize, Deserializer, Serializer};

Expand All @@ -134,22 +144,24 @@ pub mod serde_private_key {
S: Serializer,
{
let public_key = public_key.serialize();
serializer.serialize_str(&base64::encode(public_key))
serializer.serialize_str(&BASE64_STANDARD.encode(public_key))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<PrivateKey, D::Error>
where
D: Deserializer<'de>,
{
PrivateKey::deserialize(
&base64::decode(String::deserialize(deserializer)?)
&BASE64_STANDARD
.decode(String::deserialize(deserializer)?)
.map_err(serde::de::Error::custom)?,
)
.map_err(serde::de::Error::custom)
}
}

pub mod serde_optional_private_key {
use base64::prelude::*;
use libsignal_protocol::PrivateKey;
use serde::{Deserialize, Deserializer, Serializer};

Expand Down Expand Up @@ -179,7 +191,8 @@ pub mod serde_optional_private_key {
match Option::<String>::deserialize(deserializer)? {
Some(private_key) => Ok(Some(
PrivateKey::deserialize(
&base64::decode(private_key)
&BASE64_STANDARD
.decode(private_key)
.map_err(serde::de::Error::custom)?,
)
.map_err(serde::de::Error::custom)?,
Expand All @@ -193,6 +206,7 @@ pub mod serde_signaling_key {
use std::convert::TryInto;

use crate::configuration::SignalingKey;
use base64::prelude::*;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S>(
Expand All @@ -202,7 +216,7 @@ pub mod serde_signaling_key {
where
S: Serializer,
{
serializer.serialize_str(&base64::encode(signaling_key))
serializer.serialize_str(&BASE64_STANDARD.encode(signaling_key))
}

pub fn deserialize<'de, D>(
Expand All @@ -211,7 +225,8 @@ pub mod serde_signaling_key {
where
D: Deserializer<'de>,
{
base64::decode(String::deserialize(deserializer)?)
BASE64_STANDARD
.decode(String::deserialize(deserializer)?)
.map_err(serde::de::Error::custom)?
.try_into()
.map_err(|buf: Vec<u8>| {
Expand Down
7 changes: 5 additions & 2 deletions libsignal-service/src/websocket/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
sender::{OutgoingPushMessages, SendMessageResponse},
unidentified_access::UnidentifiedAccess,
};
use base64::prelude::*;

use super::*;

Expand All @@ -20,8 +21,10 @@ impl SignalWebSocket {
access: &UnidentifiedAccess,
) -> Result<SendMessageResponse, ServiceError> {
let path = format!("/v1/messages/{}", messages.recipient.uuid);
let header =
format!("Unidentified-Access-Key:{}", base64::encode(&access.key));
let header = format!(
"Unidentified-Access-Key:{}",
BASE64_STANDARD.encode(&access.key)
);
self.put_json_with_headers(&path, messages, vec![header])
.await
}
Expand Down

0 comments on commit fe3c224

Please sign in to comment.