Skip to content

Commit beb8ba7

Browse files
committed
(feat/webrtc-sniffer): node use p2p key to create self-signed dtls certificate
1 parent ace1bf5 commit beb8ba7

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p2p/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cfg-if = "1.0.0"
2424
url = "2.3.1"
2525
multihash = "0.18.1"
2626
sha2 = "0.10.6"
27-
ed25519-dalek = { version = "2.1.1", features = ["serde"] }
27+
ed25519-dalek = { version = "2.1.1", features = ["serde", "pem"] }
2828
x25519-dalek = { version = "2.0.1", features = ["static_secrets"] }
2929
aes-gcm = "0.10.3"
3030
faster-stun = { version = "1.0.1", optional = true }
@@ -84,6 +84,7 @@ webrtc = { git = "https://github.com/openmina/webrtc.git", rev = "e8705db39af1b1
8484
datachannel = { git = "https://github.com/openmina/datachannel-rs.git", rev = "1bfb064d0ff3e54a93ae0288409902aab8d102d3", optional = true, features = [
8585
"vendored",
8686
] }
87+
rcgen = { version = "0.13", features = ["pem", "x509-parser"], optional = true }
8788
reqwest = { version = "0.11", features = ["json"] }
8889
mio = { version = "0.8.11", features = ["os-poll", "net"] }
8990
libc = { version = "0.2.151" }
@@ -119,7 +120,7 @@ getrandom = { version = "0.2", features = ["js"] }
119120
[features]
120121
serializable_callbacks = []
121122
p2p-webrtc = ["p2p-webrtc-rs"]
122-
p2p-webrtc-rs = ["webrtc"]
123+
p2p-webrtc-rs = ["webrtc", "rcgen"]
123124
p2p-webrtc-cpp = ["datachannel"]
124125
p2p-libp2p = ["fuzzing", "dep:reqwest", "dep:faster-stun"]
125126
fuzzing = ["openmina-fuzzer", "openmina-core/fuzzing"]

p2p/src/identity/secret_key.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::{fmt, path::Path, str::FromStr};
22

33
use base64::Engine;
4-
use ed25519_dalek::{ed25519::signature::SignerMut, SigningKey as Ed25519SecretKey};
4+
use ed25519_dalek::{
5+
ed25519::signature::SignerMut, pkcs8::EncodePrivateKey as _, SigningKey as Ed25519SecretKey,
6+
};
57
use openmina_core::{EncryptedSecretKey, EncryptedSecretKeyFile, EncryptionError};
68
use rand::{CryptoRng, Rng};
79
use serde::{Deserialize, Serialize};
10+
use zeroize::Zeroizing;
811

912
use super::{PublicKey, Signature};
1013

@@ -53,6 +56,12 @@ impl SecretKey {
5356
self.0.to_scalar_bytes().into()
5457
}
5558

59+
pub fn to_pem(&self) -> Zeroizing<String> {
60+
self.0
61+
.to_pkcs8_pem(Default::default())
62+
.expect("must be valid key")
63+
}
64+
5665
pub fn from_encrypted_file(
5766
path: impl AsRef<Path>,
5867
password: &str,

p2p/src/service_impl/webrtc/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ use crate::{
3333
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs"))]
3434
mod imports {
3535
pub use super::webrtc_rs::{
36-
build_api, webrtc_signal_send, Api, RTCChannel, RTCConnection, RTCConnectionState,
37-
RTCSignalingError,
36+
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
37+
RTCConnection, RTCConnectionState, RTCSignalingError,
3838
};
3939
}
4040
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp"))]
4141
mod imports {
4242
pub use super::webrtc_cpp::{
43-
build_api, webrtc_signal_send, Api, RTCChannel, RTCConnection, RTCConnectionState,
44-
RTCSignalingError,
43+
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
44+
RTCConnection, RTCConnectionState, RTCSignalingError,
4545
};
4646
}
4747
#[cfg(target_arch = "wasm32")]
4848
mod imports {
4949
pub use super::web::{
50-
build_api, webrtc_signal_send, Api, RTCChannel, RTCConnection, RTCConnectionState,
51-
RTCSignalingError,
50+
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
51+
RTCConnection, RTCConnectionState, RTCSignalingError,
5252
};
5353
}
5454

@@ -140,7 +140,7 @@ pub type OnConnectionStateChangeHdlrFn = Box<
140140

141141
pub struct RTCConfig {
142142
pub ice_servers: RTCConfigIceServers,
143-
// TODO(binier): certificate
143+
pub certificate: RTCCertificate,
144144
}
145145

146146
#[derive(Serialize)]
@@ -223,7 +223,13 @@ async fn wait_for_ice_gathering_complete(pc: &mut RTCConnection) {
223223
}
224224
}
225225

226-
async fn peer_start(api: Api, args: PeerAddArgs, abort: Aborted, closed: mpsc::Sender<()>) {
226+
async fn peer_start(
227+
api: Api,
228+
args: PeerAddArgs,
229+
abort: Aborted,
230+
closed: mpsc::Sender<()>,
231+
certificate: RTCCertificate,
232+
) {
227233
let PeerAddArgs {
228234
peer_id,
229235
kind,
@@ -234,6 +240,7 @@ async fn peer_start(api: Api, args: PeerAddArgs, abort: Aborted, closed: mpsc::S
234240

235241
let config = RTCConfig {
236242
ice_servers: Default::default(),
243+
certificate,
237244
};
238245
let fut = async {
239246
let mut pc = RTCConnection::create(&api, config).await?;
@@ -727,7 +734,7 @@ pub trait P2pServiceWebrtc: redux::Service {
727734
const MAX_PEERS: usize = 500;
728735
let (cmd_sender, mut cmd_receiver) = mpsc::unbounded_channel();
729736

730-
let _ = secret_key;
737+
let certificate = certificate_from_pem_key(secret_key.to_pem().as_str());
731738

732739
spawner.spawn_main("webrtc", async move {
733740
#[allow(clippy::all)]
@@ -741,6 +748,7 @@ pub trait P2pServiceWebrtc: redux::Service {
741748
let conn_permits = conn_permits.clone();
742749
let peer_id = args.peer_id;
743750
let event_sender = args.event_sender.clone();
751+
let certificate = certificate.clone();
744752
spawn_local(async move {
745753
let Ok(_permit) = conn_permits.try_acquire() else {
746754
// state machine shouldn't allow this to happen.
@@ -755,8 +763,9 @@ pub trait P2pServiceWebrtc: redux::Service {
755763
event_sender_clone(P2pConnectionEvent::Closed(peer_id).into());
756764
});
757765
tokio::select! {
758-
_ = peer_start(api, args, aborted.clone(), closed_tx.clone()) => {}
759-
_ = aborted.wait() => {}
766+
_ = peer_start(api, args, aborted.clone(), closed_tx.clone(), certificate) => {}
767+
_ = aborted.wait() => {
768+
}
760769
}
761770

762771
// delay dropping permit to give some time for cleanup.

p2p/src/service_impl/webrtc/web.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub type RTCConnectionState = RtcPeerConnectionState;
3232

3333
pub type Api = ();
3434

35+
pub type RTCCertificate = ();
36+
37+
pub fn certificate_from_pem_key(_: &str) -> RTCCertificate {
38+
()
39+
}
40+
3541
pub fn build_api() -> Api {}
3642

3743
pub struct RTCConnection(Rc<RtcPeerConnection>, bool);

p2p/src/service_impl/webrtc/webrtc_cpp.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub type RTCConnectionState = ConnectionState;
2222

2323
pub type Api = ();
2424

25+
pub type RTCCertificate = ();
26+
27+
pub fn certificate_from_pem_key(_: &str) -> RTCCertificate {
28+
()
29+
}
30+
2531
type MessageHandler = Box<dyn FnMut(&[u8]) + 'static + Send>;
2632

2733
pub fn build_api() -> Api {}

p2p/src/service_impl/webrtc/webrtc_rs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ pub type RTCConnectionState = RTCPeerConnectionState;
2828

2929
pub type Api = Arc<webrtc::api::API>;
3030

31+
pub type RTCCertificate = webrtc::peer_connection::certificate::RTCCertificate;
32+
33+
pub fn certificate_from_pem_key(pem_str: &str) -> RTCCertificate {
34+
let keypair = rcgen::KeyPair::from_pem(pem_str).expect("valid pem");
35+
RTCCertificate::from_key_pair(keypair).expect("keypair is compatible")
36+
}
37+
3138
pub fn build_api() -> Api {
3239
APIBuilder::new().build().into()
3340
}
@@ -192,6 +199,7 @@ impl From<RTCConfig> for RTCConfiguration {
192199
RTCConfiguration {
193200
ice_servers: value.ice_servers.0.into_iter().map(Into::into).collect(),
194201
ice_transport_policy: RTCIceTransportPolicy::All,
202+
certificates: vec![value.certificate],
195203
..Default::default()
196204
}
197205
}

0 commit comments

Comments
 (0)