Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use k256 AEAD for ohttp #227

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
506 changes: 339 additions & 167 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion payjoin-cli/example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ ohttp_relay="https://pj.bobspacebkk.com"

# (v2 only, optional) The HPKE keys which need to be fetched ahead of time from the pj_endpoint for the payjoin packets to be encrypted.
# These can now be fetched and no longer need to be configured.
ohttp_keys="AQAg3c9qovMZvPzLh8XHgD8q86WG7SmPQvPamCTvEoueKBsABAABAAM"
ohttp_keys="AQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM"
4 changes: 2 additions & 2 deletions payjoin-directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ danger-local-https = ["hyper-rustls", "rustls"]
[dependencies]
anyhow = "1.0.71"
bitcoin = { version = "0.32.2", features = ["base64"] }
bhttp = { version = "=0.5.1", features = ["http"] }
bhttp = { version = "0.5.1", features = ["http"], path = "../../ohttp/bhttp" }
futures = "0.3.17"
hyper = { version = "0.14", features = ["full"] }
hyper-rustls = { version = "0.24", optional = true }
ohttp = "0.5.1"
ohttp = { version = "0.5.1", features = ["k256"], path = "../../ohttp/ohttp" }
redis = { version = "0.23.3", features = ["aio", "tokio-comp"] }
rustls = { version = "0.21", optional = true }
tokio = { version = "1.12.0", features = ["full"] }
Expand Down
7 changes: 5 additions & 2 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn init_ohttp() -> Result<ohttp::Server> {
use ohttp::{KeyId, SymmetricSuite};

const KEY_ID: KeyId = 1;
const KEM: Kem = Kem::X25519Sha256;
const KEM: Kem = Kem::K256Sha256;
const SYMMETRIC: &[SymmetricSuite] =
&[SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)];

