rs-matter/matter/src/secure_channel/core.rs

102 lines
2.6 KiB
Rust
Raw Normal View History

/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-06-10 14:01:35 +00:00
use core::borrow::Borrow;
use core::cell::RefCell;
use log::error;
use crate::{
2023-04-24 08:17:08 +00:00
error::*,
fabric::FabricMgr,
2023-06-12 09:47:20 +00:00
mdns::Mdns,
2023-06-10 14:01:35 +00:00
secure_channel::{common::*, pake::Pake},
transport::{exchange::Exchange, packet::Packet},
2023-06-09 07:47:49 +00:00
utils::{epoch::Epoch, rand::Rand},
};
use super::{case::Case, pake::PaseMgr};
/* Handle messages related to the Secure Channel
*/
pub struct SecureChannel<'a> {
pase: &'a RefCell<PaseMgr>,
2023-06-10 14:01:35 +00:00
fabric: &'a RefCell<FabricMgr>,
2023-06-12 09:47:20 +00:00
mdns: &'a dyn Mdns,
2023-06-10 14:01:35 +00:00
rand: Rand,
}
impl<'a> SecureChannel<'a> {
2023-05-28 11:45:27 +00:00
#[inline(always)]
2023-06-09 07:47:49 +00:00
pub fn new<
T: Borrow<RefCell<FabricMgr>>
+ Borrow<RefCell<PaseMgr>>
2023-06-12 09:47:20 +00:00
+ Borrow<dyn Mdns + 'a>
2023-06-09 07:47:49 +00:00
+ Borrow<Epoch>
+ Borrow<Rand>,
>(
matter: &'a T,
) -> Self {
Self::wrap(
matter.borrow(),
matter.borrow(),
matter.borrow(),
*matter.borrow(),
)
}
#[inline(always)]
pub fn wrap(
pase: &'a RefCell<PaseMgr>,
2023-06-09 07:47:49 +00:00
fabric: &'a RefCell<FabricMgr>,
2023-06-12 09:47:20 +00:00
mdns: &'a dyn Mdns,
rand: Rand,
) -> Self {
2023-06-09 07:47:49 +00:00
Self {
2023-06-10 14:01:35 +00:00
fabric,
pase,
mdns,
2023-06-10 14:01:35 +00:00
rand,
}
}
2023-06-10 14:01:35 +00:00
pub async fn handle(
&self,
exchange: &mut Exchange<'_>,
rx: &mut Packet<'_>,
tx: &mut Packet<'_>,
) -> Result<(), Error> {
match rx.get_proto_opcode()? {
OpCode::PBKDFParamRequest => {
Pake::new(self.pase)
.handle(exchange, rx, tx, self.mdns)
.await
}
OpCode::CASESigma1 => {
Case::new(self.fabric, self.rand)
.handle(exchange, rx, tx)
.await
}
proto_opcode => {
error!("OpCode not handled: {:?}", proto_opcode);
Err(ErrorCode::InvalidOpcode.into())
}
}
}
}