perl Convert :: ASN1解码不起作用

时间:2015-08-04 11:28:57

标签: perl asn.1 ocsp

我正在尝试使用perl Convert :: ASN1解码asn.1 OCSP请求 我得到的十六进制转储如下:

30773075304E304C304A300906052B0E03021A050004146283D6C38BF724E2EE10A7D2829A4F906E48F3F2041423490CF9B7D39B1BD93A60A2A67877894782E96F021100B1C544D7AFA4039D4F482BDDEE975E38A2233021301F06092B060105050730010204120410ABE72957E85AE50E8B9628DB495BD5D5

我使用在线工具来验证它是一个有效的asn.1编码,结构如下

SEQUENCE {
   SEQUENCE {
      SEQUENCE {
         SEQUENCE {
            SEQUENCE {
               SEQUENCE {
                  OBJECTIDENTIFIER 1.3.14.3.2.26 (id_sha1)
                  NULL 
               }
               OCTETSTRING 6283D6C38BF724E2EE10A7D2829A4F906E48F3F2
               OCTETSTRING 23490CF9B7D39B1BD93A60A2A67877894782E96F
               INTEGER 0x00B1C544D7AFA4039D4F482BDDEE975E38
            }
         }
      }
      [2] {
         SEQUENCE {
            SEQUENCE {
               OBJECTIDENTIFIER 1.3.6.1.5.5.7.48.1.2
               OCTETSTRING 0410ABE72957E85AE50E8B9628DB495BD5D5
            }
         }
      }
   }
}

我将十六进制打包解码

my $data = "30773075304E304C304A300906052B0E03021A050004146283D6C38BF724E2EE10A7D2829A4F906E48F3F2041423490CF9B7D39B1BD93A60A2A67877894782E96F021100B1C544D7AFA4039D4F482BDDEE975E38A2233021301F06092B060105050730010204120410ABE72957E85AE50E8B9628DB495BD5D5";
my $asn1Val=pack("H*",$data);

我从这个

创建了我的asn1.1架构
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) }

>)

当我尝试对上述内容进行解码时,它会运行一段时间并且会失败。

asn_dump($data); # This works fine
my $decoded=$asn->decode($data) or print $asn->error();
print Dumper ($decoded); 

我认为架构是正确的,但解码失败了 我无法在网上找到很多例子。

1 个答案:

答案 0 :(得分:1)

你在这里说的重点是你说&#34;它有效地工作了几次并且失败了几次&#34;。对我来说,这是一个线索,你没有使用find()告诉Convert :: ASN1在你的定义中从哪里开始。

如果您准备的ASN.1中有多个宏(一个typedef),则必须使用find()。然后使用find()中的结果对象进行解码()。还要记住,要始终从find()和decode()检查返回状态;如果是undef,则错误将出现在 - &gt; error()中,对于您使用的对象。

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;

这是一个猜测,因为你的问题没有给我足够的背景,但我希望它有所帮助。