Skip to content

Commit 41b3595

Browse files
Fix P-256 Noise handshake (updated snow fork)
1 parent 2808a4e commit 41b3595

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Cargo.lock

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

libwebauthn/src/transport/cable/tunnel.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
1010
use tokio_tungstenite::tungstenite::http::StatusCode;
1111
use tokio_tungstenite::tungstenite::Message;
1212
use tokio_tungstenite::{connect_async, WebSocketStream};
13-
use tracing::{debug, error};
13+
use tracing::{debug, error, trace};
1414

1515
use super::channel::CableChannel;
1616
use crate::transport::error::Error;
@@ -22,6 +22,14 @@ const BASE32_CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyz234567";
2222
const TLDS: &[&str] = &[".com", ".org", ".net", ".info"];
2323
const P256_X962_LENGTH: usize = 65;
2424

25+
// const CABLE_PROLOGUE_STATE_ASSISTED = [0 as u8];
26+
const CABLE_PROLOGUE_QR_INITIATED: &[u8] = &[1 as u8];
27+
28+
enum TransactionType {
29+
StateAssisted,
30+
QRInitiated,
31+
}
32+
2533
pub fn decode_tunnel_server_domain(encoded: u16) -> Option<String> {
2634
if encoded < 256 {
2735
if encoded as usize >= KNOWN_TUNNEL_DOMAINS.len() {
@@ -81,7 +89,13 @@ pub async fn connect<'d>(
8189
}
8290
debug!("Tunnel server returned success");
8391

84-
do_handshake(&mut ws_stream, psk, private_key).await?;
92+
do_handshake(
93+
&mut ws_stream,
94+
psk,
95+
private_key,
96+
TransactionType::QRInitiated,
97+
)
98+
.await?;
8599
// After this, the handshake should be complete and you can start sending/receiving encrypted messages.
86100
// ...
87101

@@ -92,12 +106,20 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
92106
ws_stream: &mut WebSocketStream<T>,
93107
psk: &[u8; 32],
94108
private_key: &NonZeroScalar,
109+
transaction_type: TransactionType,
95110
) -> Result<(), Error> {
96111
let local_private_key = private_key.to_bytes();
97-
let noise_params: NoiseParams = "Noise_KNpsk0_P256_AESGCM_SHA256".parse().unwrap();
98-
let noise_builder = Builder::new(noise_params)
99-
.local_private_key(&local_private_key.as_slice())?
100-
.psk(0, psk)?;
112+
113+
let noise_builder = match transaction_type {
114+
TransactionType::QRInitiated => Builder::new("Noise_KNpsk0_P256_AESGCM_SHA256".parse()?)
115+
.prologue(CABLE_PROLOGUE_QR_INITIATED)?
116+
.local_private_key(&local_private_key.as_slice())?
117+
.psk(0, psk)?,
118+
TransactionType::StateAssisted => {
119+
// Builder::new("Noise_NKpsk0_P256_AESGCM_SHA256".parse().unwrap())
120+
todo!()
121+
}
122+
};
101123

102124
// Build the Noise handshake as the initiator
103125
let mut noise_handshake = match noise_builder.build_initiator() {
@@ -116,7 +138,7 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
116138
return Err(Error::Transport(TransportError::ConnectionFailed));
117139
}
118140
};
119-
debug!(
141+
trace!(
120142
{ handshake = ?initial_msg_buffer[..initial_msg_len] },
121143
"Sending initial handshake message"
122144
);
@@ -134,7 +156,12 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
134156

135157
// Read the response from the server and process it
136158
let response = match ws_stream.next().await {
137-
Some(Ok(Message::Binary(response))) => response,
159+
Some(Ok(Message::Binary(response))) => {
160+
debug!(response_len = response.len(), "Received handshake response");
161+
trace!(?response);
162+
response
163+
}
164+
138165
Some(Ok(msg)) => {
139166
error!(?msg, "Unexpected message type received");
140167
return Err(Error::Transport(TransportError::ConnectionFailed));
@@ -161,8 +188,8 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
161188
return Err(Error::Transport(TransportError::ConnectionFailed));
162189
}
163190

164-
let peer_point_bytes = &response[..P256_X962_LENGTH];
165-
let ciphertext = &response[P256_X962_LENGTH..];
191+
// let peer_point_bytes = &response[..P256_X962_LENGTH];
192+
// let ciphertext = &response[P256_X962_LENGTH..];
166193

167194
let mut payload = [0u8; 1024];
168195
let payload_len = noise_handshake

0 commit comments

Comments
 (0)