Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a49a23828f | |||
|
|
188caf56cc |
12 changed files with 124 additions and 157 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1485,7 +1485,6 @@ dependencies = [
|
||||||
"mtp-host",
|
"mtp-host",
|
||||||
"mtp-transport",
|
"mtp-transport",
|
||||||
"quinn",
|
"quinn",
|
||||||
"rand",
|
|
||||||
"rcgen",
|
"rcgen",
|
||||||
"rustls",
|
"rustls",
|
||||||
"thiserror 2.0.20",
|
"thiserror 2.0.20",
|
||||||
|
|
|
||||||
|
|
@ -200,14 +200,13 @@ pub(crate) async fn expire_pending_request(
|
||||||
let mut expired = dispatcher.expired_requests.lock().await;
|
let mut expired = dispatcher.expired_requests.lock().await;
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
expired.retain(|_, expires_at| *expires_at > now);
|
expired.retain(|_, expires_at| *expires_at > now);
|
||||||
if expired.len() >= MAX_EXPIRED_REQUEST_TOMBSTONES {
|
if expired.len() >= MAX_EXPIRED_REQUEST_TOMBSTONES
|
||||||
if let Some(oldest) = expired
|
&& let Some(oldest) = expired
|
||||||
.iter()
|
.iter()
|
||||||
.min_by_key(|(_, expires_at)| **expires_at)
|
.min_by_key(|(_, expires_at)| **expires_at)
|
||||||
.map(|(id, _)| *id)
|
.map(|(id, _)| *id)
|
||||||
{
|
{
|
||||||
expired.remove(&oldest);
|
expired.remove(&oldest);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
expired.insert(request_id, now + EXPIRED_REQUEST_TOMBSTONE_TTL);
|
expired.insert(request_id, now + EXPIRED_REQUEST_TOMBSTONE_TTL);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@ pub use mtp_common::{CodecError, TimeError, unix_time_millis};
|
||||||
#[cfg(feature = "crypto")]
|
#[cfg(feature = "crypto")]
|
||||||
pub use protected::{
|
pub use protected::{
|
||||||
CURRENT_PROTECTED_VERSION, InMemoryReplayGuard, ProtectedError, ProtectedMessageBuilder,
|
CURRENT_PROTECTED_VERSION, InMemoryReplayGuard, ProtectedError, ProtectedMessageBuilder,
|
||||||
ReplayError, ReplayGuard, VerifiedProtectedMessage, open_protected, open_protected_with,
|
ProtectedOpenOptions, ReplayError, ReplayGuard, VerifiedProtectedMessage, open_protected,
|
||||||
open_protected_with_keys, protected_claimed_signer_id,
|
open_protected_with, open_protected_with_keys, protected_claimed_signer_id,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "crypto")]
|
#[cfg(feature = "crypto")]
|
||||||
pub use relay::{
|
pub use relay::{
|
||||||
|
|
|
||||||
|
|
@ -247,6 +247,35 @@ pub struct VerifiedProtectedMessage {
|
||||||
pub matched_signer_key_index: usize,
|
pub matched_signer_key_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Options that control verification of a direct protected message.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct ProtectedOpenOptions {
|
||||||
|
/// Require the protected frame to be addressed to this receiver when set.
|
||||||
|
pub expected_receiver_id: Option<u64>,
|
||||||
|
/// Purpose used to verify the protected envelope signature.
|
||||||
|
pub signature_purpose: ProtectionPurpose,
|
||||||
|
/// Purpose used to decrypt the protected envelope.
|
||||||
|
pub encryption_purpose: ProtectionPurpose,
|
||||||
|
/// Signature algorithms accepted by the receiver.
|
||||||
|
pub policy: ProtectionPolicy,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProtectedOpenOptions {
|
||||||
|
pub const fn new(
|
||||||
|
expected_receiver_id: Option<u64>,
|
||||||
|
signature_purpose: ProtectionPurpose,
|
||||||
|
encryption_purpose: ProtectionPurpose,
|
||||||
|
policy: ProtectionPolicy,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
expected_receiver_id,
|
||||||
|
signature_purpose,
|
||||||
|
encryption_purpose,
|
||||||
|
policy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn protected_field_id(
|
fn protected_field_id(
|
||||||
data_type: DataType,
|
data_type: DataType,
|
||||||
type_map: &TypeMap,
|
type_map: &TypeMap,
|
||||||
|
|
@ -385,10 +414,7 @@ pub fn open_protected_with<F>(
|
||||||
keyrings: &[&Keyring],
|
keyrings: &[&Keyring],
|
||||||
expected_signer_id: Option<u64>,
|
expected_signer_id: Option<u64>,
|
||||||
resolve_signer_keys: F,
|
resolve_signer_keys: F,
|
||||||
expected_receiver_id: Option<u64>,
|
options: ProtectedOpenOptions,
|
||||||
signature_purpose: ProtectionPurpose,
|
|
||||||
encryption_purpose: ProtectionPurpose,
|
|
||||||
policy: ProtectionPolicy,
|
|
||||||
replay_guard: Option<&mut dyn ReplayGuard>,
|
replay_guard: Option<&mut dyn ReplayGuard>,
|
||||||
) -> Result<VerifiedProtectedMessage, ProtectedError>
|
) -> Result<VerifiedProtectedMessage, ProtectedError>
|
||||||
where
|
where
|
||||||
|
|
@ -396,7 +422,7 @@ where
|
||||||
{
|
{
|
||||||
validate_protected_frame(frame)?;
|
validate_protected_frame(frame)?;
|
||||||
let type_map = frame.type_map().cloned().unwrap_or_else(TypeMap::latest);
|
let type_map = frame.type_map().cloned().unwrap_or_else(TypeMap::latest);
|
||||||
let decrypted = decrypt_protected_payload(frame, keyrings, encryption_purpose)?;
|
let decrypted = decrypt_protected_payload(frame, keyrings, options.encryption_purpose)?;
|
||||||
let signed = decrypted
|
let signed = decrypted
|
||||||
.as_signed()
|
.as_signed()
|
||||||
.ok_or(ProtectedError::PayloadNotSigned)?;
|
.ok_or(ProtectedError::PayloadNotSigned)?;
|
||||||
|
|
@ -411,16 +437,7 @@ where
|
||||||
}
|
}
|
||||||
let signer_keys = resolve_signer_keys(signed.signer_id)
|
let signer_keys = resolve_signer_keys(signed.signer_id)
|
||||||
.ok_or(ProtectionError::SignerKeyNotFound(signed.signer_id))?;
|
.ok_or(ProtectionError::SignerKeyNotFound(signed.signer_id))?;
|
||||||
open_decrypted_protected(
|
open_decrypted_protected(frame, type_map, signed, &signer_keys, options, replay_guard)
|
||||||
frame,
|
|
||||||
type_map,
|
|
||||||
signed,
|
|
||||||
&signer_keys,
|
|
||||||
expected_receiver_id,
|
|
||||||
signature_purpose,
|
|
||||||
policy,
|
|
||||||
replay_guard,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open a direct protected message against already resolved trusted signer
|
/// Open a direct protected message against already resolved trusted signer
|
||||||
|
|
@ -431,14 +448,11 @@ pub fn open_protected_with_keys(
|
||||||
keyrings: &[&Keyring],
|
keyrings: &[&Keyring],
|
||||||
expected_signer_id: u64,
|
expected_signer_id: u64,
|
||||||
signer_public_keys: &[PublicKeyBundle],
|
signer_public_keys: &[PublicKeyBundle],
|
||||||
expected_receiver_id: Option<u64>,
|
options: ProtectedOpenOptions,
|
||||||
signature_purpose: ProtectionPurpose,
|
|
||||||
encryption_purpose: ProtectionPurpose,
|
|
||||||
policy: ProtectionPolicy,
|
|
||||||
replay_guard: Option<&mut dyn ReplayGuard>,
|
replay_guard: Option<&mut dyn ReplayGuard>,
|
||||||
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
||||||
let type_map = validate_protected_frame(frame)?;
|
let type_map = validate_protected_frame(frame)?;
|
||||||
let decrypted = decrypt_protected_payload(frame, keyrings, encryption_purpose)?;
|
let decrypted = decrypt_protected_payload(frame, keyrings, options.encryption_purpose)?;
|
||||||
let signed = decrypted
|
let signed = decrypted
|
||||||
.as_signed()
|
.as_signed()
|
||||||
.ok_or(ProtectedError::PayloadNotSigned)?;
|
.ok_or(ProtectedError::PayloadNotSigned)?;
|
||||||
|
|
@ -454,9 +468,7 @@ pub fn open_protected_with_keys(
|
||||||
type_map,
|
type_map,
|
||||||
signed,
|
signed,
|
||||||
signer_public_keys,
|
signer_public_keys,
|
||||||
expected_receiver_id,
|
options,
|
||||||
signature_purpose,
|
|
||||||
policy,
|
|
||||||
replay_guard,
|
replay_guard,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -468,21 +480,15 @@ pub fn open_protected(
|
||||||
keyring: &Keyring,
|
keyring: &Keyring,
|
||||||
expected_signer_id: u64,
|
expected_signer_id: u64,
|
||||||
signer_public_key: &PublicKeyBundle,
|
signer_public_key: &PublicKeyBundle,
|
||||||
expected_receiver_id: Option<u64>,
|
options: ProtectedOpenOptions,
|
||||||
signature_purpose: ProtectionPurpose,
|
|
||||||
encryption_purpose: ProtectionPurpose,
|
|
||||||
policy: ProtectionPolicy,
|
|
||||||
replay_guard: Option<&mut dyn ReplayGuard>,
|
replay_guard: Option<&mut dyn ReplayGuard>,
|
||||||
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
||||||
open_protected_with_keys(
|
open_protected_with_keys(
|
||||||
frame,
|
frame,
|
||||||
std::slice::from_ref(&keyring),
|
std::slice::from_ref(&keyring),
|
||||||
expected_signer_id,
|
expected_signer_id,
|
||||||
std::slice::from_ref(&signer_public_key),
|
std::slice::from_ref(signer_public_key),
|
||||||
expected_receiver_id,
|
options,
|
||||||
signature_purpose,
|
|
||||||
encryption_purpose,
|
|
||||||
policy,
|
|
||||||
replay_guard,
|
replay_guard,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -492,19 +498,20 @@ fn open_decrypted_protected(
|
||||||
type_map: TypeMap,
|
type_map: TypeMap,
|
||||||
signed: &crate::SignedValue,
|
signed: &crate::SignedValue,
|
||||||
signer_public_keys: &[PublicKeyBundle],
|
signer_public_keys: &[PublicKeyBundle],
|
||||||
expected_receiver_id: Option<u64>,
|
options: ProtectedOpenOptions,
|
||||||
signature_purpose: ProtectionPurpose,
|
|
||||||
policy: ProtectionPolicy,
|
|
||||||
mut replay_guard: Option<&mut dyn ReplayGuard>,
|
mut replay_guard: Option<&mut dyn ReplayGuard>,
|
||||||
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
) -> Result<VerifiedProtectedMessage, ProtectedError> {
|
||||||
let matched_signer_key_index = signed.verify_with_key_history_index(
|
let matched_signer_key_index = signed.verify_with_key_history_index(
|
||||||
signed.signer_id,
|
signed.signer_id,
|
||||||
signer_public_keys,
|
signer_public_keys,
|
||||||
signature_purpose,
|
options.signature_purpose,
|
||||||
policy,
|
options.policy,
|
||||||
)?;
|
)?;
|
||||||
let receiver_id = frame.receiver().ok_or(ProtectedError::MissingReceiver)?;
|
let receiver_id = frame.receiver().ok_or(ProtectedError::MissingReceiver)?;
|
||||||
if expected_receiver_id.is_some_and(|expected| expected != receiver_id) {
|
if options
|
||||||
|
.expected_receiver_id
|
||||||
|
.is_some_and(|expected| expected != receiver_id)
|
||||||
|
{
|
||||||
return Err(ProtectedError::ExpectedReceiverMismatch);
|
return Err(ProtectedError::ExpectedReceiverMismatch);
|
||||||
}
|
}
|
||||||
if frame
|
if frame
|
||||||
|
|
@ -568,6 +575,15 @@ mod tests {
|
||||||
const SIGNATURE_PURPOSE: ProtectionPurpose = ProtectionPurpose(0x40);
|
const SIGNATURE_PURPOSE: ProtectionPurpose = ProtectionPurpose(0x40);
|
||||||
const ENCRYPTION_PURPOSE: ProtectionPurpose = ProtectionPurpose(0x41);
|
const ENCRYPTION_PURPOSE: ProtectionPurpose = ProtectionPurpose(0x41);
|
||||||
|
|
||||||
|
fn open_options(expected_receiver_id: Option<u64>) -> ProtectedOpenOptions {
|
||||||
|
ProtectedOpenOptions::new(
|
||||||
|
expected_receiver_id,
|
||||||
|
SIGNATURE_PURPOSE,
|
||||||
|
ENCRYPTION_PURPOSE,
|
||||||
|
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct RecordingReplayGuard {
|
struct RecordingReplayGuard {
|
||||||
created_at: Option<u64>,
|
created_at: Option<u64>,
|
||||||
|
|
@ -720,10 +736,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
Some(&mut guard),
|
Some(&mut guard),
|
||||||
)
|
)
|
||||||
.expect("protected message should open");
|
.expect("protected message should open");
|
||||||
|
|
@ -737,10 +750,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
Some(&mut guard),
|
Some(&mut guard),
|
||||||
),
|
),
|
||||||
Err(ProtectedError::Replay)
|
Err(ProtectedError::Replay)
|
||||||
|
|
@ -777,10 +787,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("outer fields should verify");
|
.expect("outer fields should verify");
|
||||||
|
|
@ -813,10 +820,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::MissingProtectedVersion)
|
Err(ProtectedError::MissingProtectedVersion)
|
||||||
|
|
@ -844,10 +848,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::UnsupportedProtectedVersion(2))
|
Err(ProtectedError::UnsupportedProtectedVersion(2))
|
||||||
|
|
@ -897,10 +898,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
None,
|
open_options(None),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::MessageTypeMismatch)
|
Err(ProtectedError::MessageTypeMismatch)
|
||||||
|
|
@ -913,10 +911,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
None,
|
open_options(None),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::FinalRecipientMismatch)
|
Err(ProtectedError::FinalRecipientMismatch)
|
||||||
|
|
@ -944,10 +939,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
None,
|
open_options(None),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::FinalRecipientMismatch)
|
Err(ProtectedError::FinalRecipientMismatch)
|
||||||
|
|
@ -959,10 +951,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(43),
|
open_options(Some(43)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::ExpectedReceiverMismatch)
|
Err(ProtectedError::ExpectedReceiverMismatch)
|
||||||
|
|
@ -994,10 +983,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("matching exposed sender");
|
.expect("matching exposed sender");
|
||||||
|
|
@ -1007,10 +993,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::SenderMismatch)
|
Err(ProtectedError::SenderMismatch)
|
||||||
|
|
@ -1049,10 +1032,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::PayloadNotEncrypted)
|
Err(ProtectedError::PayloadNotEncrypted)
|
||||||
|
|
@ -1066,10 +1046,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
Err(ProtectedError::PayloadNotSigned)
|
Err(ProtectedError::PayloadNotSigned)
|
||||||
|
|
@ -1090,10 +1067,7 @@ mod tests {
|
||||||
resolver_calls += 1;
|
resolver_calls += 1;
|
||||||
Some(vec![sender.public_key_bundle()])
|
Some(vec![sender.public_key_bundle()])
|
||||||
},
|
},
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
|
|
@ -1138,10 +1112,7 @@ mod tests {
|
||||||
current_sender.public_key_bundle(),
|
current_sender.public_key_bundle(),
|
||||||
old_sender.public_key_bundle(),
|
old_sender.public_key_bundle(),
|
||||||
],
|
],
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("key history should open");
|
.expect("key history should open");
|
||||||
|
|
@ -1167,10 +1138,7 @@ mod tests {
|
||||||
&recipient,
|
&recipient,
|
||||||
7,
|
7,
|
||||||
&sender.public_key_bundle(),
|
&sender.public_key_bundle(),
|
||||||
Some(42),
|
open_options(Some(42)),
|
||||||
SIGNATURE_PURPOSE,
|
|
||||||
ENCRYPTION_PURPOSE,
|
|
||||||
ProtectionPolicy::from(crate::SignaturePolicy::Ed25519),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("arbitrary application value should open");
|
.expect("arbitrary application value should open");
|
||||||
|
|
|
||||||
1
example/Cargo.lock
generated
1
example/Cargo.lock
generated
|
|
@ -1404,7 +1404,6 @@ dependencies = [
|
||||||
"mtp-host",
|
"mtp-host",
|
||||||
"mtp-transport",
|
"mtp-transport",
|
||||||
"quinn",
|
"quinn",
|
||||||
"rand",
|
|
||||||
"rustls",
|
"rustls",
|
||||||
"thiserror 2.0.20",
|
"thiserror 2.0.20",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use mtp::codec::{
|
use mtp::codec::{
|
||||||
CommunicationType, CommunicationValue, DataType, DataTypeId, DataValue, InMemoryReplayGuard,
|
CommunicationType, CommunicationValue, DataType, DataTypeId, DataValue, InMemoryReplayGuard,
|
||||||
ProtectionPolicy, ProtectionPurpose, SignaturePolicy, TypeMap,
|
ProtectedOpenOptions, ProtectionPolicy, ProtectionPurpose, SignaturePolicy, TypeMap,
|
||||||
forward_relay_frame, open_protected_with, open_relay_content, open_relay_metadata_with,
|
forward_relay_frame, open_protected_with, open_relay_content, open_relay_metadata_with,
|
||||||
};
|
};
|
||||||
use mtp::crypto::{Keyring, PublicKeyBundle};
|
use mtp::crypto::{Keyring, PublicKeyBundle};
|
||||||
|
|
@ -70,10 +70,12 @@ fn process_direct_protected(
|
||||||
std::slice::from_ref(&host_keyring),
|
std::slice::from_ref(&host_keyring),
|
||||||
None,
|
None,
|
||||||
|signer_id| resolve_signer_key(signer_id, registered_clients).map(|key| vec![key]),
|
|signer_id| resolve_signer_key(signer_id, registered_clients).map(|key| vec![key]),
|
||||||
Some(DIRECT_DESTINATION_ID),
|
ProtectedOpenOptions::new(
|
||||||
ProtectionPurpose::from(DIRECT_SIGNATURE_PURPOSE),
|
Some(DIRECT_DESTINATION_ID),
|
||||||
ProtectionPurpose::from(DIRECT_ENCRYPTION_PURPOSE),
|
ProtectionPurpose::from(DIRECT_SIGNATURE_PURPOSE),
|
||||||
SIGNATURE_POLICY,
|
ProtectionPurpose::from(DIRECT_ENCRYPTION_PURPOSE),
|
||||||
|
SIGNATURE_POLICY,
|
||||||
|
),
|
||||||
Some(accepted_messages),
|
Some(accepted_messages),
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("direct protected message could not be authenticated: {e}"))?;
|
.map_err(|e| format!("direct protected message could not be authenticated: {e}"))?;
|
||||||
|
|
|
||||||
|
|
@ -344,7 +344,7 @@ impl HandshakeEngine {
|
||||||
// Authenticated clients include PublicKeys in Identification as an
|
// Authenticated clients include PublicKeys in Identification as an
|
||||||
// intent marker; this avoids acknowledging the opening as a guest
|
// intent marker; this avoids acknowledging the opening as a guest
|
||||||
// connection and leaving the client waiting for a Challenge.
|
// connection and leaving the client waiting for a Challenge.
|
||||||
if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(&tm)
|
if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(tm)
|
||||||
|| first_msg.get_data(DataType::PublicKeys).is_some()
|
|| first_msg.get_data(DataType::PublicKeys).is_some()
|
||||||
{
|
{
|
||||||
send_rejection_generic(
|
send_rejection_generic(
|
||||||
|
|
@ -400,7 +400,7 @@ impl HandshakeEngine {
|
||||||
let tm = codec.type_map();
|
let tm = codec.type_map();
|
||||||
|
|
||||||
// Register frames always go through full authentication
|
// Register frames always go through full authentication
|
||||||
if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(&tm) {
|
if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(tm) {
|
||||||
let bundle = match extract_register_bundle(&first_msg) {
|
let bundle = match extract_register_bundle(&first_msg) {
|
||||||
Ok(bundle) => bundle,
|
Ok(bundle) => bundle,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
|
@ -425,7 +425,7 @@ impl HandshakeEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identification: try lookup, fall back to guest
|
// Identification: try lookup, fall back to guest
|
||||||
if Some(first_msg.get_type()) == CommunicationType::Identification.try_to_id(&tm) {
|
if Some(first_msg.get_type()) == CommunicationType::Identification.try_to_id(tm) {
|
||||||
let cid = match first_msg.get_data(DataType::Id) {
|
let cid = match first_msg.get_data(DataType::Id) {
|
||||||
Some(DataValue::UnsignedNumber(n)) => u64::try_from(*n).unwrap_or(0),
|
Some(DataValue::UnsignedNumber(n)) => u64::try_from(*n).unwrap_or(0),
|
||||||
_ => 0,
|
_ => 0,
|
||||||
|
|
@ -504,7 +504,7 @@ impl HandshakeEngine {
|
||||||
let tm = codec.type_map();
|
let tm = codec.type_map();
|
||||||
|
|
||||||
let (flow, response_type) = if Some(first_msg.get_type())
|
let (flow, response_type) = if Some(first_msg.get_type())
|
||||||
== CommunicationType::Identification.try_to_id(&tm)
|
== CommunicationType::Identification.try_to_id(tm)
|
||||||
{
|
{
|
||||||
let cid = match first_msg.get_data(DataType::Id) {
|
let cid = match first_msg.get_data(DataType::Id) {
|
||||||
Some(DataValue::UnsignedNumber(n)) => match u64::try_from(*n) {
|
Some(DataValue::UnsignedNumber(n)) => match u64::try_from(*n) {
|
||||||
|
|
@ -545,7 +545,7 @@ impl HandshakeEngine {
|
||||||
Flow::Login { id: cid, bundle },
|
Flow::Login { id: cid, bundle },
|
||||||
CommunicationType::IdentificationResponse,
|
CommunicationType::IdentificationResponse,
|
||||||
)
|
)
|
||||||
} else if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(&tm) {
|
} else if Some(first_msg.get_type()) == CommunicationType::Register.try_to_id(tm) {
|
||||||
let bundle = match extract_register_bundle(&first_msg) {
|
let bundle = match extract_register_bundle(&first_msg) {
|
||||||
Ok(bundle) => bundle,
|
Ok(bundle) => bundle,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
|
@ -710,7 +710,7 @@ impl HandshakeEngine {
|
||||||
sender.close();
|
sender.close();
|
||||||
AcceptError::Receive(e)
|
AcceptError::Receive(e)
|
||||||
})?;
|
})?;
|
||||||
if Some(proof.get_type()) != CommunicationType::ChallengeResponse.try_to_id(&tm) {
|
if Some(proof.get_type()) != CommunicationType::ChallengeResponse.try_to_id(tm) {
|
||||||
let error = AcceptError::AuthenticationFailed("missing challenge response".into());
|
let error = AcceptError::AuthenticationFailed("missing challenge response".into());
|
||||||
reject_error_generic(sender, &error, tm).await;
|
reject_error_generic(sender, &error, tm).await;
|
||||||
return Err(error);
|
return Err(error);
|
||||||
|
|
@ -1041,8 +1041,8 @@ impl HandshakeSender for mtp_transport::Sender {
|
||||||
) -> impl std::future::Future<Output = Result<(), CommunicationError>> + Send {
|
) -> impl std::future::Future<Output = Result<(), CommunicationError>> + Send {
|
||||||
mtp_transport::Sender::finish_stream(self)
|
mtp_transport::Sender::finish_stream(self)
|
||||||
}
|
}
|
||||||
fn set_type_map(&self, type_map: &TypeMap) -> impl std::future::Future<Output = ()> + Send {
|
async fn set_type_map(&self, type_map: &TypeMap) {
|
||||||
async move { self.set_type_map(type_map).await }
|
self.set_type_map(type_map).await;
|
||||||
}
|
}
|
||||||
fn close(&self) {
|
fn close(&self) {
|
||||||
let sender = self.clone();
|
let sender = self.clone();
|
||||||
|
|
@ -1058,8 +1058,8 @@ impl HandshakeReceiver for mtp_transport::Receiver {
|
||||||
mtp_transport::Receiver::receive(self)
|
mtp_transport::Receiver::receive(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_type_map(&self, type_map: &TypeMap) -> impl std::future::Future<Output = ()> + Send {
|
async fn set_type_map(&self, type_map: &TypeMap) {
|
||||||
async move { self.set_type_map(type_map).await }
|
self.set_type_map(type_map).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1075,8 +1075,8 @@ impl<C: mtp_transport::TransportConnection> HandshakeSender for mtp_transport::G
|
||||||
) -> impl std::future::Future<Output = Result<(), CommunicationError>> + Send {
|
) -> impl std::future::Future<Output = Result<(), CommunicationError>> + Send {
|
||||||
mtp_transport::GenericSender::finish_stream(self)
|
mtp_transport::GenericSender::finish_stream(self)
|
||||||
}
|
}
|
||||||
fn set_type_map(&self, type_map: &TypeMap) -> impl std::future::Future<Output = ()> + Send {
|
async fn set_type_map(&self, type_map: &TypeMap) {
|
||||||
async move { self.set_type_map(type_map).await }
|
self.set_type_map(type_map).await;
|
||||||
}
|
}
|
||||||
fn close(&self) {
|
fn close(&self) {
|
||||||
mtp_transport::GenericSender::close(self);
|
mtp_transport::GenericSender::close(self);
|
||||||
|
|
@ -1093,8 +1093,8 @@ impl<C: mtp_transport::TransportConnection> HandshakeReceiver
|
||||||
mtp_transport::GenericReceiver::receive(self)
|
mtp_transport::GenericReceiver::receive(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_type_map(&self, type_map: &TypeMap) -> impl std::future::Future<Output = ()> + Send {
|
async fn set_type_map(&self, type_map: &TypeMap) {
|
||||||
async move { self.set_type_map(type_map).await }
|
self.set_type_map(type_map).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ rustls = "0.23"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
thiserror = "2"
|
thiserror = "2"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
rand = { version = "0.10.1", optional = true }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rcgen = "0.14"
|
rcgen = "0.14"
|
||||||
|
|
@ -33,5 +32,5 @@ hyper = { version = "1", features = ["client", "http2"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
crypto = ["mtp-host/crypto", "dep:rand"]
|
crypto = ["mtp-host/crypto"]
|
||||||
pipes = ["mtp-host/pipes", "mtp-transport/pipes"]
|
pipes = ["mtp-host/pipes", "mtp-transport/pipes"]
|
||||||
|
|
|
||||||
|
|
@ -220,6 +220,7 @@ pub type WebMtpReceiver = GenericReceiver<H3TransportConnection>;
|
||||||
pub type WebMTPConnection =
|
pub type WebMTPConnection =
|
||||||
mtp_host::MTPConnection<WebMtpSender, WebMtpReceiver, H3TransportReceiver>;
|
mtp_host::MTPConnection<WebMtpSender, WebMtpReceiver, H3TransportReceiver>;
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub(crate) async fn accept_web_connection(
|
pub(crate) async fn accept_web_connection(
|
||||||
session: Arc<Session>,
|
session: Arc<Session>,
|
||||||
path: String,
|
path: String,
|
||||||
|
|
@ -268,6 +269,7 @@ pub(crate) async fn accept_web_connection(
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
async fn accept_web_connection_inner(
|
async fn accept_web_connection_inner(
|
||||||
session: Arc<Session>,
|
session: Arc<Session>,
|
||||||
path: String,
|
path: String,
|
||||||
|
|
|
||||||
|
|
@ -1078,18 +1078,12 @@ impl Receiver {
|
||||||
#[instrument(skip(self), level = "trace")]
|
#[instrument(skip(self), level = "trace")]
|
||||||
pub async fn receive(&self) -> Result<CommunicationValue, CommunicationError> {
|
pub async fn receive(&self) -> Result<CommunicationValue, CommunicationError> {
|
||||||
let mut close_rx = self.inner.handle.subscribe_close();
|
let mut close_rx = self.inner.handle.subscribe_close();
|
||||||
if close_rx.borrow().is_some() {
|
|
||||||
return Err(self
|
|
||||||
.inner
|
|
||||||
.handle
|
|
||||||
.close_reason()
|
|
||||||
.unwrap_or(CommunicationError::StreamClosed));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "pipes")]
|
#[cfg(feature = "pipes")]
|
||||||
{
|
{
|
||||||
let mut rx = self.inner.msg_rx.lock().await;
|
let mut rx = self.inner.msg_rx.lock().await;
|
||||||
let result = tokio::select! {
|
let result = tokio::select! {
|
||||||
|
biased;
|
||||||
message = rx.recv() => message,
|
message = rx.recv() => message,
|
||||||
_ = close_rx.changed() => return Err(close_rx
|
_ = close_rx.changed() => return Err(close_rx
|
||||||
.borrow()
|
.borrow()
|
||||||
|
|
@ -1113,6 +1107,7 @@ impl Receiver {
|
||||||
{
|
{
|
||||||
let mut rx = self.inner.rx.lock().await;
|
let mut rx = self.inner.rx.lock().await;
|
||||||
let result = tokio::select! {
|
let result = tokio::select! {
|
||||||
|
biased;
|
||||||
message = rx.recv() => message,
|
message = rx.recv() => message,
|
||||||
_ = close_rx.changed() => return Err(close_rx
|
_ = close_rx.changed() => return Err(close_rx
|
||||||
.borrow()
|
.borrow()
|
||||||
|
|
@ -1136,17 +1131,11 @@ impl Receiver {
|
||||||
#[cfg(feature = "pipes")]
|
#[cfg(feature = "pipes")]
|
||||||
#[instrument(skip(self), level = "trace")]
|
#[instrument(skip(self), level = "trace")]
|
||||||
pub async fn receive_event(&self) -> Result<TransportEvent, CommunicationError> {
|
pub async fn receive_event(&self) -> Result<TransportEvent, CommunicationError> {
|
||||||
if self.inner.handle.is_closed() {
|
let mut close_rx = self.inner.handle.subscribe_close();
|
||||||
return Err(self
|
|
||||||
.inner
|
|
||||||
.handle
|
|
||||||
.close_reason()
|
|
||||||
.unwrap_or(CommunicationError::StreamClosed));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut msg_rx = self.inner.msg_rx.lock().await;
|
let mut msg_rx = self.inner.msg_rx.lock().await;
|
||||||
let mut pipe_rx = self.inner.pipe_rx.lock().await;
|
let mut pipe_rx = self.inner.pipe_rx.lock().await;
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
|
biased;
|
||||||
msg = msg_rx.recv() => {
|
msg = msg_rx.recv() => {
|
||||||
match msg {
|
match msg {
|
||||||
Some(Ok(val)) => {
|
Some(Ok(val)) => {
|
||||||
|
|
@ -1174,6 +1163,10 @@ impl Receiver {
|
||||||
.unwrap_or(CommunicationError::StreamClosed)),
|
.unwrap_or(CommunicationError::StreamClosed)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ = close_rx.changed() => Err(close_rx
|
||||||
|
.borrow()
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(CommunicationError::StreamClosed)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -342,13 +342,17 @@ async fn test_max_frames_per_stream_enforced() -> Result<(), Box<dyn std::error:
|
||||||
let tm = TypeMap::latest();
|
let tm = TypeMap::latest();
|
||||||
client_tx
|
client_tx
|
||||||
.send(&numbered_message(CommunicationType::Ping, 1, &tm))
|
.send(&numbered_message(CommunicationType::Ping, 1, &tm))
|
||||||
.await?;
|
.await
|
||||||
let first = host_rx.receive().await?;
|
.expect("first frame should be sent");
|
||||||
|
let first = host_rx
|
||||||
|
.receive()
|
||||||
|
.await
|
||||||
|
.expect("first frame should be received");
|
||||||
assert_numbered_message(&first, CommunicationType::Ping, 1, &tm);
|
assert_numbered_message(&first, CommunicationType::Ping, 1, &tm);
|
||||||
|
|
||||||
client_tx
|
let _ = client_tx
|
||||||
.send(&numbered_message(CommunicationType::Ping, 2, &tm))
|
.send(&numbered_message(CommunicationType::Ping, 2, &tm))
|
||||||
.await?;
|
.await;
|
||||||
let second = host_rx.receive().await;
|
let second = host_rx.receive().await;
|
||||||
assert!(second.is_err(), "stream should be closed after frame limit");
|
assert!(second.is_err(), "stream should be closed after frame limit");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use mtp_codec::{
|
use mtp_codec::{
|
||||||
DataValue, ProtectedError, ProtectedMessageBuilder, ProtectionError, ProtectionPurpose,
|
DataValue, ProtectedError, ProtectedMessageBuilder, ProtectedOpenOptions, ProtectionError,
|
||||||
VerifiedProtectedMessage,
|
ProtectionPurpose, VerifiedProtectedMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::crypto::{
|
use crate::crypto::{
|
||||||
|
|
@ -273,10 +273,12 @@ pub fn open_protected_with_keyrings(
|
||||||
&references,
|
&references,
|
||||||
expected_signer_id,
|
expected_signer_id,
|
||||||
&signer_public_keys,
|
&signer_public_keys,
|
||||||
expected_receiver_id,
|
ProtectedOpenOptions::new(
|
||||||
ProtectionPurpose::from(signature_purpose),
|
expected_receiver_id,
|
||||||
ProtectionPurpose::from(encryption_purpose),
|
ProtectionPurpose::from(signature_purpose),
|
||||||
policy,
|
ProtectionPurpose::from(encryption_purpose),
|
||||||
|
policy,
|
||||||
|
),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.map_err(protected_error)?;
|
.map_err(protected_error)?;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue