Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to decode asn.1 OCSP request with perl Convert::ASN1
The hex dump I get is as follows:
30773075304E304C304A300906052B0E03021A050004146283D6C38BF724E2EE10A7D2829A4F906E48F3F2041423490CF9B7D39B1BD93A60A2A67877894782E96F021100B1C544D7AFA4039D4F482BDDEE975E38A2233021301F06092B060105050730010204120410ABE72957E85AE50E8B9628DB495BD5D5
I used an online tool to verifiy that it is a valid asn.1 encoded and the structure is as follows
SEQUENCE {
SEQUENCE {
SEQUENCE {
SEQUENCE {
SEQUENCE {
SEQUENCE {
OBJECTIDENTIFIER 1.3.14.3.2.26 (id_sha1)
OCTETSTRING 6283D6C38BF724E2EE10A7D2829A4F906E48F3F2
OCTETSTRING 23490CF9B7D39B1BD93A60A2A67877894782E96F
INTEGER 0x00B1C544D7AFA4039D4F482BDDEE975E38
[2] {
SEQUENCE {
SEQUENCE {
OBJECTIDENTIFIER 1.3.6.1.5.5.7.48.1.2
OCTETSTRING 0410ABE72957E85AE50E8B9628DB495BD5D5
I pack the hex up to decode
my $data = "30773075304E304C304A300906052B0E03021A050004146283D6C38BF724E2EE10A7D2829A4F906E48F3F2041423490CF9B7D39B1BD93A60A2A67877894782E96F021100B1C544D7AFA4039D4F482BDDEE975E38A2233021301F06092B060105050730010204120410ABE72957E85AE50E8B9628DB495BD5D5";
my $asn1Val=pack("H*",$data);
I created my asn1.1 schema from this
my $asn = Convert::ASN1->new;
$asn->prepare( q<
OCSPRequest ::= SEQUENCE {
tbsRequest TBSRequest,
optionalSignature [0] EXPLICIT Signature OPTIONAL
TBSRequest ::= SEQUENCE {
version [0] EXPLICIT Version OPTIONAL, -- DEFAULT v1
-- requestorName [1] EXPLICIT GeneralName OPTIONAL,
requestList SEQUENCE OF Request,
requestExtensions [2] EXPLICIT Extensions OPTIONAL
Request ::= SEQUENCE {
reqCert CertID,
singleRequestExtensions [0] EXPLICIT Extensions OPTIONAL
AlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER,
parameters ANY DEFINED BY algorithm OPTIONAL
CertID ::= SEQUENCE {
hashAlgorithm AlgorithmIdentifier,
issuerNameHash OCTET STRING, -- Hash of issuer's DN
issuerKeyHash OCTET STRING, -- Hash of issuer's public key
serialNumber CertificateSerialNumber
CertificateSerialNumber ::= INTEGER
Extension ::= SEQUENCE {
extnID OBJECT IDENTIFIER,
critical BOOLEAN OPTIONAL, -- DEFAULT FALSE,
extnValue OCTET STRING
-- contains the DER encoding of an ASN.1 value
-- corresponding to the extension type identified
-- by extnID
Extensions ::= SEQUENCE OF Extension
Signature ::= SEQUENCE {
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING
--, certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL
Version ::= INTEGER -- { v1(0) }
When I try to deocode the above it works some times and fails somes time.
asn_dump($data); # This works fine
my $decoded=$asn->decode($data) or print $asn->error();
print Dumper ($decoded);
I think the schema is correct but the decoding is failing
I have not been able to find many examples online.
The significant point you make here is you say "it works some times and fails some times". To me, that's a clue that you did not use find() to tell Convert::ASN1 where to start in your definition.
You must use find() if there's more than one macro (one typedef) in the ASN.1 that you prepare. Then use the resulting object from the find() to do your decode(). Remember, too, to always check the return status from find() and decode(); if undef then the error will be in ->error() for the object you used.
my $asn = new Convert::ASN1;
my $ok = $asn->prepare( q< ...your asn.1 definition here... >);
die "*** Could not prepare definition: ".$asn->error()
if !$ok;
my $top = $asn->find("OCSPRequest");
die "*** Could not find top of structure: ".$asn->error()
if !$top;
my $result = $top->decode($your_pdu); # Use $top, NOT $asn !
die "*** Could not decode PDU: ".$top->error()
if !$result;
This is a bit of a guess as your question doesn't give me enough context, but I hope it helps.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.