Skip to content

Conversation

@facutuesca
Copy link
Contributor

This PR adds support for BIT STRING to the ASN.1 API

@asn1.sequence
class Example
  field: asn1.BitString

bt = asn1.BitString(data=b"\x01\x10", padding_bits=2)

object = Example(field=bt)
encoded = encode_der(object)

decoded = decode_der(Example, encoded)
assert decoded.field.as_bytes() == b"\x01\x10"
assert decoded.field.padding_bits() == 2

Part of #12283

Signed-off-by: Facundo Tuesca <[email protected]>
@alex
Copy link
Member

alex commented Nov 22, 2025

@reaperhulk does a BitString type (with API that looks like rust-asn1: as_byes() + padding_bits()) look right to you?

@reaperhulk
Copy link
Member

There are two primary ways bit strings are used. One is the “wrong type”, as in a signature in x509, where you really just want the byte value. The other is an actual bit field, like key usage or CRL revocation reasons. This API seems fine for the former and lacking for the latter.

@alex
Copy link
Member

alex commented Dec 2, 2025

Nothing prevents us from introducing another representation of bitstring for that in the future. Will go ahead and review this PR as-is.

pub fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!(
"BitString(data: {}, padding_bits: {})",
self.data, self.padding_bits,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repr should be formatted like the constructor, so =, not :

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

self.data.clone_ref(py)
}

#[pyo3(signature = ())]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary for anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, fixed

Signed-off-by: Facundo Tuesca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants