|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +#[cfg(feature = "fuzzing")] |
| 4 | +use libfuzzer_sys::fuzz_target; |
| 5 | +use ohttp::hpke::{Aead, Kdf, Kem}; |
| 6 | +use ohttp::{KeyId, SymmetricSuite}; |
| 7 | +use payjoin::{HpkeKeyPair, HpkePublicKey, OhttpKeys}; |
| 8 | +use url::Url; |
| 9 | + |
| 10 | +fn do_test(data: &[u8]) { |
| 11 | + let data_str = String::from_utf8_lossy(data); |
| 12 | + let mut url = Url::parse("https://example.com").unwrap(); |
| 13 | + |
| 14 | + let serialized = "OH1QYPM5JXYNS754Y4R45QWE336QFX6ZR8DQGVQCULVZTV20TFVEYDMFQC"; |
| 15 | + const KEY_ID: KeyId = 1; |
| 16 | + const KEM: Kem = Kem::K256Sha256; |
| 17 | + const SYMMETRIC: &[SymmetricSuite] = |
| 18 | + &[ohttp::SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)]; |
| 19 | + let ohttp_keys = OhttpKeys(ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC)).unwrap()); |
| 20 | + //let ohttp_keys = OhttpKeys::from_str(serialized).unwrap(); |
| 21 | + url.set_ohttp(ohttp_keys.clone()); |
| 22 | + assert_eq!(url.fragment(), Some(serialized)); |
| 23 | + |
| 24 | + let receiver_pubkey = HpkeKeyPair::gen_keypair().1; |
| 25 | + url.set_receiver_pubkey(receiver_pubkey); |
| 26 | + |
| 27 | + let exp_time = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1720547781); |
| 28 | + url.set_exp(exp_time); |
| 29 | + |
| 30 | + let compressed_receiver_pubkey = match url.receiver_pubkey() { |
| 31 | + Ok(rpk) => rpk.to_compressed_bytes(), |
| 32 | + Err(_) => return, |
| 33 | + }; |
| 34 | + let receiver_pubkey_roundtrip = |
| 35 | + match HpkePublicKey::from_compressed_bytes(&compressed_receiver_pubkey) { |
| 36 | + Ok(rpk) => rpk.0, |
| 37 | + Err(_) => return, |
| 38 | + }; |
| 39 | + |
| 40 | + assert_eq!(url.ohttp().unwrap(), ohttp_keys); |
| 41 | + assert_eq!(url.receiver_pubkey().unwrap().0, receiver_pubkey_roundtrip); |
| 42 | + assert_eq!(url.exp().unwrap(), exp_time); |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(feature = "fuzzing")] |
| 46 | +fuzz_target!(|data| { |
| 47 | + do_test(data); |
| 48 | +}); |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { |
| 53 | + let mut b = 0; |
| 54 | + for (idx, c) in hex.as_bytes().iter().enumerate() { |
| 55 | + b <<= 4; |
| 56 | + match *c { |
| 57 | + b'A'..=b'F' => b |= c - b'A' + 10, |
| 58 | + b'a'..=b'f' => b |= c - b'a' + 10, |
| 59 | + b'0'..=b'9' => b |= c - b'0', |
| 60 | + _ => panic!("Bad hex"), |
| 61 | + } |
| 62 | + if (idx & 1) == 1 { |
| 63 | + out.push(b); |
| 64 | + b = 0; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn duplicate_crash() { |
| 71 | + let mut a = Vec::new(); |
| 72 | + extend_vec_from_hex("00000000", &mut a); |
| 73 | + super::do_test(&a); |
| 74 | + } |
| 75 | +} |
0 commit comments