Skip to content

Commit 2cbb788

Browse files
Addressed comments on #51
1 parent 0716708 commit 2cbb788

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

libwebauthn/src/proto/ctap2/cbor/response.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ impl CborResponse {
1414
pub fn new_success_from_slice(slice: &[u8]) -> Self {
1515
Self {
1616
status_code: CtapError::Ok,
17-
data: Some(slice.to_vec()),
17+
data: match slice.len() {
18+
0 => None,
19+
_ => Some(Vec::from(slice)),
20+
},
1821
}
1922
}
2023
}

libwebauthn/src/transport/cable/crypto.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum KeyPurpose {
1313
}
1414

1515

16-
pub fn derive(secret: &[u8], salt: Option<&[u8]>, purpose: KeyPurpose) -> Vec<u8> {
16+
pub fn derive(secret: &[u8; 16], salt: Option<&[u8]>, purpose: KeyPurpose) -> Vec<u8> {
1717
let mut purpose32 = [0u8; 4];
1818
purpose32[0] = purpose as u8;
1919

@@ -34,6 +34,11 @@ pub fn trial_decrypt_advert(eid_key: &[u8], candidate_advert: &[u8]) -> Option<V
3434
return None;
3535
}
3636

37+
if eid_key.len() != 64 {
38+
warn!("EID key is not 64 bytes");
39+
return None;
40+
}
41+
3742
let expected_tag = hmac_sha256(&eid_key[32..], &candidate_advert[..16]);
3843
if expected_tag[..4] != candidate_advert[16..] {
3944
warn!({ expected = ?expected_tag[..4], actual = ?candidate_advert[16..] },

libwebauthn/src/transport/cable/digit_encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn digit_encode(input: &[u8]) -> String {
1919
input = &input[CHUNK_SIZE..];
2020
}
2121
if !input.is_empty() {
22-
let digits = 15 & (PARTIAL_CHUNK_DIGITS >> (4 * input.len()));
22+
let digits = 0x0F & (PARTIAL_CHUNK_DIGITS >> (4 * input.len()));
2323
let mut chunk = [0u8; 8];
2424
chunk[..input.len()].copy_from_slice(input);
2525
let v = u64::from_le_bytes(chunk);

libwebauthn/src/transport/cable/qr_code_device.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use p256::{NonZeroScalar, SecretKey};
77
use rand::rngs::OsRng;
88
use rand::RngCore;
99
use serde::Serialize;
10-
use serde_bytes::ByteBuf;
10+
use serde_bytes::ByteArray;
1111
use serde_indexed::SerializeIndexed;
1212
use tokio::time::sleep;
1313
use tracing::{debug, error, instrument, trace};
@@ -49,9 +49,9 @@ impl Serialize for QrCodeOperationHint {
4949
#[derive(Debug, SerializeIndexed)]
5050
pub struct CableQrCode {
5151
// Key 0: a 33-byte, P-256, X9.62, compressed public key.
52-
pub public_key: ByteBuf,
52+
pub public_key: ByteArray<33>,
5353
// Key 1: a 16-byte random QR secret.
54-
pub qr_secret: ByteBuf,
54+
pub qr_secret: ByteArray<16>,
5555
/// Key 2: the number of assigned tunnel server domains known to this implementation.
5656
pub known_tunnel_domains_count: u8,
5757
/// Key 3: (optional) the current time in epoch seconds.
@@ -144,7 +144,13 @@ impl<'d> CableQrCodeDevice<'d> {
144144
) -> Self {
145145
let private_key_scalar = NonZeroScalar::random(&mut OsRng);
146146
let private_key = SecretKey::from_bytes(&private_key_scalar.to_bytes()).unwrap();
147-
let public_key = private_key.public_key().as_affine().to_encoded_point(true);
147+
let public_key: [u8; 33] = private_key
148+
.public_key()
149+
.as_affine()
150+
.to_encoded_point(true)
151+
.as_bytes()
152+
.try_into()
153+
.unwrap();
148154
let mut qr_secret = [0u8; 16];
149155
OsRng::default().fill_bytes(&mut qr_secret);
150156

@@ -155,8 +161,8 @@ impl<'d> CableQrCodeDevice<'d> {
155161

156162
Self {
157163
qr_code: CableQrCode {
158-
public_key: ByteBuf::from(public_key.as_bytes()),
159-
qr_secret: ByteBuf::from(qr_secret),
164+
public_key: ByteArray::from(public_key),
165+
qr_secret: ByteArray::from(qr_secret),
160166
known_tunnel_domains_count: KNOWN_TUNNEL_DOMAINS.len() as u8,
161167
current_time: current_unix_time,
162168
operation_hint: hint,
@@ -251,11 +257,11 @@ impl<'d> Device<'d, Cable, CableChannel<'d>> for CableQrCodeDevice<'_> {
251257
let routing_id_str = hex::encode(&advert.routing_id);
252258
let _nonce_str = hex::encode(&advert.nonce);
253259

254-
let tunnel_id = &derive(&self.qr_code.qr_secret, None, KeyPurpose::TunnelID)[..16];
260+
let tunnel_id = &derive(&self.qr_code.qr_secret.as_ref(), None, KeyPurpose::TunnelID)[..16];
255261
let tunnel_id_str = hex::encode(&tunnel_id);
256262

257263
let psk: &[u8; 32] = &derive(
258-
&self.qr_code.qr_secret,
264+
&self.qr_code.qr_secret.as_ref(),
259265
Some(&advert.plaintext),
260266
KeyPurpose::PSK,
261267
)[..32]

0 commit comments

Comments
 (0)