use std::{
convert::TryFrom,
fmt,
io::{self, Cursor, Read, Write},
};
use crate::{
content, crypto,
identity::Private as PrivateIdentity,
io::{ReadFrom, SizedReadFrom, WriteTo},
message,
object::{DecryptError, Header, ObjectKind, ObjectType, ObjectVersion, TryFromObjectTypeError},
var_type::VarInt,
};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Msg {
encrypted: Vec<u8>,
}
impl Msg {
pub fn new(encrypted: Vec<u8>) -> Self {
Self { encrypted }
}
pub fn encrypted(&self) -> &[u8] {
&self.encrypted
}
fn signed_header(&self, header: &Header) -> Result<Vec<u8>, io::Error> {
let mut bytes = Vec::new();
header.expires_time().write_to(&mut bytes)?;
header.object_type().write_to(&mut bytes)?;
VarInt::new(1).write_to(&mut bytes)?;
header.stream_number().write_to(&mut bytes)?;
Ok(bytes)
}
pub fn decrypt(
&self,
header: &Header,
identity: &PrivateIdentity,
) -> Result<content::Msg, DecryptError> {
let mut bytes = Cursor::new(self.encrypted());
let encrypted = crypto::Encrypted::sized_read_from(&mut bytes, self.encrypted().len())?;
let private_key = identity.private_encryption_key();
let decrypted = encrypted.decrypt(&private_key)?;
let mut bytes = Cursor::new(decrypted);
let content = content::Msg::read_from(&mut bytes)?;
if content.stream_number() != header.stream_number() {
return Err(DecryptError::StreamsNotMatch {
headers: header.stream_number(),
contents: content.stream_number(),
});
}
content.verify(self.signed_header(header)?)?;
Ok(content)
}
}
impl WriteTo for Msg {
fn write_to(&self, w: &mut dyn Write) -> io::Result<()> {
self.encrypted.write_to(w)?;
Ok(())
}
}
impl SizedReadFrom for Msg {
fn sized_read_from(r: &mut dyn Read, len: usize) -> io::Result<Self>
where
Self: Sized,
{
let encrypted = Vec::<u8>::sized_read_from(r, len)?;
Ok(Self { encrypted })
}
}
#[derive(Debug)]
pub enum TryIntoMsgError {
InvalidType(ObjectType),
IoError(io::Error),
UnsupportedVersion(ObjectVersion),
}
impl fmt::Display for TryIntoMsgError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidType(object_type) => write!(f, "invalid object type: {}", object_type),
Self::IoError(err) => err.fmt(f),
Self::UnsupportedVersion(version) => {
write!(f, "unsupported broadcast version: {}", version)
}
}
}
}
impl std::error::Error for TryIntoMsgError {}
impl From<io::Error> for TryIntoMsgError {
fn from(err: io::Error) -> Self {
Self::IoError(err)
}
}
impl TryFrom<message::Object> for Msg {
type Error = TryIntoMsgError;
fn try_from(
object: message::Object,
) -> Result<Self, <Self as TryFrom<message::Object>>::Error> {
let kind = ObjectKind::try_from(object.header().object_type());
if let Err(TryFromObjectTypeError(object_type)) = kind {
return Err(TryIntoMsgError::InvalidType(object_type));
}
if kind.unwrap() != ObjectKind::Msg {
return Err(TryIntoMsgError::InvalidType(object.header().object_type()));
}
let mut bytes = Cursor::new(object.object_payload());
let msg = Msg::sized_read_from(&mut bytes, object.object_payload().len())?;
Ok(msg)
}
}