Skip to content

Commit 2dcc366

Browse files
client: Use getrandom for generating a session secret
Share the same code as the portal key
1 parent a37fa5d commit 2dcc366

File tree

7 files changed

+21
-35
lines changed

7 files changed

+21
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ digest = { version = "0.10", optional = true }
2828
endi.workspace = true
2929
futures-lite = { workspace = true, optional = true }
3030
futures-util.workspace = true
31+
getrandom = "0.2"
3132
hkdf = { version = "0.12", optional = true }
3233
hmac = { version = "0.12", optional = true }
3334
md-5 = { version = "0.10", optional = true }

client/src/secret.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use rand::Rng;
21
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
32

43
/// A safe wrapper around a combination of (secret, content-type).
5-
#[derive(Debug, Zeroize, ZeroizeOnDrop)]
4+
#[derive(Debug, Clone, Zeroize, ZeroizeOnDrop)]
65
pub enum Secret {
76
/// Corresponds to `text/plain`
87
Text(String),
@@ -12,8 +11,12 @@ pub enum Secret {
1211

1312
impl Secret {
1413
/// Generate a random secret, used when creating a session collection.
15-
pub fn random() -> Self {
16-
Self::Blob(rand::thread_rng().gen::<[u8; 8]>().to_vec())
14+
pub fn random() -> Result<Self, getrandom::Error> {
15+
let mut secret = [0; 64];
16+
// Equivalent of `ring::rand::SecureRandom`
17+
getrandom::getrandom(&mut secret)?;
18+
19+
Ok(Self::blob(secret))
1720
}
1821

1922
/// Create a text secret, stored with `text/plain` content type.

portal/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ version.workspace = true
1515
ashpd = {workspace = true, features = ["backend", "tracing"]}
1616
clap.workspace = true
1717
oo7.workspace = true
18-
getrandom = "0.2"
1918
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
2019
tracing.workspace = true
2120
tracing-subscriber.workspace = true

portal/src/error.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[derive(Debug)]
22
pub enum Error {
3-
Rand(getrandom::Error),
43
Oo7(oo7::dbus::Error),
54
Io(std::io::Error),
65
Portal(ashpd::PortalError),
@@ -9,7 +8,6 @@ pub enum Error {
98
impl std::fmt::Display for Error {
109
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1110
match self {
12-
Error::Rand(e) => f.write_fmt(format_args!("Rand error {e}")),
1311
Error::Oo7(e) => f.write_fmt(format_args!("DBus error: {e}")),
1412
Error::Io(e) => f.write_fmt(format_args!("IO error: {e}")),
1513
Error::Portal(e) => f.write_fmt(format_args!("Portal error: {e}")),
@@ -20,20 +18,13 @@ impl std::fmt::Display for Error {
2018
impl std::error::Error for Error {
2119
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
2220
match self {
23-
Self::Rand(_) => None,
2421
Self::Oo7(e) => Some(e),
2522
Self::Io(e) => Some(e),
2623
Self::Portal(e) => Some(e),
2724
}
2825
}
2926
}
3027

31-
impl From<getrandom::Error> for Error {
32-
fn from(err: getrandom::Error) -> Self {
33-
Self::Rand(err)
34-
}
35-
}
36-
3728
impl From<oo7::dbus::Error> for Error {
3829
fn from(value: oo7::dbus::Error) -> Self {
3930
Self::Oo7(value)

portal/src/main.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub use error::Result;
1111
use oo7::dbus::Service;
1212
use tokio::io::AsyncWriteExt;
1313

14-
const PORTAL_SECRET_SIZE: usize = 64;
1514
const PORTAL_NAME: &str = "org.freedesktop.impl.portal.desktop.oo7";
1615

1716
struct Secret;
@@ -36,13 +35,6 @@ impl ashpd::backend::secret::SecretImpl for Secret {
3635
}
3736
}
3837

39-
fn generate_secret() -> Result<zeroize::Zeroizing<Vec<u8>>> {
40-
let mut secret = [0; PORTAL_SECRET_SIZE];
41-
// Equivalent of `ring::rand::SecureRandom`
42-
getrandom::getrandom(&mut secret)?;
43-
Ok(zeroize::Zeroizing::new(secret.to_vec()))
44-
}
45-
4638
/// Generates, stores and send the secret back to the fd stream
4739
async fn send_secret_to_app(app_id: &AppID, fd: std::os::fd::OwnedFd) -> Result<()> {
4840
let service = Service::new().await?;
@@ -56,11 +48,17 @@ async fn send_secret_to_app(app_id: &AppID, fd: std::os::fd::OwnedFd) -> Result<
5648
(oo7::XDG_SCHEMA_ATTRIBUTE, GENERIC_SCHEMA_VALUE),
5749
("app_id", app_id),
5850
]);
59-
let secret = if let Some(item) = collection.search_items(&attributes).await?.first() {
60-
item.secret().await?
51+
52+
// Write the secret to the FD.
53+
let std_stream = UnixStream::from(fd);
54+
std_stream.set_nonblocking(true)?;
55+
let mut stream = tokio::net::UnixStream::from_std(std_stream)?;
56+
57+
if let Some(item) = collection.search_items(&attributes).await?.first() {
58+
stream.write_all(&item.secret().await?).await?;
6159
} else {
6260
tracing::debug!("Could not find secret for {app_id}, creating one");
63-
let secret = generate_secret()?;
61+
let secret = oo7::Secret::random().unwrap();
6462

6563
collection
6664
.create_item(
@@ -72,14 +70,8 @@ async fn send_secret_to_app(app_id: &AppID, fd: std::os::fd::OwnedFd) -> Result<
7270
)
7371
.await?;
7472

75-
secret
76-
};
77-
78-
// Write the secret to the FD.
79-
let std_stream = UnixStream::from(fd);
80-
std_stream.set_nonblocking(true)?;
81-
let mut stream = tokio::net::UnixStream::from_std(std_stream)?;
82-
stream.write_all(&secret).await?;
73+
stream.write_all(&secret).await?;
74+
}
8375

8476
Ok(())
8577
}

server/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl Service {
233233
"session",
234234
false,
235235
Arc::clone(&service.manager),
236-
Arc::new(Keyring::temporary(Secret::random()).await?),
236+
Arc::new(Keyring::temporary(Secret::random().unwrap()).await?),
237237
);
238238
collections.push(collection.clone());
239239
object_server

0 commit comments

Comments
 (0)