crypto: Refactor the VerificationReqest struct a bit

master
Damir Jelić 2021-05-21 12:44:10 +02:00
parent 110b8eb8dd
commit 98c259dc1e
1 changed files with 63 additions and 52 deletions

View File

@ -140,12 +140,9 @@ impl<'a> TryFrom<&'a OutgoingContent> for ReadyContent<'a> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
/// TODO /// TODO
pub struct VerificationRequest { pub struct VerificationRequest {
inner: Arc<Mutex<InnerRequest>>,
account: ReadOnlyAccount,
other_user_id: Arc<UserId>,
private_cross_signing_identity: PrivateCrossSigningIdentity,
store: Arc<Box<dyn CryptoStore>>,
flow_id: Arc<FlowId>, flow_id: Arc<FlowId>,
other_user_id: Arc<UserId>,
inner: Arc<Mutex<InnerRequest>>,
} }
impl VerificationRequest { impl VerificationRequest {
@ -161,21 +158,15 @@ impl VerificationRequest {
let flow_id = (room_id.to_owned(), event_id.to_owned()).into(); let flow_id = (room_id.to_owned(), event_id.to_owned()).into();
let inner = Mutex::new(InnerRequest::Created(RequestState::new( let inner = Mutex::new(InnerRequest::Created(RequestState::new(
account.user_id(), account,
account.device_id(), private_cross_signing_identity,
store,
other_user, other_user,
&flow_id, &flow_id,
))) )))
.into(); .into();
Self { Self { flow_id: flow_id.into(), inner, other_user_id: other_user.to_owned().into() }
inner,
account,
private_cross_signing_identity,
store,
other_user_id: other_user.clone().into(),
flow_id: flow_id.into(),
}
} }
/// TODO /// TODO
@ -256,16 +247,14 @@ impl VerificationRequest {
) -> Self { ) -> Self {
Self { Self {
inner: Arc::new(Mutex::new(InnerRequest::Requested(RequestState::from_request_event( inner: Arc::new(Mutex::new(InnerRequest::Requested(RequestState::from_request_event(
account.user_id(), account,
account.device_id(), private_cross_signing_identity,
store,
sender, sender,
&flow_id, &flow_id,
content, content,
)))), )))),
account, other_user_id: sender.to_owned().into(),
other_user_id: sender.clone().into(),
private_cross_signing_identity,
store,
flow_id: flow_id.into(), flow_id: flow_id.into(),
} }
} }
@ -316,9 +305,9 @@ impl VerificationRequest {
FlowId::ToDevice(_) => todo!(), FlowId::ToDevice(_) => todo!(),
FlowId::InRoom(r, _) => s.clone().into_started_sas( FlowId::InRoom(r, _) => s.clone().into_started_sas(
&event.clone().into_full_event(r.to_owned()), &event.clone().into_full_event(r.to_owned()),
self.store.clone(), s.store.clone(),
self.account.clone(), s.account.clone(),
self.private_cross_signing_identity.clone(), s.private_cross_signing_identity.clone(),
device, device,
user_identity, user_identity,
), ),
@ -338,9 +327,9 @@ impl VerificationRequest {
InnerRequest::Ready(s) => match &s.state.flow_id { InnerRequest::Ready(s) => match &s.state.flow_id {
FlowId::ToDevice(_) => todo!(), FlowId::ToDevice(_) => todo!(),
FlowId::InRoom(_, _) => Some(s.clone().start_sas( FlowId::InRoom(_, _) => Some(s.clone().start_sas(
self.store.clone(), s.store.clone(),
self.account.clone(), s.account.clone(),
self.private_cross_signing_identity.clone(), s.private_cross_signing_identity.clone(),
device, device,
user_identity, user_identity,
)), )),
@ -378,6 +367,15 @@ impl InnerRequest {
} }
} }
fn other_user_id(&self) -> &UserId {
match self {
InnerRequest::Created(s) => &s.other_user_id,
InnerRequest::Requested(s) => &s.other_user_id,
InnerRequest::Ready(s) => &s.other_user_id,
InnerRequest::Passive(s) => &s.other_user_id,
}
}
fn accept(&mut self) -> Option<OutgoingContent> { fn accept(&mut self) -> Option<OutgoingContent> {
if let InnerRequest::Requested(s) = self { if let InnerRequest::Requested(s) = self {
let (state, content) = s.clone().accept(); let (state, content) = s.clone().accept();
@ -415,11 +413,10 @@ impl InnerRequest {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct RequestState<S: Clone> { struct RequestState<S: Clone> {
/// Our own user id. account: ReadOnlyAccount,
pub own_user_id: UserId, private_cross_signing_identity: PrivateCrossSigningIdentity,
store: Arc<Box<dyn CryptoStore>>,
/// Our own device id. flow_id: Arc<FlowId>,
pub own_device_id: DeviceIdBox,
/// The id of the user which is participating in this verification request. /// The id of the user which is participating in this verification request.
pub other_user_id: UserId, pub other_user_id: UserId,
@ -430,24 +427,29 @@ struct RequestState<S: Clone> {
impl RequestState<Created> { impl RequestState<Created> {
fn new( fn new(
own_user_id: &UserId, account: ReadOnlyAccount,
own_device_id: &DeviceId, private_identity: PrivateCrossSigningIdentity,
store: Arc<Box<dyn CryptoStore>>,
other_user_id: &UserId, other_user_id: &UserId,
flow_id: &FlowId, flow_id: &FlowId,
) -> Self { ) -> Self {
Self { Self {
own_user_id: own_user_id.to_owned(), account,
own_device_id: own_device_id.to_owned(),
other_user_id: other_user_id.to_owned(), other_user_id: other_user_id.to_owned(),
private_cross_signing_identity: private_identity,
state: Created { methods: SUPPORTED_METHODS.to_vec(), flow_id: flow_id.to_owned() }, state: Created { methods: SUPPORTED_METHODS.to_vec(), flow_id: flow_id.to_owned() },
store,
flow_id: flow_id.to_owned().into(),
} }
} }
fn into_ready(self, _sender: &UserId, content: ReadyContent) -> RequestState<Ready> { fn into_ready(self, _sender: &UserId, content: ReadyContent) -> RequestState<Ready> {
// TODO check the flow id, and that the methods match what we suggested. // TODO check the flow id, and that the methods match what we suggested.
RequestState { RequestState {
own_user_id: self.own_user_id, account: self.account,
own_device_id: self.own_device_id, flow_id: self.flow_id,
private_cross_signing_identity: self.private_cross_signing_identity,
store: self.store,
other_user_id: self.other_user_id, other_user_id: self.other_user_id,
state: Ready { state: Ready {
methods: content.methods().to_owned(), methods: content.methods().to_owned(),
@ -483,16 +485,19 @@ struct Requested {
impl RequestState<Requested> { impl RequestState<Requested> {
fn from_request_event( fn from_request_event(
own_user_id: &UserId, account: ReadOnlyAccount,
own_device_id: &DeviceId, private_identity: PrivateCrossSigningIdentity,
store: Arc<Box<dyn CryptoStore>>,
sender: &UserId, sender: &UserId,
flow_id: &FlowId, flow_id: &FlowId,
content: RequestContent, content: RequestContent,
) -> RequestState<Requested> { ) -> RequestState<Requested> {
// TODO only create this if we suport the methods // TODO only create this if we suport the methods
RequestState { RequestState {
own_user_id: own_user_id.clone(), account,
own_device_id: own_device_id.into(), private_cross_signing_identity: private_identity,
store,
flow_id: flow_id.to_owned().into(),
other_user_id: sender.clone(), other_user_id: sender.clone(),
state: Requested { state: Requested {
methods: content.methods().to_owned(), methods: content.methods().to_owned(),
@ -504,26 +509,32 @@ impl RequestState<Requested> {
fn accept(self) -> (RequestState<Ready>, OutgoingContent) { fn accept(self) -> (RequestState<Ready>, OutgoingContent) {
let state = RequestState { let state = RequestState {
own_user_id: self.own_user_id, account: self.account.clone(),
own_device_id: self.own_device_id.clone(), store: self.store,
private_cross_signing_identity: self.private_cross_signing_identity,
flow_id: self.flow_id,
other_user_id: self.other_user_id, other_user_id: self.other_user_id,
state: Ready { state: Ready {
methods: self.state.methods.clone(), methods: SUPPORTED_METHODS.to_vec(),
other_device_id: self.state.other_device_id.clone(), other_device_id: self.state.other_device_id.clone(),
flow_id: self.state.flow_id.clone(), flow_id: self.state.flow_id.clone(),
}, },
}; };
let content = match self.state.flow_id { let content = match self.state.flow_id {
FlowId::ToDevice(i) => AnyToDeviceEventContent::KeyVerificationReady( FlowId::ToDevice(i) => {
ReadyToDeviceEventContent::new(self.own_device_id, self.state.methods, i), AnyToDeviceEventContent::KeyVerificationReady(ReadyToDeviceEventContent::new(
) self.account.device_id().to_owned(),
.into(), SUPPORTED_METHODS.to_vec(),
i,
))
.into()
}
FlowId::InRoom(r, e) => ( FlowId::InRoom(r, e) => (
r, r,
AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new( AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new(
self.own_device_id, self.account.device_id().to_owned(),
self.state.methods, SUPPORTED_METHODS.to_vec(),
Relation::new(e), Relation::new(e),
)), )),
) )
@ -599,7 +610,7 @@ struct Passive {
/// The event id of the `m.key.verification.request` event which acts as an /// The event id of the `m.key.verification.request` event which acts as an
/// unique id identifying this verification flow. /// unique id identifying this verification flow.
pub flow_id: EventId, pub flow_id: FlowId,
} }
#[cfg(test)] #[cfg(test)]