Skip to content

Commit 332a0da

Browse files
committed
remove duplicate Mutex inside cipher_suite
1 parent 6de36ae commit 332a0da

8 files changed

+78
-122
lines changed

dtls/src/cipher_suite.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ pub trait CipherSuite {
116116
fn certificate_type(&self) -> ClientCertificateType;
117117
fn hash_func(&self) -> CipherSuiteHash;
118118
fn is_psk(&self) -> bool;
119-
async fn is_initialized(&self) -> bool;
119+
fn is_initialized(&self) -> bool;
120120

121121
// Generate the internal encryption state
122-
async fn init(
123-
&self,
122+
fn init(
123+
&mut self,
124124
master_secret: &[u8],
125125
client_random: &[u8],
126126
server_random: &[u8],
127127
is_client: bool,
128128
) -> Result<(), Error>;
129129

130-
async fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error>;
131-
async fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error>;
130+
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error>;
131+
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error>;
132132
}
133133

134134
// Taken from https://www.iana.org/assignments/tls-parameters/tls-parameters.xml

dtls/src/cipher_suite/cipher_suite_tls_ecdhe_ecdsa_with_aes_128_gcm_sha256.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ use super::*;
22
use crate::crypto::crypto_gcm::*;
33
use crate::prf::*;
44

5-
use async_trait::async_trait;
6-
use std::sync::Arc;
7-
use tokio::sync::Mutex;
8-
95
#[derive(Clone)]
106
pub struct CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
11-
gcm: Arc<Mutex<Option<CryptoGcm>>>,
7+
gcm: Option<CryptoGcm>,
128
}
139

1410
impl CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
@@ -19,13 +15,10 @@ impl CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
1915

2016
impl Default for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
2117
fn default() -> Self {
22-
CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
23-
gcm: Arc::new(Mutex::new(None)),
24-
}
18+
CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 { gcm: None }
2519
}
2620
}
2721

28-
#[async_trait]
2922
impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
3023
fn to_string(&self) -> String {
3124
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256".to_owned()
@@ -47,13 +40,12 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
4740
false
4841
}
4942

50-
async fn is_initialized(&self) -> bool {
51-
let gcm = self.gcm.lock().await;
52-
gcm.is_some()
43+
fn is_initialized(&self) -> bool {
44+
self.gcm.is_some()
5345
}
5446

55-
async fn init(
56-
&self,
47+
fn init(
48+
&mut self,
5749
master_secret: &[u8],
5850
client_random: &[u8],
5951
server_random: &[u8],
@@ -69,16 +61,15 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
6961
self.hash_func(),
7062
)?;
7163

72-
let mut gcm = self.gcm.lock().await;
7364
if is_client {
74-
*gcm = Some(CryptoGcm::new(
65+
self.gcm = Some(CryptoGcm::new(
7566
&keys.client_write_key,
7667
&keys.client_write_iv,
7768
&keys.server_write_key,
7869
&keys.server_write_iv,
7970
));
8071
} else {
81-
*gcm = Some(CryptoGcm::new(
72+
self.gcm = Some(CryptoGcm::new(
8273
&keys.server_write_key,
8374
&keys.server_write_iv,
8475
&keys.client_write_key,
@@ -89,9 +80,8 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
8980
Ok(())
9081
}
9182

92-
async fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
93-
let gcm = self.gcm.lock().await;
94-
if let Some(cg) = &*gcm {
83+
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
84+
if let Some(cg) = &self.gcm {
9585
cg.encrypt(pkt_rlh, raw)
9686
} else {
9787
Err(Error::new(
@@ -100,9 +90,8 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes128GcmSha256 {
10090
}
10191
}
10292

103-
async fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
104-
let gcm = self.gcm.lock().await;
105-
if let Some(cg) = &*gcm {
93+
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
94+
if let Some(cg) = &self.gcm {
10695
cg.decrypt(input)
10796
} else {
10897
Err(Error::new(

dtls/src/cipher_suite/cipher_suite_tls_ecdhe_ecdsa_with_aes_256_cbc_sha.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ use super::*;
22
use crate::crypto::crypto_cbc::*;
33
use crate::prf::*;
44

5-
use async_trait::async_trait;
6-
use std::sync::Arc;
7-
use tokio::sync::Mutex;
8-
95
#[derive(Clone)]
106
pub struct CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
11-
cbc: Arc<Mutex<Option<CryptoCbc>>>,
7+
cbc: Option<CryptoCbc>,
128
}
139

1410
impl CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
@@ -19,13 +15,10 @@ impl CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
1915

2016
impl Default for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
2117
fn default() -> Self {
22-
CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
23-
cbc: Arc::new(Mutex::new(None)),
24-
}
18+
CipherSuiteTLSEcdheEcdsaWithAes256CbcSha { cbc: None }
2519
}
2620
}
2721

28-
#[async_trait]
2922
impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
3023
fn to_string(&self) -> String {
3124
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA".to_owned()
@@ -47,13 +40,12 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
4740
false
4841
}
4942

50-
async fn is_initialized(&self) -> bool {
51-
let cbc = self.cbc.lock().await;
52-
cbc.is_some()
43+
fn is_initialized(&self) -> bool {
44+
self.cbc.is_some()
5345
}
5446

55-
async fn init(
56-
&self,
47+
fn init(
48+
&mut self,
5749
master_secret: &[u8],
5850
client_random: &[u8],
5951
server_random: &[u8],
@@ -69,9 +61,8 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
6961
self.hash_func(),
7062
)?;
7163

72-
let mut cbc = self.cbc.lock().await;
7364
if is_client {
74-
*cbc = Some(CryptoCbc::new(
65+
self.cbc = Some(CryptoCbc::new(
7566
&keys.client_write_key,
7667
&keys.client_write_iv,
7768
&keys.client_mac_key,
@@ -80,7 +71,7 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
8071
&keys.server_mac_key,
8172
)?);
8273
} else {
83-
*cbc = Some(CryptoCbc::new(
74+
self.cbc = Some(CryptoCbc::new(
8475
&keys.server_write_key,
8576
&keys.server_write_iv,
8677
&keys.server_mac_key,
@@ -93,9 +84,8 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
9384
Ok(())
9485
}
9586

96-
async fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
97-
let cbc = self.cbc.lock().await;
98-
if let Some(cg) = &*cbc {
87+
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
88+
if let Some(cg) = &self.cbc {
9989
cg.encrypt(pkt_rlh, raw)
10090
} else {
10191
Err(Error::new(
@@ -104,9 +94,8 @@ impl CipherSuite for CipherSuiteTLSEcdheEcdsaWithAes256CbcSha {
10494
}
10595
}
10696

107-
async fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
108-
let cbc = self.cbc.lock().await;
109-
if let Some(cg) = &*cbc {
97+
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
98+
if let Some(cg) = &self.cbc {
11099
cg.decrypt(input)
111100
} else {
112101
Err(Error::new(

dtls/src/cipher_suite/cipher_suite_tls_psk_with_aes_128_gcm_sha256.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ use super::*;
22
use crate::crypto::crypto_gcm::*;
33
use crate::prf::*;
44

5-
use async_trait::async_trait;
6-
use std::sync::Arc;
7-
use tokio::sync::Mutex;
8-
95
#[derive(Clone)]
106
pub struct CipherSuiteTLSPskWithAes128GcmSha256 {
11-
gcm: Arc<Mutex<Option<CryptoGcm>>>,
7+
gcm: Option<CryptoGcm>,
128
}
139

1410
impl CipherSuiteTLSPskWithAes128GcmSha256 {
@@ -19,13 +15,10 @@ impl CipherSuiteTLSPskWithAes128GcmSha256 {
1915

2016
impl Default for CipherSuiteTLSPskWithAes128GcmSha256 {
2117
fn default() -> Self {
22-
CipherSuiteTLSPskWithAes128GcmSha256 {
23-
gcm: Arc::new(Mutex::new(None)),
24-
}
18+
CipherSuiteTLSPskWithAes128GcmSha256 { gcm: None }
2519
}
2620
}
2721

28-
#[async_trait]
2922
impl CipherSuite for CipherSuiteTLSPskWithAes128GcmSha256 {
3023
fn to_string(&self) -> String {
3124
"TLS_PSK_WITH_AES_128_GCM_SHA256".to_owned()
@@ -47,13 +40,12 @@ impl CipherSuite for CipherSuiteTLSPskWithAes128GcmSha256 {
4740
true
4841
}
4942

50-
async fn is_initialized(&self) -> bool {
51-
let gcm = self.gcm.lock().await;
52-
gcm.is_some()
43+
fn is_initialized(&self) -> bool {
44+
self.gcm.is_some()
5345
}
5446

55-
async fn init(
56-
&self,
47+
fn init(
48+
&mut self,
5749
master_secret: &[u8],
5850
client_random: &[u8],
5951
server_random: &[u8],
@@ -69,16 +61,15 @@ impl CipherSuite for CipherSuiteTLSPskWithAes128GcmSha256 {
6961
self.hash_func(),
7062
)?;
7163

72-
let mut gcm = self.gcm.lock().await;
7364
if is_client {
74-
*gcm = Some(CryptoGcm::new(
65+
self.gcm = Some(CryptoGcm::new(
7566
&keys.client_write_key,
7667
&keys.client_write_iv,
7768
&keys.server_write_key,
7869
&keys.server_write_iv,
7970
));
8071
} else {
81-
*gcm = Some(CryptoGcm::new(
72+
self.gcm = Some(CryptoGcm::new(
8273
&keys.server_write_key,
8374
&keys.server_write_iv,
8475
&keys.client_write_key,
@@ -89,9 +80,8 @@ impl CipherSuite for CipherSuiteTLSPskWithAes128GcmSha256 {
8980
Ok(())
9081
}
9182

92-
async fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
93-
let gcm = self.gcm.lock().await;
94-
if let Some(cg) = &*gcm {
83+
fn encrypt(&self, pkt_rlh: &RecordLayerHeader, raw: &[u8]) -> Result<Vec<u8>, Error> {
84+
if let Some(cg) = &self.gcm {
9585
cg.encrypt(pkt_rlh, raw)
9686
} else {
9787
Err(Error::new(
@@ -100,9 +90,8 @@ impl CipherSuite for CipherSuiteTLSPskWithAes128GcmSha256 {
10090
}
10191
}
10292

103-
async fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
104-
let gcm = self.gcm.lock().await;
105-
if let Some(cg) = &*gcm {
93+
fn decrypt(&self, input: &[u8]) -> Result<Vec<u8>, Error> {
94+
if let Some(cg) = &self.gcm {
10695
cg.decrypt(input)
10796
} else {
10897
Err(Error::new(

dtls/src/conn.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,9 @@ impl Conn {
378378

379379
// ConnectionState returns basic DTLS details about the connection.
380380
// Note that this replaced the `Export` function of v1.
381-
//pub fn connection_state(&self) -> State {
382-
//c.lock.RLock()
383-
//defer c.lock.RUnlock()
384-
//self.state.clone()
385-
//}
381+
pub async fn connection_state(&self) -> State {
382+
self.state.clone().await
383+
}
386384

387385
// selected_srtpprotection_profile returns the selected SRTPProtectionProfile
388386
pub fn selected_srtpprotection_profile(&self) -> SRTPProtectionProfile {
@@ -524,9 +522,7 @@ impl Conn {
524522
if p.should_encrypt {
525523
let cipher_suite = cipher_suite.lock().await;
526524
if let Some(cipher_suite) = &*cipher_suite {
527-
raw_packet = cipher_suite
528-
.encrypt(&p.record.record_layer_header, &raw_packet)
529-
.await?;
525+
raw_packet = cipher_suite.encrypt(&p.record.record_layer_header, &raw_packet)?;
530526
}
531527
}
532528

@@ -583,9 +579,7 @@ impl Conn {
583579
if p.should_encrypt {
584580
let cipher_suite = cipher_suite.lock().await;
585581
if let Some(cipher_suite) = &*cipher_suite {
586-
raw_packet = cipher_suite
587-
.encrypt(&record_layer_header, &raw_packet)
588-
.await?;
582+
raw_packet = cipher_suite.encrypt(&record_layer_header, &raw_packet)?;
589583
}
590584
}
591585

@@ -851,7 +845,7 @@ impl Conn {
851845
if cipher_suite.is_none() {
852846
true
853847
} else if let Some(cipher_suite) = &*cipher_suite {
854-
!cipher_suite.is_initialized().await
848+
!cipher_suite.is_initialized()
855849
} else {
856850
false
857851
}
@@ -869,7 +863,7 @@ impl Conn {
869863

870864
let cipher_suite = ctx.cipher_suite.lock().await;
871865
if let Some(cipher_suite) = &*cipher_suite {
872-
pkt = match cipher_suite.decrypt(&pkt).await {
866+
pkt = match cipher_suite.decrypt(&pkt) {
873867
Ok(pkt) => pkt,
874868
Err(err) => {
875869
debug!("{}: decrypt failed: {}", srv_cli_str(ctx.is_client), err);
@@ -965,7 +959,7 @@ impl Conn {
965959
if cipher_suite.is_none() {
966960
true
967961
} else if let Some(cipher_suite) = &*cipher_suite {
968-
!cipher_suite.is_initialized().await
962+
!cipher_suite.is_initialized()
969963
} else {
970964
false
971965
}

dtls/src/flight/flight4.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ impl Flight for Flight4 {
258258
}
259259

260260
{
261-
let cipher_suite = state.cipher_suite.lock().await;
262-
if let Some(cipher_suite) = &*cipher_suite {
263-
if !cipher_suite.is_initialized().await {
261+
let mut cipher_suite = state.cipher_suite.lock().await;
262+
if let Some(cipher_suite) = &mut *cipher_suite {
263+
if !cipher_suite.is_initialized() {
264264
let mut server_random = vec![];
265265
{
266266
let mut writer = BufWriter::<&mut Vec<u8>>::new(server_random.as_mut());
@@ -359,10 +359,12 @@ impl Flight for Flight4 {
359359
};
360360
}
361361

362-
if let Err(err) = cipher_suite
363-
.init(&state.master_secret, &client_random, &server_random, false)
364-
.await
365-
{
362+
if let Err(err) = cipher_suite.init(
363+
&state.master_secret,
364+
&client_random,
365+
&server_random,
366+
false,
367+
) {
366368
return Err((
367369
Some(Alert {
368370
alert_level: AlertLevel::Fatal,

0 commit comments

Comments
 (0)