Debian Codegraph @

src/commands/packet/armor.rs

raw
1 use std::io;
2
3 use buffered_reader::BufferedReader;
4 use buffered_reader::Dup;
5 use buffered_reader::Limitor;
6
7 use sequoia_openpgp as openpgp;
8 use openpgp::Packet;
9 use openpgp::armor;
10 use openpgp::parse::Cookie;
11 use openpgp::parse::Parse;
12 use openpgp::parse::PacketParser;
13 use openpgp::parse::PacketParserResult;
14
15 use crate::Sq;
16 use crate::Result;
17 use crate::cli;
18
19 /// How much data to look at when detecting armor kinds.
20 const ARMOR_DETECTION_LIMIT: u64 = 1 << 24;
21
22 /// Peeks at the first packet to guess the type.
23 ///
24 /// Returns the given reader unchanged. If the detection fails,
25 /// armor::Kind::File is returned as safe default.
26 fn detect_armor_kind<'a, B, C>(input: B)
27 -> armor::Kind
28 where B: BufferedReader<C>,
29 C: Default + std::fmt::Debug + Sync + Send,
30 {
31 let dup = Dup::with_cookie(input, C::default());
32 let limitor = Limitor::with_cookie(
33 dup, ARMOR_DETECTION_LIMIT, C::default());
34 let kind = match PacketParser::from_reader(limitor) {
35 Ok(PacketParserResult::Some(pp)) => match pp.next() {
36 Ok((Packet::Signature(_), _)) => armor::Kind::Signature,
37 Ok((Packet::SecretKey(_), _)) => armor::Kind::SecretKey,
38 Ok((Packet::PublicKey(_), _)) => armor::Kind::PublicKey,
39 Ok((Packet::PKESK(_), _)) => armor::Kind::Message,
40 Ok((Packet::SKESK(_), _)) => armor::Kind::Message,
41 _ => armor::Kind::File,
42 },
43 _ => armor::Kind::File,
44 };
45 kind
46 }
47
48 pub fn dispatch(sq: Sq, command: cli::packet::armor::Command)
49 -> Result<()>
50 {
51 tracer!(TRACE, "armor::dispatch");
52
53 let mut input = command.input.open("the data to encode")?;
54 let mut want_kind: Option<armor::Kind>
55 = command.kind.into();
56
57 // Peek at the data. If it looks like it is armored
58 // data, avoid armoring it again.
59 let dup = Dup::with_cookie(&mut input, Cookie::default());
60 let mut limitor = Limitor::with_cookie(
61 dup, ARMOR_DETECTION_LIMIT, Cookie::default());
62 let (already_armored, have_kind) = {
63 let mut reader =
64 armor::Reader::from_buffered_reader(
65 &mut limitor,
66 armor::ReaderMode::Tolerant(None))?;
67 (reader.data(8).is_ok(), reader.kind())
68 };
69
70 if already_armored
71 && (want_kind.is_none() || want_kind == have_kind)
72 {
73 // It is already armored and has the correct kind.
74 let mut output = command.output.create_safe(&sq)?;
75 io::copy(&mut input, &mut output)?;
76 return Ok(());
77 }
78
79 if want_kind.is_none() {
80 let kind = detect_armor_kind(&mut input);
81 want_kind = Some(kind);
82 }
83
84 // At this point, want_kind is determined.
85 let want_kind = want_kind.expect("given or detected");
86
87 let mut output =
88 command.output.create_pgp_safe(&sq, false, want_kind)?;
89
90 if already_armored {
91 // Dearmor and copy to change the type.
92 let mut reader =
93 armor::Reader::from_buffered_reader(
94 input,
95 armor::ReaderMode::Tolerant(None))?;
96 io::copy(&mut reader, &mut output)?;
97 } else {
98 io::copy(&mut input, &mut output)?;
99 }
100 output.finalize()?;
101
102 Ok(())
103 }