| 1 |
use std::fs::File; |
| 2 |
use std::io::Seek; |
| 3 |
use std::io::Write; |
| 4 |
use std::io; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::Result; |
| 8 |
|
| 9 |
use super::common::*; |
| 10 |
|
| 11 |
// Make sure verifying bad data fails, and removes the intermediate |
| 12 |
// file. |
| 13 |
#[test] |
| 14 |
fn sq_verify_bad() -> Result<()> { |
| 15 |
let sq = Sq::new(); |
| 16 |
|
| 17 |
// The signer. |
| 18 |
let (_cert, cert_file, _rev_file) = sq.key_generate(&[], &["alice"]); |
| 19 |
|
| 20 |
// Create a big file. When verifying, we buffer the first ~25 MB |
| 21 |
// of verified data so that we can at least withhold some data if |
| 22 |
// we can't verify the message. |
| 23 |
let data_file = sq.scratch_file("data"); |
| 24 |
let size = { |
| 25 |
let mut file = File::create(&data_file)?; |
| 26 |
let data = vec![42; 1024 * 1024]; |
| 27 |
let mut size = 0; |
| 28 |
for _ in 0..30 { |
| 29 |
file.write_all(&data).expect("can write"); |
| 30 |
size += data.len(); |
| 31 |
} |
| 32 |
|
| 33 |
size as u64 |
| 34 |
}; |
| 35 |
assert_eq!(std::fs::metadata(&data_file).expect("can stat").len(), |
| 36 |
size); |
| 37 |
|
| 38 |
// Sign the message. |
| 39 |
let sig_file = sq.scratch_file("sig"); |
| 40 |
sq.sign(&cert_file, None, &data_file, sig_file.as_path()); |
| 41 |
|
| 42 |
// Verify the signed message. |
| 43 |
let output = sq.scratch_file("output"); |
| 44 |
std::fs::write(&output, "xxx").expect("can write to scratch file"); |
| 45 |
sq.verify( |
| 46 |
&["--overwrite", |
| 47 |
"--signer-file", &cert_file.display().to_string()], |
| 48 |
Verify::Message, |
| 49 |
&sig_file, |
| 50 |
output.as_path()); |
| 51 |
assert!(output.exists()); |
| 52 |
assert_eq!(std::fs::metadata(&output).expect("can stat").len(), |
| 53 |
size); |
| 54 |
|
| 55 |
// Modify the data, and make sure verification now fails. |
| 56 |
let mut f = File::options().write(true).open(&sig_file)?; |
| 57 |
f.seek(io::SeekFrom::Start(size / 2)).expect("can seek"); |
| 58 |
f.write_all(b"xxx").expect("can write"); |
| 59 |
drop(f); |
| 60 |
|
| 61 |
let output2 = sq.scratch_file("output2"); |
| 62 |
std::fs::write(&output2, "xxx").expect("can write to scratch file"); |
| 63 |
assert!( |
| 64 |
sq.verify_maybe( |
| 65 |
&["--overwrite", |
| 66 |
"--signer-file", &cert_file.display().to_string()], |
| 67 |
Verify::Message, |
| 68 |
&sig_file, |
| 69 |
output2.as_path()) |
| 70 |
.is_err()); |
| 71 |
// Since we failed to verify the message, we should have deleted |
| 72 |
// the output file. |
| 73 |
assert!(! output2.exists()); |
| 74 |
|
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
|
| 78 |
// Ensure bad/missing keyring produce errors |
| 79 |
#[test] |
| 80 |
fn sq_verify_bad_keyring() -> Result<()> { |
| 81 |
let sq = Sq::new(); |
| 82 |
let msg = artifact("examples/document.pgp"); |
| 83 |
|
| 84 |
let error_doesnotexist = sq.verify_maybe( |
| 85 |
&["--keyring", "doesnotexist"], |
| 86 |
Verify::Message, |
| 87 |
&msg, |
| 88 |
None |
| 89 |
); |
| 90 |
|
| 91 |
assert!(format!("{error_doesnotexist:?}").contains("Open")); |
| 92 |
assert!(format!("{error_doesnotexist:?}").contains("doesnotexist")); |
| 93 |
|
| 94 |
// Just use the readme as an invalid keyring |
| 95 |
let error_invalid = sq.verify_maybe( |
| 96 |
&[ |
| 97 |
"--keyring", |
| 98 |
&artifact("examples/README.md").display().to_string(), |
| 99 |
], |
| 100 |
Verify::Message, |
| 101 |
&msg, |
| 102 |
None, |
| 103 |
); |
| 104 |
|
| 105 |
assert!(format!("{error_invalid:?}").contains("Parsing")); |
| 106 |
assert!(format!("{error_invalid:?}").contains("examples/README.md")); |
| 107 |
|
| 108 |
Ok(()) |
| 109 |
} |
| 110 |
|
| 111 |
// Make sure --policy-as-of works |
| 112 |
#[test] |
| 113 |
fn sq_verify_policy_as_of() -> Result<()> { |
| 114 |
let sq = Sq::at_str("2024-11-01"); |
| 115 |
|
| 116 |
let cert = artifact("keys/only-sha1-pub.pgp"); |
| 117 |
let msg = artifact("messages/signed-by-only-sha1.pgp"); |
| 118 |
|
| 119 |
assert!( |
| 120 |
sq.verify_maybe( |
| 121 |
&[ |
| 122 |
"--signer-file", &cert.display().to_string(), |
| 123 |
], |
| 124 |
Verify::Message, |
| 125 |
&msg, |
| 126 |
None) |
| 127 |
.is_err()); |
| 128 |
|
| 129 |
// Setting the reference time is not enough, because the message's |
| 130 |
// creation time would be in the future. |
| 131 |
assert!( |
| 132 |
sq.verify_maybe( |
| 133 |
&[ |
| 134 |
"--time", "2022-01-01", |
| 135 |
"--signer-file", &cert.display().to_string(), |
| 136 |
], |
| 137 |
Verify::Message, |
| 138 |
&msg, |
| 139 |
None) |
| 140 |
.is_err()); |
| 141 |
|
| 142 |
// But setting the policy time is. |
| 143 |
assert!( |
| 144 |
sq.verify_maybe( |
| 145 |
&[ |
| 146 |
"--policy-as-of", "2022-01-01", |
| 147 |
"--signer-file", &cert.display().to_string(), |
| 148 |
], |
| 149 |
Verify::Message, |
| 150 |
&msg, |
| 151 |
None) |
| 152 |
.is_ok()); |
| 153 |
|
| 154 |
// Make sure we can set both the reference time and the policy |
| 155 |
// time. |
| 156 |
assert!( |
| 157 |
sq.verify_maybe( |
| 158 |
&[ |
| 159 |
"--time", "2025-01-01", |
| 160 |
"--policy-as-of", "2022-01-01", |
| 161 |
"--signer-file", &cert.display().to_string(), |
| 162 |
], |
| 163 |
Verify::Message, |
| 164 |
&msg, |
| 165 |
None) |
| 166 |
.is_ok()); |
| 167 |
|
| 168 |
Ok(()) |
| 169 |
} |
| 170 |
|
| 171 |
// Make sure --policy-as-of works with relative time. |
| 172 |
#[test] |
| 173 |
fn sq_verify_policy_as_of_relative_time() -> Result<()> { |
| 174 |
let sq = Sq::at_str("2024-11-01"); |
| 175 |
|
| 176 |
// Creation time: 2020-11-04 18:36:04 UTC |
| 177 |
let cert = artifact("keys/only-sha1-pub.pgp"); |
| 178 |
let msg = artifact("messages/signed-by-only-sha1.pgp"); |
| 179 |
|
| 180 |
// Signature creation time: 2024-10-31 13:40:28 UTC |
| 181 |
assert!( |
| 182 |
sq.verify_maybe( |
| 183 |
&[ |
| 184 |
"--signer-file", &cert.display().to_string(), |
| 185 |
], |
| 186 |
Verify::Message, |
| 187 |
&msg, |
| 188 |
None) |
| 189 |
.is_err()); |
| 190 |
|
| 191 |
// Setting the reference time is not enough, because the message's |
| 192 |
// creation time would be in the future. |
| 193 |
assert!( |
| 194 |
sq.verify_maybe( |
| 195 |
&[ |
| 196 |
// => 2021-11-01 |
| 197 |
"--time", "-3y", |
| 198 |
"--signer-file", &cert.display().to_string(), |
| 199 |
], |
| 200 |
Verify::Message, |
| 201 |
&msg, |
| 202 |
None) |
| 203 |
.is_err()); |
| 204 |
|
| 205 |
// But setting the policy time is. |
| 206 |
assert!( |
| 207 |
sq.verify_maybe( |
| 208 |
&[ |
| 209 |
// => 2021-11-01 |
| 210 |
"--policy-as-of", "-3y", |
| 211 |
"--signer-file", &cert.display().to_string(), |
| 212 |
], |
| 213 |
Verify::Message, |
| 214 |
&msg, |
| 215 |
None) |
| 216 |
.is_ok()); |
| 217 |
|
| 218 |
// Make sure we can set both the reference time and the policy |
| 219 |
// time. |
| 220 |
assert!( |
| 221 |
sq.verify_maybe( |
| 222 |
&[ |
| 223 |
"--time", "2025-11-01", |
| 224 |
// => 2021-11-01 |
| 225 |
"--policy-as-of", "-4y", |
| 226 |
"--signer-file", &cert.display().to_string(), |
| 227 |
], |
| 228 |
Verify::Message, |
| 229 |
&msg, |
| 230 |
None) |
| 231 |
.is_ok()); |
| 232 |
|
| 233 |
Ok(()) |
| 234 |
} |
| 235 |
|
| 236 |
/// Make sure designated signers are respected. |
| 237 |
#[test] |
| 238 |
fn sq_verify_designated_signers() -> Result<()> { |
| 239 |
let sq = Sq::new(); |
| 240 |
|
| 241 |
// First, import Neal's cert and make sure the message verifies |
| 242 |
// ok. |
| 243 |
sq.cert_import(artifact("examples/juliet.pgp")); |
| 244 |
sq.pki_link_add(&["--all"], |
| 245 |
"7A58B15E3B9459483D9FFA8D40E299AC5F2B0872".parse()?, |
| 246 |
NO_USERIDS); |
| 247 |
assert!(sq.verify_maybe( |
| 248 |
&[], |
| 249 |
Verify::Message, |
| 250 |
artifact("examples/document.pgp"), None).is_ok()); |
| 251 |
|
| 252 |
// Now repeat, but require a signature from Bob, which doesn't |
| 253 |
// exist. |
| 254 |
assert!(sq.verify_maybe( |
| 255 |
&[&format!("--signer-file={}", artifact("examples/bob.pgp").display())], |
| 256 |
Verify::Message, |
| 257 |
artifact("examples/document.pgp"), None).is_err()); |
| 258 |
|
| 259 |
Ok(()) |
| 260 |
} |