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

as_bytes method for TimestampResponse #62

Merged
merged 3 commits into from
Nov 7, 2024
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- TimestampResponse now has a `as_bytes` method to retrieve the original
request bytes ([#XX](https://github.com/trailofbits/rfc3161-client/pull/XX))

## [0.0.3] - 2024-11-06

### Added
Expand Down
18 changes: 11 additions & 7 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ impl TimeStampResp {
}
}

fn as_bytes<'p>(
&self,
py: pyo3::Python<'p>,
) -> PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let result = asn1::write_single(&self.raw.borrow_dependent());
match result {
Ok(response_bytes) => Ok(pyo3::types::PyBytes::new_bound(py, &response_bytes)),
Err(e) => Err(pyo3::exceptions::PyValueError::new_err(format!("{e}"))),
}
}

// Timestamp Token (as_bytes)
fn time_stamp_token<'p>(
&self,
Expand Down Expand Up @@ -771,13 +782,6 @@ mod tests {
.unwrap();

let py_bytes = pyo3::types::PyBytes::new_bound(py, &data);

// Does not work
// let raw = OwnedTimeStampResp::try_new(py_bytes.into(), |v| {
// RawTimeStampResp::parse_data(v.as_bytes(py))
// }).unwrap();

// Works
let raw = OwnedTimeStampResp::try_new(py_bytes.into(), |v| {
asn1::parse_single::<RawTimeStampResp>(v.as_bytes(py))
})
Expand Down
4 changes: 4 additions & 0 deletions src/rfc3161_client/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def signed_data(self) -> SignedData:
def time_stamp_token(self) -> bytes:
"""Return the bytes of the TimestampToken field."""

@abc.abstractmethod
def as_bytes(self) -> bytes:
"""Returns the Timestamp Response as bytes."""


TimeStampResponse.register(_rust.TimeStampResp)

Expand Down
8 changes: 8 additions & 0 deletions test/test_tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def test_equality(self):
(_FIXTURE / "response.tsr").read_bytes()
) == decode_timestamp_response((_FIXTURE / "response.tsr").read_bytes())

def test_round_trip(self):
timestamp_response = decode_timestamp_response((_FIXTURE / "response.tsr").read_bytes())
assert timestamp_response == decode_timestamp_response(timestamp_response.as_bytes())


class TestTimestampRequest:
def test_parsing_good(self):
Expand All @@ -53,3 +57,7 @@ def test_equality(self):
other_request = parse_timestamp_request((_FIXTURE / "other_request.der").read_bytes())

assert timestamp_request != other_request

def test_round_trip(self):
timestamp_request = parse_timestamp_request((_FIXTURE / "request.der").read_bytes())
assert timestamp_request == parse_timestamp_request(timestamp_request.as_bytes())