Expand Down Expand Up @@ -166,7 +166,10 @@ async fn handle_ohttp(
let response = handle_v2(pool, request).await?;

let (parts, body) = response.into_parts();
let mut bhttp_res = bhttp::Message::response(parts.status.as_u16());
let mut bhttp_res = bhttp::Message::response(
bhttp::StatusCode::try_from(parts.status.as_u16())
.map_err(|e| HandlerError::InternalServerError(e.into()))?,
);
let full_body = hyper::body::to_bytes(body)
.await
.map_err(|e| HandlerError::InternalServerError(e.into()))?;
Expand Down
10 changes: 6 additions & 4 deletions payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ exclude = ["tests"]
send = []
receive = ["bitcoin/rand"]
base64 = ["bitcoin/base64"]
v2 = ["bitcoin/rand", "bitcoin/serde", "chacha20poly1305", "dep:http", "bhttp", "ohttp", "serde", "url/serde"]
v2 = ["bitcoin/rand", "bitcoin/serde", "chacha20poly1305", "dep:http", "ohttp", "bhttp", "serde", "url/serde"]
io = ["reqwest/rustls-tls"]
danger-local-https = ["io", "reqwest/rustls-tls", "rustls"]

[dependencies]
bitcoin = { version = "0.32.2", features = ["base64"] }
bip21 = "0.5.0"
chacha20poly1305 = { version = "0.10.1", optional = true }
log = { version = "0.4.14"}
http = { version = "1", optional = true }
bhttp = { version = "=0.5.1", optional = true }
ohttp = { version = "0.5.1", optional = true }
hpke = { version = "0.11.0", optional = true, default-features = false, features = ["std", "k256"], git = "https://github.com/DanGould/rust-hpke", branch = "k256" }
log = { version = "0.4.14"}
ohttp = { version = "0.5.1", features = ["k256", "rust-hpke"], optional = true, path = "../../ohttp/ohttp" }
bhttp = { version = "0.5.1", optional = true, path = "../../ohttp/bhttp" }
rand = { version = "0.8.4", optional = true }
serde = { version = "1.0.186", default-features = false, optional = true }
reqwest = { version = "0.12", default-features = false, optional = true }
rustls = { version = "0.22.2", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ mod test {
use ohttp::hpke::{Aead, Kdf, Kem};
use ohttp::{KeyId, SymmetricSuite};
const KEY_ID: KeyId = 1;
const KEM: Kem = Kem::X25519Sha256;
const KEM: Kem = Kem::K256Sha256;
const SYMMETRIC: &[SymmetricSuite] =
&[ohttp::SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)];

Expand Down
8 changes: 4 additions & 4 deletions payjoin/src/uri/url_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ mod tests {
let mut url = Url::parse("https://example.com").unwrap();

let ohttp_keys =
OhttpKeys::from_str("AQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM").unwrap();
OhttpKeys::from_str("AQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM").unwrap();
let _ = url.set_ohttp(Some(ohttp_keys.clone()));
assert_eq!(
url.fragment(),
Some("ohttp=AQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM")
Some("ohttp=AQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM")
);

assert_eq!(url.ohttp(), Some(ohttp_keys));
Expand Down Expand Up @@ -124,7 +124,7 @@ mod tests {
// fragment is not percent encoded so `&ohttp=` is parsed as a query parameter, not a fragment parameter
let uri = "bitcoin:12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?amount=0.01\
&pj=https://example.com\
#exp=1720547781&ohttp=AQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM";
#exp=1720547781&ohttp=AQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM";
let uri = Uri::try_from(uri).unwrap().assume_checked().check_pj_supported().unwrap();
assert!(uri.extras.endpoint().ohttp().is_none());
}
Expand All @@ -133,7 +133,7 @@ mod tests {
fn test_valid_v2_url_fragment_on_bip21() {
let uri = "bitcoin:12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?amount=0.01\
&pj=https://example.com\
#ohttp%3DAQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM%26exp%3D1720547781";
#ohttp%3DAQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM%26exp%3D1720547781";
let uri = Uri::try_from(uri).unwrap().assume_checked().check_pj_supported().unwrap();
assert!(uri.extras.endpoint().ohttp().is_some());
}
Expand Down
7 changes: 5 additions & 2 deletions payjoin/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ pub fn ohttp_decapsulate(
let bhttp_body = res_ctx.decapsulate(ohttp_body)?;
let mut r = std::io::Cursor::new(bhttp_body);
let m: bhttp::Message = bhttp::Message::read_bhttp(&mut r)?;
let status_code = m.control().status().map_or(500, Into::into);
let status = http::StatusCode::try_from(status_code)
.map_err(|_| OhttpEncapsulationError::Bhttp(bhttp::Error::InvalidStatus))?;
http::Response::builder()
.status(m.control().status().unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR.into()))
.status(status)
.body(m.content().to_vec())
.map_err(OhttpEncapsulationError::Http)
}
Expand Down Expand Up @@ -345,7 +348,7 @@ mod test {
use ohttp::hpke::{Aead, Kdf, Kem};
use ohttp::{KeyId, SymmetricSuite};
const KEY_ID: KeyId = 1;
const KEM: Kem = Kem::X25519Sha256;
const KEM: Kem = Kem::K256Sha256;
const SYMMETRIC: &[SymmetricSuite] =
&[ohttp::SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)];
let keys = OhttpKeys(ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC)).unwrap());
Expand Down
2 changes: 1 addition & 1 deletion payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ mod integration {
#[tokio::test]
async fn test_bad_ohttp_keys() {
let bad_ohttp_keys =
OhttpKeys::from_str("AQAg3WpRjS0aqAxQUoLvpas2VYjT2oIg6-3XSiB-QiYI1BAABAABAAM")
OhttpKeys::from_str("AQAWBG3fkg7fQCN-bafc-BEJOSnDfq8k1M9Cy1kgQZX42GVOvI0bWVAciTaJCy2A_wy7R7VxtU88xej692bv0uXgt98ABAABAAM")
.expect("Invalid OhttpKeys");

let (cert, key) = local_cert_key();
Expand Down
Loading