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

How do I convert an asn1_rs::Any to a BerObject? #67

Closed
lilyball opened this issue Apr 14, 2023 · 4 comments
Closed

How do I convert an asn1_rs::Any to a BerObject? #67

lilyball opened this issue Apr 14, 2023 · 4 comments

Comments

@lilyball
Copy link

NOT A CONTRIBUTION

I'm looking to update my code that uses x509-parser (and transitively der-parser). I have code that takes an x509_parser::x509::AlgorithmIdentifier, reads the parameters field, and pretty-prints it. In x509-parser v0.12, the parameters are a DerObject so that works. In v0.15 it's an Any. The pretty-printing provided by der-parser exists on BerObject.

So, my question, how do I convert an asn1_rs::Any to a BerObject? I would have expected BerObject to implement TryFrom<Any> but it doesn't1. Is there any alternative way I'm missing?

Footnotes

  1. I see in the source a comment saying this is because of the lack of a max_depth argument, but I would have expected such an implementation to be recursive, and perhaps a method on BerObject for converting from Any with a maximum depth specified.

@chifflier
Copy link
Member

Hi,
Thank you for your report. The Any type is expected to be more generic than BerObject, and have more conversion traits etc. However, I see multiple issues here:

  • there is no pretty-printer for Any. I've created an issue Add a pretty-printer for Any asn1-rs#25, if you have any comment please move them to this issue.
  • Implementing From<Any> for `BerObject. This is not yet done, but will be added.

In addition to the above comments, I was wondering why you need a BerObject. Is it only for printing?

@chifflier
Copy link
Member

Note: since Any contains the header and raw data, the ber_read_element_content_as function can be used.
I've used it to implement TryFrom, here's the relevant code:

let (header, data) = (any.header, any.data);
let (_, content) = ber_read_element_content_as(
    data,
    header.tag(),
    header.length(),
    header.constructed(),
    MAX_RECURSION,
)?;
let obj = BerObject::from_header_and_content(header, content);

Of course, this is only useful until TryFrom is merged.

@lilyball
Copy link
Author

In addition to the above comments, I was wondering why you need a BerObject. Is it only for printing?

In this particular case, yes. I'm updating code that pretty-printed algorithm parameters and now it can't. I stopped doing the upgrade when I hit this issue so I don't know if I'm going to run into Any anywhere else, but I don't expect to need anything beyond pretty-printing and possibly custom parsing of the contents for custom oids.

@lilyball
Copy link
Author

Actually what I said wasn't true, I also have code that tests if the parameters is an Oid, a Sequence, or Null, though I should be able to adapt that to operate on the Any directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants