Assuming the following object is defined in an .asn file.
TestObject := IMPLICIT [APPLICATION 7] SEQUENCE {
version UTF8String,
a GeneralizedTime OPTIONAL,
b [APPLICATION C0] IMPLICIT SEQUENCE OF [PRIVATE P0] IMPLICIT ad OPTIONAL,
c [APPLICATION C1] IMPLICIT SEQUENCE OF ab OPTIONAL,
d [APPLICATION C2] IMPLICIT ac OPTIONAL,
e [APPLICATION C3] IMPLICIT ad OPTIONAL,
f [APPLICATION C4] IMPLICIT al OPTIONAL,
g [APPLICATION C6] IMPLICIT SEQUENCE OF an OPTIONAL,
h ASNSyncPayload OPTIONAL,
signature NULLOCTETSTRING,
}
There are multiple optional fields and I am not sure how they will be encoded.
Can someone explain a way I can encode the following to a binary file possibly using libtasn1?
Please note that your tags [APPLICATION C0] through [APPLICATION C6] are not valid ASN.1 tags. The same is true for [PRIVATE P0]. Remove the 'C's and 'P' so that you have [APPLICATION 0] through APPLICATION 6] and [PRIVATE 0] and your tags will then be fine. Also, ASN.1 type names must begin with an uppercase letter, so "ad", "ab", "ac", "al" and "an" are not valid type names.
ASN.1 has several encoding rules which handle optional fields in different manners. BER, DER, and CER use the presence or absence of the ASN.1 tags in an encoding to determine the presence or absence of a component. This means that if an encoding has the tag, length and value for the version field followed by the tag length and value for the signature field, all of the intervening optional fields are absent.
PER uses a bitmask up front in each sequence with a bit for each optional field to indicate its presence or absence.
You can try your ASN.1 example in the free online ASN.1 compiler with encoder/decoder at http://asn1-playground.oss.com to see in detail how the optional fields are handled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With