Skip to content

Commit 74c4e49

Browse files
committed
fix: replace OsRng in new code from upstream merge
1 parent 7d6c8cb commit 74c4e49

3 files changed

Lines changed: 32 additions & 42 deletions

File tree

russh/src/keys/agent/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ impl AgentIdentity {
8383
#[cfg(test)]
8484
mod tests {
8585
use super::*;
86-
use ssh_key::rand_core::OsRng;
8786
use ssh_key::{PrivateKey, certificate};
8887

8988
fn create_test_certificate() -> Certificate {
9089
use std::time::{SystemTime, UNIX_EPOCH};
9190

9291
// Create a CA key
93-
let ca_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
92+
let ca_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
9493

9594
// Create a user key to be certified
96-
let user_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
95+
let user_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
9796

9897
// Build and sign the certificate with reasonable validity window
9998
let now = SystemTime::now()
@@ -104,7 +103,7 @@ mod tests {
104103
let valid_before = now + 86400 * 365; // 1 year from now
105104

106105
let mut builder = certificate::Builder::new_with_random_nonce(
107-
&mut OsRng,
106+
&mut rand::rng(),
108107
user_key.public_key(),
109108
valid_after,
110109
valid_before,
@@ -120,7 +119,7 @@ mod tests {
120119

121120
#[test]
122121
fn test_agent_identity_public_key_variant() {
123-
let key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519)
122+
let key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519)
124123
.unwrap()
125124
.public_key()
126125
.clone();
@@ -161,7 +160,7 @@ mod tests {
161160

162161
#[test]
163162
fn test_agent_identity_clone() {
164-
let key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519)
163+
let key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519)
165164
.unwrap()
166165
.public_key()
167166
.clone();
@@ -177,7 +176,7 @@ mod tests {
177176

178177
#[test]
179178
fn test_agent_identity_debug() {
180-
let key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519)
179+
let key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519)
181180
.unwrap()
182181
.public_key()
183182
.clone();

russh/src/keys/mod.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
10201020
#[cfg(unix)]
10211021
fn create_test_cert(ca_key: &PrivateKey, user_key: &PrivateKey) -> ssh_key::Certificate {
10221022
use ssh_key::certificate;
1023-
use ssh_key::rand_core::OsRng;
1024-
use std::time::{SystemTime, UNIX_EPOCH};
1023+
use std::time::{SystemTime, UNIX_EPOCH};
10251024

10261025
let now = SystemTime::now()
10271026
.duration_since(UNIX_EPOCH)
@@ -1031,7 +1030,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
10311030
let valid_before = now + 86400 * 365; // 1 year from now
10321031

10331032
let mut builder = certificate::Builder::new_with_random_nonce(
1034-
&mut OsRng,
1033+
&mut rand::rng(),
10351034
user_key.public_key(),
10361035
valid_after,
10371036
valid_before,
@@ -1049,18 +1048,17 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
10491048
#[cfg(unix)]
10501049
async fn test_request_identities_full_with_keys_and_certs() {
10511050
use crate::keys::agent::{AgentIdentity, client::AgentClient};
1052-
use ssh_key::rand_core::OsRng;
1053-
use std::io::Write;
1051+
use std::io::Write;
10541052
use std::process::Stdio;
10551053

10561054
env_logger::try_init().unwrap_or(());
10571055

10581056
let (mut agent, agent_path, dir) = spawn_agent().await.unwrap();
10591057

10601058
// Create a CA key and user key
1061-
let ca_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1062-
let user_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1063-
let plain_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1059+
let ca_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
1060+
let user_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
1061+
let plain_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
10641062

10651063
// Create a certificate
10661064
let cert = create_test_cert(&ca_key, &user_key);
@@ -1176,17 +1174,16 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
11761174
#[cfg(unix)]
11771175
async fn test_sign_request_cert() {
11781176
use crate::keys::agent::client::AgentClient;
1179-
use ssh_key::rand_core::OsRng;
1180-
use std::io::Write;
1177+
use std::io::Write;
11811178
use std::process::Stdio;
11821179

11831180
env_logger::try_init().unwrap_or(());
11841181

11851182
let (mut agent, agent_path, dir) = spawn_agent().await.unwrap();
11861183

11871184
// Create a CA key and user key
1188-
let ca_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1189-
let user_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1185+
let ca_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
1186+
let user_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
11901187

11911188
// Create a certificate
11921189
let cert = create_test_cert(&ca_key, &user_key);
@@ -1254,15 +1251,14 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
12541251
#[cfg(unix)]
12551252
async fn test_sign_request_cert_missing_key_returns_agent_failure() {
12561253
use crate::keys::agent::client::AgentClient;
1257-
use ssh_key::rand_core::OsRng;
1258-
1254+
12591255
env_logger::try_init().unwrap_or(());
12601256

12611257
let (mut agent, agent_path, _dir) = spawn_agent().await.unwrap();
12621258

12631259
// Create a CA key and user key, but DON'T add them to the agent
1264-
let ca_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1265-
let user_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1260+
let ca_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
1261+
let user_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
12661262

12671263
// Create a certificate
12681264
let cert = create_test_cert(&ca_key, &user_key);
@@ -1307,14 +1303,13 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
13071303
#[cfg(unix)]
13081304
async fn test_sign_request_missing_key_returns_agent_failure() {
13091305
use crate::keys::agent::client::AgentClient;
1310-
use ssh_key::rand_core::OsRng;
1311-
1306+
13121307
env_logger::try_init().unwrap_or(());
13131308

13141309
let (mut agent, agent_path, _dir) = spawn_agent().await.unwrap();
13151310

13161311
// Create a key but DON'T add it to the agent
1317-
let key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
1312+
let key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
13181313

13191314
// Connect to agent WITHOUT adding any keys
13201315
let stream = tokio::net::UnixStream::connect(&agent_path).await.unwrap();
@@ -1358,8 +1353,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
13581353
#[cfg(all(unix, feature = "rsa"))]
13591354
fn create_test_rsa_cert(ca_key: &PrivateKey, user_key: &PrivateKey) -> ssh_key::Certificate {
13601355
use ssh_key::certificate;
1361-
use ssh_key::rand_core::OsRng;
1362-
use std::time::{SystemTime, UNIX_EPOCH};
1356+
use std::time::{SystemTime, UNIX_EPOCH};
13631357

13641358
let now = SystemTime::now()
13651359
.duration_since(UNIX_EPOCH)
@@ -1369,7 +1363,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
13691363
let valid_before = now + 86400 * 365; // 1 year from now
13701364

13711365
let mut builder = certificate::Builder::new_with_random_nonce(
1372-
&mut OsRng,
1366+
&mut rand::rng(),
13731367
user_key.public_key(),
13741368
valid_after,
13751369
valid_before,
@@ -1387,8 +1381,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
13871381
#[cfg(all(unix, feature = "rsa"))]
13881382
async fn test_sign_request_cert_rsa() {
13891383
use crate::keys::agent::client::AgentClient;
1390-
use ssh_key::rand_core::OsRng;
1391-
use std::io::Write;
1384+
use std::io::Write;
13921385
use std::process::Stdio;
13931386

13941387
env_logger::try_init().unwrap_or(());
@@ -1397,14 +1390,14 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
13971390

13981391
// Create RSA CA key and user key
13991392
let ca_key = PrivateKey::random(
1400-
&mut OsRng,
1393+
&mut rand::rng(),
14011394
ssh_key::Algorithm::Rsa {
14021395
hash: Some(HashAlg::Sha256),
14031396
},
14041397
)
14051398
.unwrap();
14061399
let user_key = PrivateKey::random(
1407-
&mut OsRng,
1400+
&mut rand::rng(),
14081401
ssh_key::Algorithm::Rsa {
14091402
hash: Some(HashAlg::Sha256),
14101403
},
@@ -1482,8 +1475,7 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
14821475
#[cfg(all(unix, feature = "rsa"))]
14831476
async fn test_sign_request_cert_rsa_sha512() {
14841477
use crate::keys::agent::client::AgentClient;
1485-
use ssh_key::rand_core::OsRng;
1486-
use std::io::Write;
1478+
use std::io::Write;
14871479
use std::process::Stdio;
14881480

14891481
env_logger::try_init().unwrap_or(());
@@ -1492,14 +1484,14 @@ Cog3JMeTrb3LiPHgN6gU2P30MRp6L1j1J/MtlOAr5rux
14921484

14931485
// Create RSA CA key and user key
14941486
let ca_key = PrivateKey::random(
1495-
&mut OsRng,
1487+
&mut rand::rng(),
14961488
ssh_key::Algorithm::Rsa {
14971489
hash: Some(HashAlg::Sha512),
14981490
},
14991491
)
15001492
.unwrap();
15011493
let user_key = PrivateKey::random(
1502-
&mut OsRng,
1494+
&mut rand::rng(),
15031495
ssh_key::Algorithm::Rsa {
15041496
hash: Some(HashAlg::Sha512),
15051497
},

russh/src/tests.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,6 @@ mod future_certificate {
623623
use std::sync::Arc;
624624

625625
use ssh_key::{certificate, PrivateKey};
626-
use ssh_key::rand_core::OsRng;
627626

628627
use crate::keys::agent::client::AgentClient;
629628
use crate::{client, server};
@@ -666,7 +665,7 @@ mod future_certificate {
666665
let valid_before = now + 86400 * 365;
667666

668667
let mut builder = certificate::Builder::new_with_random_nonce(
669-
&mut OsRng,
668+
&mut rand::rng(),
670669
user_key.public_key(),
671670
valid_after,
672671
valid_before,
@@ -688,8 +687,8 @@ mod future_certificate {
688687
let (mut agent, agent_path, dir) = spawn_agent().await;
689688

690689
// 2. Create CA key and user key
691-
let ca_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
692-
let user_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap();
690+
let ca_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
691+
let user_key = PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap();
693692

694693
// 3. Create a certificate
695694
let cert = create_test_cert(&ca_key, &user_key);
@@ -731,7 +730,7 @@ mod future_certificate {
731730
server_config.auth_rejection_time = std::time::Duration::from_secs(3);
732731
server_config
733732
.keys
734-
.push(PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap());
733+
.push(PrivateKey::random(&mut rand::rng(), ssh_key::Algorithm::Ed25519).unwrap());
735734
let server_config = Arc::new(server_config);
736735

737736
let socket = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();

0 commit comments

Comments
 (0)