use std::io::{BufRead, Read};
use buffer_redux::BufReader;
use log::{debug, warn};
use crate::armor::{self, BlockType};
use crate::errors::{Error, Result};
use crate::packet::{Packet, PacketParser};
pub trait Deserializable: Sized {
fn from_bytes(bytes: impl Read) -> Result<Self> {
let mut el = Self::from_bytes_many(bytes);
el.next().ok_or(Error::NoMatchingPacket)?
}
fn from_string(input: &str) -> Result<(Self, armor::Headers)> {
let (mut el, headers) = Self::from_string_many(input)?;
Ok((el.next().ok_or(Error::NoMatchingPacket)??, headers))
}
#[allow(clippy::type_complexity)]
fn from_string_many<'a>(
input: &'a str,
) -> Result<(Box<dyn Iterator<Item = Result<Self>> + 'a>, armor::Headers)> {
Self::from_armor_many_buf(input.as_bytes())
}
fn from_armor_single<R: Read>(input: R) -> Result<(Self, armor::Headers)> {
let (mut el, headers) = Self::from_armor_many(input)?;
Ok((el.next().ok_or(Error::NoMatchingPacket)??, headers))
}
fn from_armor_single_buf<R: BufRead>(input: R) -> Result<(Self, armor::Headers)> {
let (mut el, headers) = Self::from_armor_many_buf(input)?;
Ok((el.next().ok_or(Error::NoMatchingPacket)??, headers))
}
#[allow(clippy::type_complexity)]
fn from_armor_many<'a, R: Read + 'a>(
input: R,
) -> Result<(Box<dyn Iterator<Item = Result<Self>> + 'a>, armor::Headers)> {
Self::from_armor_many_buf(BufReader::new(input))
}
#[allow(clippy::type_complexity)]
fn from_armor_many_buf<'a, R: BufRead + 'a>(
input: R,
) -> Result<(Box<dyn Iterator<Item = Result<Self>> + 'a>, armor::Headers)> {
let mut dearmor = armor::Dearmor::new(input);
dearmor.read_header()?;
let typ = dearmor
.typ
.ok_or_else(|| format_err!("dearmor failed to retrieve armor type"))?;
match typ {
BlockType::PublicKey
| BlockType::PrivateKey
| BlockType::Message
| BlockType::MultiPartMessage(_, _)
| BlockType::Signature
| BlockType::CleartextMessage
| BlockType::File => {
let headers = dearmor.headers.clone();
if !Self::matches_block_type(typ) {
bail!("unexpected block type: {}", typ);
}
Ok((Self::from_bytes_many(dearmor), headers))
}
BlockType::PublicKeyPKCS1(_)
| BlockType::PublicKeyPKCS8
| BlockType::PublicKeyOpenssh
| BlockType::PrivateKeyPKCS1(_)
| BlockType::PrivateKeyPKCS8
| BlockType::PrivateKeyOpenssh => {
unimplemented_err!("key format {:?}", typ);
}
}
}
fn from_bytes_many<'a>(bytes: impl Read + 'a) -> Box<dyn Iterator<Item = Result<Self>> + 'a> {
let packets = PacketParser::new(bytes).filter_map(filter_parsed_packet_results);
Self::from_packets(packets.peekable())
}
fn from_packets<'a, I: Iterator<Item = Result<Packet>> + 'a>(
packets: std::iter::Peekable<I>,
) -> Box<dyn Iterator<Item = Result<Self>> + 'a>;
fn matches_block_type(typ: BlockType) -> bool;
#[allow(clippy::type_complexity)]
fn from_reader_single<'a, R: Read + 'a>(input: R) -> Result<(Self, Option<armor::Headers>)> {
Self::from_reader_single_buf(BufReader::new(input))
}
#[allow(clippy::type_complexity)]
fn from_reader_single_buf<'a, R: BufRead + 'a>(
mut input: R,
) -> Result<(Self, Option<armor::Headers>)> {
if !is_binary(&mut input)? {
let (keys, headers) = Self::from_armor_single(input)?;
Ok((keys, Some(headers)))
} else {
Ok((Self::from_bytes(input)?, None))
}
}
#[allow(clippy::type_complexity)]
fn from_reader_many<'a, R: Read + 'a>(
input: R,
) -> Result<(
Box<dyn Iterator<Item = Result<Self>> + 'a>,
Option<armor::Headers>,
)> {
Self::from_reader_many_buf(BufReader::new(input))
}
#[allow(clippy::type_complexity)]
fn from_reader_many_buf<'a, R: BufRead + 'a>(
mut input: R,
) -> Result<(
Box<dyn Iterator<Item = Result<Self>> + 'a>,
Option<armor::Headers>,
)> {
if !is_binary(&mut input)? {
let (keys, headers) = Self::from_armor_many_buf(input)?;
Ok((keys, Some(headers)))
} else {
Ok((Self::from_bytes_many(input), None))
}
}
}
pub(crate) fn filter_parsed_packet_results(p: Result<Packet>) -> Option<Result<Packet>> {
match &p {
Ok(Packet::Marker(_m)) => {
debug!("skipping marker packet");
None
}
Ok(_) => Some(p),
Err(e) => {
if let Error::Unsupported(e) = e {
warn!("skipping unsupported packet: {p:?}");
debug!("error: {e:?}");
return None;
}
if let Error::InvalidPacketContent(b) = &e {
let err: &Error = b; if let Error::Unsupported(e) = err {
warn!("skipping unsupported packet: {p:?}");
debug!("error: {e:?}");
return None;
}
if let Error::EllipticCurve(e) = err {
warn!("skipping bad elliptic curve data: {p:?}");
debug!("error: {e:?}");
return None;
}
}
if let Error::PacketIncomplete = e {
warn!("skipping incomplete packet: {p:?}");
return None;
}
Some(Err(Error::Message(format!(
"unexpected packet data: {e:?}"
))))
}
}
}
pub(crate) fn is_binary<R: BufRead>(input: &mut R) -> Result<bool> {
let buf = input.fill_buf()?;
if buf.is_empty() {
bail!("empty input");
}
let binary = buf[0] & 0x80 != 0;
Ok(binary)
}