summaryrefslogtreecommitdiffstats
path: root/openpgp/examples/wrap-literal.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-08-14 15:46:20 +0200
committerJustus Winter <justus@sequoia-pgp.org>2018-08-14 17:26:26 +0200
commitb6c3b0cbea62e3f9e12fe61713a64a3d48cf7961 (patch)
tree513291af98a1bc1efc87bbc48ba243427f2c4489 /openpgp/examples/wrap-literal.rs
parent7ac4372f981304856e179262eb9c6e8903a7dafb (diff)
openpgp: Make armor parsing more robust.
- ASCII Armor, designed to protect OpenPGP data in transit, has been a source of problems if the armor structure is damaged. For example, copying data manually from one program to another might introduce or drop newlines. - This change introduces a heuristic that simply tries to detect the base64-encoded OpenPGP data. This way, if the framing is in any way damaged, or even stripped, we can still recover the message. - Fixes #40.
Diffstat (limited to 'openpgp/examples/wrap-literal.rs')
-rw-r--r--openpgp/examples/wrap-literal.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/openpgp/examples/wrap-literal.rs b/openpgp/examples/wrap-literal.rs
new file mode 100644
index 00000000..5d57d118
--- /dev/null
+++ b/openpgp/examples/wrap-literal.rs
@@ -0,0 +1,40 @@
+/// This program demonstrates how to wrap a stream into a literal data
+/// packet.
+///
+/// It is also used to generate test vectors for the armor subsystem.
+
+use std::env;
+use std::io;
+
+extern crate openpgp;
+use openpgp::armor;
+use openpgp::constants::DataFormat;
+use openpgp::serialize::stream::{wrap, LiteralWriter};
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args.len() != 1 {
+ panic!("A simple filter wrapping data into a literal data packet.\n\n\
+ Usage: {} <input >output\n", args[0]);
+ }
+
+ // Compose a writer stack corresponding to the output format and
+ // packet structure we want. First, we want the output to be
+ // ASCII armored.
+ let sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[][..])
+ .expect("Failed to create armored writer.");
+
+ // Then, create a literal writer to wrap the data in a literal
+ // message packet.
+ let mut literal = LiteralWriter::new(wrap(sink), DataFormat::Binary,
+ None, None)
+ .expect("Failed to create literal writer");
+
+ // Finally, just copy all the data.
+ io::copy(&mut io::stdin(), &mut literal)
+ .expect("Failed to sign data");
+
+ // Teardown the stack to ensure all the data is written.
+ literal.finalize()
+ .expect("Failed to write data");
+}