Debian Codegraph @

src/commands/sign/merge_signatures.rs

raw
1 use anyhow::Context as _;
2 use std::io;
3
4 extern crate sequoia_openpgp as openpgp;
5 use openpgp::packet::Literal;
6 use openpgp::packet::Tag;
7 use openpgp::parse::{PacketParser, PacketParserResult, Parse};
8 use openpgp::serialize::stream::{LiteralWriter, Message};
9 use openpgp::serialize::Serialize;
10 use openpgp::{Packet, Result};
11
12 pub fn merge_signatures(
13 input1: &mut (dyn io::Read + Send + Sync),
14 input2: &mut (dyn io::Read + Send + Sync),
15 mut sink: Message,
16 ) -> Result<()> {
17 let parser1 =
18 PacketParser::from_reader(input1).context("Failed to build parser")?;
19 let parser2 =
20 PacketParser::from_reader(input2).context("Failed to build parser")?;
21
22 let (ops1, post_ops_parser1) = read_while_by_tag(parser1, Tag::OnePassSig)?;
23 let (ops2, post_ops_parser2) = read_while_by_tag(parser2, Tag::OnePassSig)?;
24
25 let ops1 = ops1
26 .into_iter()
27 .map(ops_with_last_false)
28 .collect::<Result<Vec<_>>>()?;
29
30 write_packets(ops1, &mut sink)?;
31 write_packets(ops2, &mut sink)?;
32
33 let (sink_new, post_literal_parser1, post_literal_parser2) =
34 compare_and_write_literal(sink, post_ops_parser1, post_ops_parser2)?;
35 sink = sink_new;
36
37 let (sigs2, _) = read_while_by_tag(post_literal_parser2, Tag::Signature)?;
38 let (sigs1, _) = read_while_by_tag(post_literal_parser1, Tag::Signature)?;
39 write_packets(sigs2, &mut sink)?;
40 write_packets(sigs1, &mut sink)?;
41
42 sink.finalize().context("Failed to write data")?;
43 Ok(())
44 }
45
46 fn ops_with_last_false(p: Packet) -> Result<Packet> {
47 if let Packet::OnePassSig(mut ops) = p {
48 ops.set_last(false);
49 Ok(Packet::OnePassSig(ops))
50 } else {
51 Err(anyhow::anyhow!("Not a OnePassSig packet"))
52 }
53 }
54
55 fn write_packets(packets: Vec<Packet>, mut sink: &mut Message) -> Result<()> {
56 for packet in packets {
57 packet.serialize(&mut sink)?;
58 }
59 Ok(())
60 }
61
62 fn compare_and_write_literal<'a, 'b, 'c>(
63 sink: Message<'a>,
64 ppr1: PacketParserResult<'b>,
65 ppr2: PacketParserResult<'c>,
66 ) -> Result<(Message<'a>, PacketParserResult<'b>, PacketParserResult<'c>)> {
67 // We want to compare the bodies of the literal packets, by comparing their digests.
68 // Digests are only known after reading the packets, so:
69 // First, move both parsers past the literal packet, copy out the body of one of them.
70 // Second, compare the packets which now include the correct hashes,
71 // normalize to ignore metadata.
72 let (mut lp1, ppr1) = read_while_by_tag(ppr1, Tag::Literal)?;
73 let lp1 = lp1.remove(0);
74
75 let (sink, lp2, ppr2) = write_literal_(sink, ppr2)?;
76
77 let lp1 = normalize_literal(lp1)?;
78 let lp2 = normalize_literal(lp2)?;
79
80 if lp1 == lp2 {
81 Ok((sink, ppr1, ppr2))
82 } else {
83 Err(anyhow::anyhow!("Literal Packets differ, aborting!"))
84 }
85 }
86
87 // Clear date and filename.
88 fn normalize_literal(p: Packet) -> Result<Literal> {
89 if let Packet::Literal(mut l) = p {
90 l.set_date(None)?;
91 l.set_filename(&[])?;
92 Ok(l)
93 } else {
94 Err(anyhow::anyhow!("Not a literal packet"))
95 }
96 }
97
98 fn write_literal_<'a, 'b>(
99 mut sink: Message<'a>,
100 ppr: PacketParserResult<'b>,
101 ) -> Result<(Message<'a>, Packet, PacketParserResult<'b>)> {
102 if let PacketParserResult::Some(mut pp) = ppr {
103 // Assemble a new Literal packet.
104 // Cannot use packet.serialize because that does not include the body.
105 if let Packet::Literal(l) = pp.packet.clone() {
106 // Create a literal writer to wrap the data in a literal
107 // message packet.
108 let mut literal = LiteralWriter::new(sink)
109 .format(l.format())
110 .build()
111 .context("Failed to create literal writer")?;
112 // Do not add any metadata as it is unprotected anyway.
113
114 // Just copy all the data.
115 io::copy(&mut pp, &mut literal).context("Failed to copy data")?;
116
117 // Pop the literal writer.
118 sink = literal
119 .finalize_one()
120 .context("Failed to write literal packet")?
121 .unwrap();
122 }
123
124 let (packet, ppr) = pp.recurse()?;
125 Ok((sink, packet, ppr))
126 } else {
127 Err(anyhow::anyhow!("Unexpected end of file"))
128 }
129 }
130
131 fn read_while_by_tag(
132 mut ppr: PacketParserResult,
133 tag: Tag,
134 ) -> Result<(Vec<Packet>, PacketParserResult)> {
135 let mut result = vec![];
136
137 while let PacketParserResult::Some(pp) = ppr {
138 pp.possible_message()?;
139
140 let next_tag_matches = pp.header().ctb().tag() == tag;
141 if !next_tag_matches {
142 return Ok((result, PacketParserResult::Some(pp)));
143 }
144
145 // Start parsing the next packet, recursing.
146 let (packet, next_ppr) = pp.recurse()?;
147 ppr = next_ppr;
148 result.push(packet);
149 }
150
151 if let PacketParserResult::EOF(eof) = &ppr {
152 eof.is_message()?;
153 }
154
155 Ok((result, ppr))
156 }