Open Arenash13 opened 5 months ago
You have multiple options
If you know the definition of your types, the recommended solution is to create a struct for each sequence and derive parsers to be able to call from_der
. See for example this unit test:
typeA
in your exampleBerSequence
derive and from_ber
in all the examples belowResulting code would look like:
#[derive(Debug, PartialEq, DerSequence)]
pub struct MyType {
a: TypeA,
b: TypeB,
}
#[derive(Debug, PartialEq, DerSequence)]
pub struct TypeA {
field1: u32,
...
}
let (rem, obj) = MyType::from_der(input)?;
If you can't derive or prefer to use manual parsers, the solution you describe will work, but it will not be idiomatic. You have multiple solutions (again, deriving is preferred):
let (rem, seq) = Sequence::from_der(input)?;
let (inner_rem, type_a) = Sequence::from_der(seq.content)?;
let (inner_rem, type_b) = Sequence::from_der(inner_rem)?;
let (rem, seq) = Sequence::from_der(input)?;
let (inner_rem, type_a) = Sequence::from_der_and_then(seq.content, |i| {
let (rem2, a) = u32::from_der(input)?;
...
Ok(...)
})?;
let (inner_rem, type_b) = Sequence::from_der(inner_rem)?;
let (rem, seq) = Sequence::from_der(input)?;
let (inner_rem, type_a) = TypeA::from_der(seq.content)?;
})?;
FromDer
. See for example x509-parser-0.16.0/src/revocation_list.rs#L128-L148 (and all other parsing code from the x509-parser
crate) for example using custom errors
Note: parse_der_sequence_defined_g
is the old way to parse a sequence, equivalent to Sequence::from_der_and_then
Thanks for your answer !
I will check for the idiomatic way if my use case grows. For now, I only need to retrieve a few fields in the BER data
I have a BER schema which looks like the following :
I try to retrieve fields that are located in typeA SEQUENCE, how can I inspect it in a way to deal with errors, i.e. is it possible to do something like the following :
Is it the right to do that ? I'm not able to find the correct code to write in order to run that.
Thanks in advance