use std::io::BufReader;
use pgp::{
composed::{Deserializable, Message, SignedSecretKey},
packet::{NewPacketHeader, PacketHeader, Signature},
types::{EncryptionKey, KeyDetails, PacketLength, Password},
};
#[test]
fn malformed_signature_146() {
let data: &[u8] = &[5, 2, 2, 11, 0, 2, 0, 0];
let res = Signature::try_from_reader(
PacketHeader::New {
header: NewPacketHeader::new(),
length: PacketLength::Fixed(99),
},
BufReader::new(data),
);
assert!(res.is_ok());
}
#[test]
fn rpg_022_message_from_armor_single_panic2() {
let bad_input: &[u8] = b"-----BEGIN PGP SIGNATURE-----\n00LL";
let _ = Message::from_armor(bad_input);
}
#[test]
fn rpg_019_message_decrypt_with_password_panic1() {
let bad_input: &[u8] = &[
140, 159, 4, 1, 0, 0, 0, 167, 167, 167, 167, 167, 167, 167, 167, 0, 0, 0, 0, 0, 0, 0, 145,
68, 32, 70, 73, 76, 69, 208, 0, 0, 0, 0, 227, 167, 167, 76, 69, 210, 69, 208, 210, 167,
167, 167, 227, 167, 167, 76, 69, 210, 167, 167, 167, 167, 167, 167, 227, 167, 167, 76, 69,
210, 69, 208, 210, 167, 167, 167, 227, 167, 167, 76, 69, 227, 167, 167, 76, 69, 1, 0, 0, 0,
0, 0, 4, 184, 167, 167, 167, 227, 167, 167, 76, 69, 167, 167, 167, 167, 167, 167, 68, 32,
70, 73, 76, 69, 208, 210, 167, 167, 167, 227, 167, 167, 76, 69, 210, 69, 208, 210, 167,
167, 167, 227, 167, 167, 76, 69, 227, 167, 167, 69, 73, 76, 69, 208, 210, 167, 167, 167,
227, 167, 167, 76, 69, 210, 69, 208, 210, 167, 167, 167, 227, 167, 167, 76, 69, 227, 167,
167,
];
let message = Message::from_bytes(bad_input).unwrap();
let _ = message.decrypt_with_password(&"password does not matter".into());
}
#[test]
fn rpg_019_message_decrypt_with_password_panic2() {
let bad_input: &[u8] = &[
0xc3, 0x20, 0x04, 0x01, 0x01, 0x02, 0x32, 0xf6, 0xe3, 0xff, 0xff, 0xac, 0xa7, 0xa7, 0xa7,
0xff, 0xff, 0xa7, 0x26, 0xaf, 0x20, 0x4b, 0xaf, 0xa7, 0xa7, 0xa7, 0xa7, 0xd1, 0x22, 0xa7,
0xa7, 0xa7, 0x00, 0xa7, 0xa7, 0xd1, 0x22, 0xff, 0xff, 0xff, 0xa7, 0x26, 0xaf, 0x20, 0x4b,
0xaf,
];
let message = Message::from_bytes(bad_input).unwrap();
let _ = message.decrypt_with_password(&"bogus_password".into());
}
#[test]
fn rpg_016_message_parser_panic2() {
let bad_input: &[u8] = &[0xb7];
let _ = Message::from_bytes(bad_input);
}
#[test]
fn rpg_015_cleartext_signed_message_from_armor_panic1() {
let bad_input: &[u8] = &[
45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 71, 80, 32, 83, 73, 71, 78, 69, 68, 32, 77,
69, 83, 83, 65, 71, 69, 45, 45, 45, 45, 45, 10, 10, 22, 10, 45, 45, 45, 45, 45, 66, 69, 71,
73, 78, 32, 80, 71, 80, 32, 83, 73, 71, 78, 65, 84, 85, 82, 69, 45, 45, 45, 45, 45, 10, 72,
];
let _ = pgp::composed::CleartextSignedMessage::from_armor(bad_input);
}
#[test]
fn rgp_015_cleartext_signed_message_from_string_panic1() {
let bad_input = "-----BEGIN PGP SIGNED MESSAGE-----\n\n-\n-----BEGIN PGP SIGNATURE-----\n-";
let _ = pgp::composed::CleartextSignedMessage::from_string(bad_input);
}
#[test]
fn rpg_015_cleartext_signed_message_from_string_panic2() {
let data = "-----BEGIN PGP SIGNED MESSAGE-----\n\r\nqq\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n-----BEGIN PGP SIGNATURE-----\n\n\n\n\n\n\n\n\n
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq-qqqqqqqqqqqqqqqq\0----BE\u{7}IN-D*'S-- \u{1}{\0\0\u{1}\0\0\0\0\0\0\0\0\0\0\0\0\0\0-----BEGIN PGP PRIVATE KEY BLOCK
-----\n-----CEGIN PGP-----BEGIN OPENSSH PRIVATE qqqKEY----- M-----BEGIN OPENSSH PRIVATE KEY---- KEY----- M[ESSA-----BEGIN PGP SIGNED MESSAGE-----GE\t-
--\0\0\0\0\0\0>ATE KEY B- M[ESSAGE\t0--\0\0\0\0\0\0>ATE KEY BLOCK----%\n-----\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0>>>>>>>>\0\0\0\0\"DE-----END PGP PRIVATE KEY BLOCK---------BEYGIN PGP M[ESSAGE\t---\0\0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>#>>>>>>>>>>>>>>>>>>>>>>>>>>>>qq>>>>>>>>>>>>>>>>>>>\t>>>>>>>PGP M[ESSAGE\t---\0\0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>[ESSAGE\tw:::::::";
let _ = pgp::composed::CleartextSignedMessage::from_string(data);
}
#[test]
fn rpg_015_cleartext_signed_message_from_armor_panic2() {
let data = vec![
45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 71, 80, 32, 83, 73, 71, 78, 69, 68, 32, 77,
69, 83, 83, 65, 71, 69, 45, 45, 45, 45, 45, 10, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 45,
89, 45, 45, 45, 45, 45, 10, 10, 10, 10, 10, 10, 10, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71,
73, 78, 32, 80, 71, 80, 32, 83, 73, 71, 78, 65, 84, 85, 82, 69, 45, 45, 45, 45, 45, 10, 10,
10, 10, 10, 26, 45, 45, 45, 45, 45, 45, 45, 10, 10, 86, 10, 10, 10, 10, 10, 10, 10, 10, 10,
45, 45, 69, 78, 68, 32, 68, 83, 65, 32, 80, 82, 73, 86, 65, 84, 69, 32, 75, 69, 89, 45, 58,
26, 10, 10, 10, 10, 10, 10, 10, 86, 10, 10, 0, 0, 0, 0, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 45, 0, 45, 45, 45, 45, 10, 10, 10, 10, 10, 45, 45, 45, 45, 45, 66, 69, 71, 73,
78, 32, 69, 67, 32, 80, 45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 71, 80, 32, 83, 73, 71, 78, 69, 68, 32, 77,
69, 83, 83, 65, 71, 69, 45, 0, 13, 10, 247, 255, 15, 0, 45, 45, 45, 45, 45, 45, 45, 69, 78,
68, 32, 69, 67, 32, 80, 82, 71, 80, 32, 80, 82, 73, 86, 65, 84, 69, 32, 75, 189, 10, 73,
86, 65, 84, 69, 45, 45, 45, 66, 187, 175, 73, 78, 32, 80, 71, 80, 32, 77, 69, 83, 83, 65,
71, 69, 45, 45, 83, 83, 65, 71, 69, 45, 45, 45, 45, 45, 133, 133, 133, 133, 10, 10, 10, 10,
10, 133, 133, 133, 64, 10, 86, 10, 10, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 7, 121, 0, 0, 0,
0, 0, 45, 0, 0, 0, 45, 45, 0, 13, 10, 45, 45, 45, 45, 10, 13, 10, 10, 45, 45, 45, 10, 166,
];
let _ = pgp::composed::CleartextSignedMessage::from_armor(&data[..]);
}
#[test]
fn rpg_007_message_parser_panic1() {
let bad_input: &[u8] = &[0xff, 0x1];
let _ = Message::from_bytes(bad_input);
}
#[test]
fn rpg_007_message_from_armor_single_panic1() {
let bad_input: &[u8] = &[
45, 45, 45, 45, 45, 66, 69, 71, 73, 78, 32, 80, 71, 80, 32, 77, 69, 83, 83, 65, 71, 69, 45,
45, 45, 45, 45, 10, 54, 84, 54, 53, 45, 45, 45, 45, 45, 69, 78, 68, 32, 80, 71, 80, 32, 77,
69, 83, 83, 65, 71, 69, 45, 45, 45, 45, 45,
];
let _ = Message::from_armor(bad_input);
}
#[test]
fn rpg_021_signed_secret_key_encrypt_panic1() {
let bad_input: &[u8] = &[
197, 159, 4, 159, 1, 0, 20, 2, 0, 61, 0, 0, 0, 64, 0, 201, 0, 197, 0, 1, 251, 213, 0, 201,
0, 250, 196, 0, 197, 0, 197, 0, 197, 0, 201, 0, 197, 0, 197, 0, 201, 255, 255, 255, 255,
255, 255, 255, 5, 205, 205, 205, 205, 43, 129, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 1,
161, 4, 0, 242, 143, 4, 4, 135, 6, 0, 0, 0, 0, 6, 0, 0, 0, 0, 242, 143, 4, 4, 0, 0, 0, 0,
0, 0, 0, 2, 0, 0, 0, 1, 1, 1, 161, 4, 0, 143, 4, 4, 135, 6, 0, 0, 0, 0, 4, 0, 242, 143, 4,
4, 135, 6, 0, 0, 0, 0, 0, 0, 0, 0, 242, 143, 4, 4, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1,
1, 143, 4, 4, 135, 6, 0, 0, 0, 0,
];
let dummy_plaintext = vec![0u8; 128];
let mut rng = rand::thread_rng();
let key = pgp::composed::SignedSecretKey::from_bytes(bad_input).unwrap();
let _ciphertext = {
key.primary_key.public_key().encrypt(
&mut rng,
dummy_plaintext.as_slice(),
pgp::types::EskType::V6,
)
};
}
#[test]
fn rpg_021_signed_secret_key_encrypt_panic2() {
let bad_input: &[u8] = &[
0x97, 0x04, 0x00, 0x1a, 0x1a, 0x1a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x29,
];
let key = pgp::composed::SignedSecretKey::from_bytes(bad_input);
assert!(key.is_err());
}
#[test]
fn rpg_020_signed_secret_key_create_signature_panic1() {
let bad_input: &[u8] = &[
151, 3, 255, 251, 255, 63, 39, 254, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
];
let res = pgp::composed::SignedSecretKey::from_bytes(bad_input);
assert!(res.is_err());
}
#[test]
fn rpg_020_signed_secret_key_create_signature_panic2() {
let bad_input: &[u8] = &[
0x97, 0x04, 0x00, 0x00, 0x08, 0x29, 0xc1, 0xfd, 0xff, 0x03, 0x03, 0x02, 0x08, 0x00, 0xf8,
0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xf8, 0xf8, 0xff, 0x00, 0xff,
0x00, 0xff, 0x00,
];
let res = pgp::composed::SignedSecretKey::from_bytes(bad_input);
assert!(res.is_err());
}
#[test]
fn rpg_020_signed_secret_key_create_signature_oom_crash1() {
let bad_input: &[u8] = &[
0x97, 0x04, 0x00, 0x00, 0x08, 0x29, 0xc1, 0xfd, 0xff, 0x9f, 0x04, 0x8f, 0xe4, 0xff, 0xff,
0xff, 0xff, 0x80, 0x8f, 0x8f, 0x8f, 0x00, 0x01, 0x00, 0x00, 0x00, 0xaf, 0xf8, 0x1b, 0x1b,
];
let res = pgp::composed::SignedSecretKey::from_bytes(bad_input);
assert!(res.is_err());
}
#[test]
fn rpg_010_standalone_signature_subtract_with_overflow1() {
let bad_input: &[u8] = &[209, 3, 0, 252, 45];
let _ = pgp::composed::DetachedSignature::from_bytes(bad_input);
}
#[test]
fn rpg_009_message_from_bytes_subtract_with_overflow1() {
let bad_input: &[u8] = &[187, 6, 227, 0, 255, 255, 255, 255, 255, 255, 255];
let _ = Message::from_bytes(bad_input);
}
#[test]
fn rpg_009_message_from_bytes_subtract_with_overflow2() {
let bad_input: &[u8] = &[139, 4, 16, 0, 0, 0, 2, 0, 0];
let _ = Message::from_bytes(bad_input);
}
#[test]
fn rpg_009_message_from_bytes_subtract_with_overflow3() {
let bad_input: &[u8] = &[151, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 113, 113];
let _ = Message::from_bytes(bad_input);
}
#[test]
fn oom_signature_1() {
let bad_input = [
155, 6, 3, 72, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
];
let res = pgp::composed::DetachedSignature::from_bytes(&bad_input[..]);
assert!(res.is_err());
}
#[test]
fn signed_public_key_legacy_key_id_crash() {
let bad_input = [155, 2, 23, 4, 1, 165, 0, 23, 1, 0, 1, 165, 0, 1, 23];
let res = pgp::composed::SignedPublicKey::from_bytes(&bad_input[..]);
res.unwrap().legacy_key_id();
}
#[test]
fn pr662_crash1() {
let bad_input: &[u8] = &[
193, 192, 204, 3, 124, 47, 170, 77, 249, 60, 55, 178, 1, 12, 0, 208, 100, 161, 141, 75,
159, 113, 34, 116, 60, 162, 234, 101, 121, 180, 166, 145, 180, 141, 129, 118, 249, 252,
233, 3, 138, 5, 161, 253, 89, 240, 169, 110, 130, 20, 110, 161, 161, 254, 101, 101, 133,
165, 98, 71, 131, 44, 34, 162, 168, 127, 62, 14, 212, 125, 240, 165, 8, 244, 106, 145, 11,
255, 196, 139, 135, 34, 237, 143, 198, 208, 222, 56, 228, 205, 41, 180, 93, 11, 95, 220,
135, 164, 116, 222, 95, 162, 40, 221, 237, 12, 33, 94, 81, 16, 84, 139, 204, 147, 143, 112,
147, 75, 14, 29, 195, 229, 200, 219, 240, 54, 95, 129, 160, 230, 122, 180, 139, 232, 73,
136, 28, 162, 45, 208, 110, 95, 125, 143, 220, 27, 187, 107, 21, 9, 160, 190, 110, 9, 250,
196, 81, 148, 115, 201, 46, 41, 54, 51, 141, 24, 103, 152, 188, 177, 239, 30, 161, 83, 87,
204, 249, 153, 65, 125, 138, 38, 150, 124, 48, 226, 0, 39, 95, 89, 59, 16, 89, 110, 87, 34,
110, 110, 224, 127, 95, 215, 142, 96, 157, 112, 226, 61, 40, 46, 250, 4, 72, 13, 89, 106,
86, 241, 129, 13, 32, 160, 80, 110, 202, 30, 52, 208, 119, 151, 174, 151, 218, 206, 182,
130, 73, 169, 80, 228, 126, 68, 200, 70, 230, 37, 143, 102, 5, 95, 134, 93, 246, 108, 116,
29, 112, 113, 212, 99, 8, 172, 172, 24, 72, 25, 200, 242, 19, 8, 43, 229, 215, 238, 12,
119, 41, 191, 2, 153, 60, 251, 145, 17, 163, 156, 33, 0, 180, 126, 227, 221, 156, 69, 210,
182, 106, 113, 161, 43, 249, 224, 46, 198, 124, 114, 127, 145, 248, 135, 248, 76, 183, 159,
75, 112, 95, 158, 214, 113, 157, 53, 188, 173, 168, 21, 7, 21, 190, 138, 7, 224, 152, 70,
213, 24, 193, 6, 81, 228, 238, 122, 217, 172, 146, 100, 209, 222, 140, 45, 30, 133, 199,
67, 13, 46, 121, 85, 251, 189, 60, 220, 24, 178, 1, 126, 101, 61, 183, 109, 197, 30, 38,
48, 123, 142, 202, 99, 182, 207, 101, 106, 252, 173, 8, 233, 108, 32, 119, 163, 230, 7, 74,
118, 238, 167, 25, 242, 105, 210, 40, 1, 74, 92, 164, 57, 220, 200, 43, 232, 4, 30, 193,
225, 240, 5, 244, 112, 26, 194, 157, 76, 187, 216, 231, 67, 242, 16, 223, 91, 151, 160,
109, 31, 75, 249, 110, 219, 40, 53, 50, 130, 36, 23, 110,
];
let key_input = include_str!("draft-bre-openpgp-samples-00/bob.sec.asc");
let (decrypt_key, _headers) = SignedSecretKey::from_string(key_input).unwrap();
let message = Message::from_bytes(bad_input).unwrap();
let res = message
.decrypt(&Password::empty(), &decrypt_key)
.err()
.unwrap();
assert_eq!(
res.to_string(),
"IO error: Modification Detection Code error"
);
}
#[test]
fn signed_public_key_details_write_len_expect() {
use pgp::ser::Serialize;
let bad_input =
std::fs::read_to_string("tests/unit-tests/signed_public_key_details_write_len_expect.asc")
.unwrap();
let res = pgp::composed::SignedPublicKey::from_armor_single(bad_input.as_bytes());
match res {
Ok((pubkey, _)) => {
let _ = pubkey.details.write_len();
unreachable!("This should not be reached");
}
Err(e) => {
assert!(e.to_string().contains("Inconsistent subpacket length"));
}
}
}
#[test]
fn signed_public_key_subkey_write_len_expect() {
use pgp::ser::Serialize;
let bad_input =
std::fs::read_to_string("tests/unit-tests/signed_public_key_subkey_write_len_expect.asc")
.unwrap();
let res = pgp::composed::SignedPublicKey::from_armor_single(bad_input.as_bytes());
match res {
Ok((pubkey, _)) => {
for signedsubkey in pubkey.public_subkeys {
let _ = signedsubkey.write_len();
}
unreachable!("This should not be reached");
}
Err(e) => {
assert!(e.to_string().contains("Inconsistent subpacket length"));
}
}
}