summaryrefslogtreecommitdiffstats
path: root/openpgp/src/serialize
diff options
context:
space:
mode:
Diffstat (limited to 'openpgp/src/serialize')
-rw-r--r--openpgp/src/serialize/stream.rs73
-rw-r--r--openpgp/src/serialize/stream/padding.rs12
-rw-r--r--openpgp/src/serialize/stream/partial_body.rs10
-rw-r--r--openpgp/src/serialize/stream/writer/mod.rs28
-rw-r--r--openpgp/src/serialize/stream/writer/writer_bzip2.rs3
-rw-r--r--openpgp/src/serialize/stream/writer/writer_deflate.rs6
6 files changed, 78 insertions, 54 deletions
diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs
index 957c6963..87e587b0 100644
--- a/openpgp/src/serialize/stream.rs
+++ b/openpgp/src/serialize/stream.rs
@@ -282,8 +282,8 @@ impl<'a> Message<'a> {
/// message.finalize()?;
/// # Ok(()) }
/// ```
- pub fn finalize_one(self) -> Result<Option<Message<'a>>> {
- Ok(self.0.into_inner()?.map(|bs| Self::from(bs)))
+ pub async fn finalize_one(self) -> Result<Option<Message<'a>>> {
+ Ok(self.0.into_inner().await?.map(|bs| Self::from(bs)))
}
/// Finalizes the message.
@@ -313,9 +313,9 @@ impl<'a> Message<'a> {
/// message.finalize()?;
/// # Ok(()) }
/// ```
- pub fn finalize(self) -> Result<()> {
+ pub async fn finalize(self) -> Result<()> {
let mut stack = self;
- while let Some(s) = stack.finalize_one()? {
+ while let Some(s) = stack.finalize_one().await? {
stack = s;
}
Ok(())
@@ -586,9 +586,10 @@ impl<'a> Write for ArbitraryWriter<'a> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a> writer::Stackable<'a, Cookie> for ArbitraryWriter<'a> {
- fn into_inner(self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
- Box::new(self.inner).into_inner()
+ async fn into_inner(self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
+ Box::new(self.inner).into_inner().await
}
fn pop(&mut self) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
unreachable!("Only implemented by Signer")
@@ -1214,6 +1215,7 @@ impl<'a> Write for Signer<'a> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a> writer::Stackable<'a, Cookie> for Signer<'a> {
fn pop(&mut self) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
Ok(self.inner.take())
@@ -1231,10 +1233,9 @@ impl<'a> writer::Stackable<'a, Cookie> for Signer<'a> {
fn inner_ref(&self) -> Option<&dyn writer::Stackable<'a, Cookie>> {
self.inner.as_ref().map(|r| r.as_ref())
}
- fn into_inner(mut self: Box<Self>)
+ async fn into_inner(mut self: Box<Self>)
-> Result<Option<writer::BoxStack<'a, Cookie>>> {
- // XXX: OH SHIT
- futures::executor::block_on(self.emit_signatures())?;
+ self.emit_signatures().await?;
Ok(self.inner.take())
}
fn cookie_set(&mut self, cookie: Cookie) -> Cookie {
@@ -1496,12 +1497,13 @@ impl<'a> Write for LiteralWriter<'a> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a> writer::Stackable<'a, Cookie> for LiteralWriter<'a> {
- fn into_inner(mut self: Box<Self>)
+ async fn into_inner(mut self: Box<Self>)
-> Result<Option<writer::BoxStack<'a, Cookie>>> {
let signer = self.signature_writer.take();
let stack = self.inner
- .into_inner()?.unwrap(); // Peel off the PartialBodyFilter.
+ .into_inner().await?.unwrap(); // Peel off the PartialBodyFilter.
if let Some(mut signer) = signer {
// We stashed away a Signer. Reattach it to the
@@ -1745,9 +1747,10 @@ impl<'a> io::Write for Compressor<'a> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a> writer::Stackable<'a, Cookie> for Compressor<'a> {
- fn into_inner(self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
- Box::new(self.inner).into_inner()?.unwrap().into_inner()
+ async fn into_inner(self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
+ Box::new(self.inner).into_inner().await?.unwrap().into_inner().await
}
fn pop(&mut self) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
unreachable!("Only implemented by Signer")
@@ -2578,7 +2581,7 @@ impl<'a> Encryptor<'a> {
}
/// Emits the MDC packet and recovers the original writer.
- fn emit_mdc(&mut self) -> Result<writer::BoxStack<'a, Cookie>> {
+ async fn emit_mdc(&mut self) -> Result<writer::BoxStack<'a, Cookie>> {
if let Some(mut w) = self.inner.take() {
// Write the MDC, which must be the last packet inside the
// encrypted packet stream. The hash includes the MDC's
@@ -2592,9 +2595,9 @@ impl<'a> Encryptor<'a> {
// Now recover the original writer. First, strip the
// Encryptor.
- let w = w.into_inner()?.unwrap();
+ let w = w.into_inner().await?.unwrap();
// And the partial body filter.
- let w = w.into_inner()?.unwrap();
+ let w = w.into_inner().await?.unwrap();
Ok(w)
} else {
@@ -2632,6 +2635,7 @@ impl<'a> Write for Encryptor<'a> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a> writer::Stackable<'a, Cookie> for Encryptor<'a> {
fn pop(&mut self) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
unreachable!("Only implemented by Signer")
@@ -2650,8 +2654,8 @@ impl<'a> writer::Stackable<'a, Cookie> for Encryptor<'a> {
None
}
}
- fn into_inner(mut self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
- Ok(Some(self.emit_mdc()?))
+ async fn into_inner(mut self: Box<Self>) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
+ Ok(Some(self.emit_mdc().await?))
}
fn cookie_set(&mut self, cookie: Cookie) -> Cookie {
::std::mem::replace(&mut self.cookie, cookie)
@@ -2669,6 +2673,7 @@ impl<'a> writer::Stackable<'a, Cookie> for Encryptor<'a> {
#[cfg(test)]
mod test {
+ use futures::FutureExt;
use std::io::Read;
use crate::{Packet, PacketPile, packet::CompressedData};
use crate::parse::{Parse, PacketParserResult, PacketParser};
@@ -2687,7 +2692,7 @@ mod test {
ustr.write_all(b"\x00").unwrap(); // fn length
ustr.write_all(b"\x00\x00\x00\x00").unwrap(); // date
ustr.write_all(b"Hello world.").unwrap(); // body
- ustr.finalize().unwrap();
+ ustr.finalize().now_or_never().unwrap().unwrap();
}
let mut pp = PacketParser::from_bytes(&o).unwrap().unwrap();
@@ -2737,14 +2742,14 @@ mod test {
.algo(CompressionAlgorithm::Uncompressed).build().unwrap();
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "one").unwrap();
- let c = ls.finalize_one().unwrap().unwrap(); // Pop the LiteralWriter.
+ let c = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap(); // Pop the LiteralWriter.
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "two").unwrap();
- let c = ls.finalize_one().unwrap().unwrap(); // Pop the LiteralWriter.
- let c = c.finalize_one().unwrap().unwrap(); // Pop the Compressor.
+ let c = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap(); // Pop the LiteralWriter.
+ let c = c.finalize_one().now_or_never().unwrap().unwrap().unwrap(); // Pop the Compressor.
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "three").unwrap();
- ls.finalize().unwrap();
+ ls.finalize().now_or_never().unwrap().unwrap();
}
let pile = PacketPile::from(reference);
@@ -2799,19 +2804,19 @@ mod test {
.algo(CompressionAlgorithm::Uncompressed).build().unwrap();
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "one").unwrap();
- let c = ls.finalize_one().unwrap().unwrap();
+ let c = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap();
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "two").unwrap();
- let c = ls.finalize_one().unwrap().unwrap();
- let c0 = c.finalize_one().unwrap().unwrap();
+ let c = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap();
+ let c0 = c.finalize_one().now_or_never().unwrap().unwrap().unwrap();
let c = Compressor::new(c0)
.algo(CompressionAlgorithm::Uncompressed).build().unwrap();
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "three").unwrap();
- let c = ls.finalize_one().unwrap().unwrap();
+ let c = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap();
let mut ls = LiteralWriter::new(c).format(T).build().unwrap();
write!(ls, "four").unwrap();
- ls.finalize().unwrap();
+ ls.finalize().now_or_never().unwrap().unwrap();
}
let pile = PacketPile::from(reference);
@@ -2877,7 +2882,7 @@ mod test {
let signer = signer.build().unwrap();
let mut ls = LiteralWriter::new(signer).build().unwrap();
ls.write_all(b"Tis, tis, tis. Tis is important.").unwrap();
- let _ = ls.finalize().unwrap();
+ let _ = ls.finalize().now_or_never().unwrap().unwrap();
}
let mut ppr = PacketParser::from_bytes(&o).unwrap();
@@ -2911,7 +2916,7 @@ mod test {
let mut literal = LiteralWriter::new(encryptor).build()
.unwrap();
literal.write_all(message).unwrap();
- literal.finalize().unwrap();
+ literal.finalize().now_or_never().unwrap().unwrap();
}
// ... and recover it...
@@ -3097,7 +3102,7 @@ mod test {
let mut literal = LiteralWriter::new(encryptor).build()
.unwrap();
literal.write_all(&content).unwrap();
- literal.finalize().unwrap();
+ literal.finalize().now_or_never().unwrap().unwrap();
}
for &read_len in &[
@@ -3201,8 +3206,8 @@ mod test {
let mut ls = LiteralWriter::new(signer).build().unwrap();
ls.write_all(b"Tis, tis, tis. Tis is important.").unwrap();
- let signer = ls.finalize_one().unwrap().unwrap();
- let _ = signer.finalize_one().unwrap().unwrap();
+ let signer = ls.finalize_one().now_or_never().unwrap().unwrap().unwrap();
+ let _ = signer.finalize_one().now_or_never().unwrap().unwrap().unwrap();
}
let mut ppr = PacketParser::from_bytes(&o).unwrap();
@@ -3261,7 +3266,7 @@ mod test {
signature::SignatureBuilder::new(SignatureType::Text)
).detached().build()?;
message.write_all(data)?;
- message.finalize()?;
+ message.finalize().now_or_never().unwrap()?;
}
struct Helper {};
diff --git a/openpgp/src/serialize/stream/padding.rs b/openpgp/src/serialize/stream/padding.rs
index b1750b07..de4277e1 100644
--- a/openpgp/src/serialize/stream/padding.rs
+++ b/openpgp/src/serialize/stream/padding.rs
@@ -218,9 +218,10 @@ impl<'a, P: Fn(u64) -> u64 + 'a> io::Write for Padder<'a, P> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, P: Fn(u64) -> u64 + 'a> writer::Stackable<'a, Cookie> for Padder<'a, P>
{
- fn into_inner(self: Box<Self>)
+ async fn into_inner(self: Box<Self>)
-> Result<Option<writer::BoxStack<'a, Cookie>>> {
// Make a note of the amount of data written to this filter.
let uncompressed_size = self.position();
@@ -228,7 +229,7 @@ impl<'a, P: Fn(u64) -> u64 + 'a> writer::Stackable<'a, Cookie> for Padder<'a, P>
// Pop-off us and the compression filter, leaving only our
// partial body encoder on the stack. This finalizes the
// compression.
- let mut pb_writer = Box::new(self.inner).into_inner()?.unwrap();
+ let mut pb_writer = Box::new(self.inner).into_inner().await?.unwrap();
// Compressed size is what we've actually written out, modulo
// partial body encoding.
@@ -263,7 +264,7 @@ impl<'a, P: Fn(u64) -> u64 + 'a> writer::Stackable<'a, Cookie> for Padder<'a, P>
amount -= n as u64;
}
- pb_writer.into_inner()
+ pb_writer.into_inner().await
}
fn pop(&mut self) -> Result<Option<writer::BoxStack<'a, Cookie>>> {
unreachable!("Only implemented by Signer")
@@ -335,6 +336,7 @@ fn log2(x: u64) -> usize {
#[cfg(test)]
mod test {
+ use futures::FutureExt;
use super::*;
#[test]
@@ -408,7 +410,7 @@ mod test {
let padder = Padder::new(message, padme).unwrap();
let mut w = LiteralWriter::new(padder).build().unwrap();
w.write_all(&msg).unwrap();
- w.finalize().unwrap();
+ w.finalize().now_or_never().unwrap().unwrap();
}
let m = crate::Message::from_bytes(&padded).unwrap();
@@ -431,7 +433,7 @@ mod test {
let padder = Padder::new(message, padme).unwrap();
let mut w = LiteralWriter::new(padder).build().unwrap();
w.write_all(MSG).unwrap();
- w.finalize().unwrap();
+ w.finalize().now_or_never().unwrap().unwrap();
}
assert!(padded.windows(MSG.len()).any(|ch| ch == MSG),
diff --git a/openpgp/src/serialize/stream/partial_body.rs b/openpgp/src/serialize/stream/partial_body.rs
index e293c427..2e25949c 100644
--- a/openpgp/src/serialize/stream/partial_body.rs
+++ b/openpgp/src/serialize/stream/partial_body.rs
@@ -194,8 +194,9 @@ impl<'a, C: 'a> fmt::Debug for PartialBodyFilter<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> writer::Stackable<'a, C> for PartialBodyFilter<'a, C> {
- fn into_inner(mut self: Box<Self>) -> Result<Option<writer::BoxStack<'a, C>>> {
+ async fn into_inner(mut self: Box<Self>) -> Result<Option<writer::BoxStack<'a, C>>> {
self.write_out(&b""[..], true)?;
Ok(self.inner.take())
}
@@ -236,6 +237,7 @@ impl<'a, C: 'a> writer::Stackable<'a, C> for PartialBodyFilter<'a, C> {
#[cfg(test)]
mod test {
+ use futures::FutureExt;
use std::io::Write;
use super::*;
use crate::serialize::stream::Message;
@@ -252,7 +254,7 @@ mod test {
.unwrap();
pb.write_all(b"0123").unwrap();
pb.write_all(b"4567").unwrap();
- pb.finalize().unwrap();
+ pb.finalize().now_or_never().unwrap().unwrap();
}
assert_eq!(&buf,
&[8, // no chunking
@@ -270,7 +272,7 @@ mod test {
/* max_chunk_size: */ 16)
.unwrap();
pb.write_all(b"01234567").unwrap();
- pb.finalize().unwrap();
+ pb.finalize().now_or_never().unwrap().unwrap();
}
assert_eq!(&buf,
&[0xe0 + 3, // first chunk
@@ -290,7 +292,7 @@ mod test {
/* max_chunk_size: */ 16)
.unwrap();
pb.write_all(b"012345670123456701234567").unwrap();
- pb.finalize().unwrap();
+ pb.finalize().now_or_never().unwrap().unwrap();
}
assert_eq!(&buf,
&[0xe0 + 4, // first chunk
diff --git a/openpgp/src/serialize/stream/writer/mod.rs b/openpgp/src/serialize/stream/writer/mod.rs
index 8aca6891..f71779bc 100644
--- a/openpgp/src/serialize/stream/writer/mod.rs
+++ b/openpgp/src/serialize/stream/writer/mod.rs
@@ -57,12 +57,14 @@ impl<'a> From<Message<'a>> for BoxStack<'a, Cookie> {
pub(crate) type BoxStack<'a, C> = Box<dyn Stackable<'a, C> + 'a>;
/// Makes a writer stackable and provides convenience functions.
+#[async_trait::async_trait(?Send)]
pub(crate) trait Stackable<'a, C> : io::Write + fmt::Debug {
/// Recovers the inner stackable.
///
/// This can fail if the current `Stackable` has buffered data
/// that hasn't been written to the underlying `Stackable`.
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>>;
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>>
+ where 'a: 'async_trait;
/// Pops the stackable from the stack, detaching it.
///
@@ -115,9 +117,12 @@ pub(crate) trait Stackable<'a, C> : io::Write + fmt::Debug {
}
/// Make a `Box<Stackable>` look like a Stackable.
+#[async_trait::async_trait(?Send)]
impl <'a, C> Stackable<'a, C> for BoxStack<'a, C> {
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
- (*self).into_inner()
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>>
+ where 'a: 'async_trait
+ {
+ (*self).into_inner().await
}
/// Recovers the inner stackable.
fn pop(&mut self) -> Result<Option<BoxStack<'a, C>>> {
@@ -222,9 +227,10 @@ impl<'a, C> io::Write for Identity<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C> Stackable<'a, C> for Identity<'a, C> {
/// Recovers the inner stackable.
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
Ok(self.inner)
}
/// Recovers the inner stackable.
@@ -308,9 +314,12 @@ impl<W: io::Write, C> io::Write for Generic<W, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, W: io::Write, C> Stackable<'a, C> for Generic<W, C> {
/// Recovers the inner stackable.
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>>
+ where 'a: 'async_trait
+ {
Ok(None)
}
/// Recovers the inner stackable.
@@ -389,8 +398,9 @@ impl<'a, C: 'a> io::Write for Armorer<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for Armorer<'a, C> {
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finalize()?;
Ok(Some(inner))
}
@@ -458,8 +468,9 @@ impl<'a, C: 'a> io::Write for Encryptor<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for Encryptor<'a, C> {
- fn into_inner(mut self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(mut self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finish()?;
Ok(Some(inner))
}
@@ -531,8 +542,9 @@ impl<'a, C: 'a> io::Write for AEADEncryptor<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for AEADEncryptor<'a, C> {
- fn into_inner(mut self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(mut self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finish()?;
Ok(Some(inner))
}
diff --git a/openpgp/src/serialize/stream/writer/writer_bzip2.rs b/openpgp/src/serialize/stream/writer/writer_bzip2.rs
index 72bc3f1d..73eb027a 100644
--- a/openpgp/src/serialize/stream/writer/writer_bzip2.rs
+++ b/openpgp/src/serialize/stream/writer/writer_bzip2.rs
@@ -43,8 +43,9 @@ impl<'a, C: 'a> io::Write for BZ<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for BZ<'a, C> {
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finish()?;
Ok(Some(inner))
}
diff --git a/openpgp/src/serialize/stream/writer/writer_deflate.rs b/openpgp/src/serialize/stream/writer/writer_deflate.rs
index db773e0b..264fa0c8 100644
--- a/openpgp/src/serialize/stream/writer/writer_deflate.rs
+++ b/openpgp/src/serialize/stream/writer/writer_deflate.rs
@@ -43,8 +43,9 @@ impl<'a, C: 'a> io::Write for ZIP<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for ZIP<'a, C> {
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finish()?;
Ok(Some(inner))
}
@@ -111,8 +112,9 @@ impl<'a, C: 'a> io::Write for ZLIB<'a, C> {
}
}
+#[async_trait::async_trait(?Send)]
impl<'a, C: 'a> Stackable<'a, C> for ZLIB<'a, C> {
- fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
+ async fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
let inner = self.inner.inner.finish()?;
Ok(Some(inner))
}