如何在phpseclib上设置Application 7标记和无标记ASN1

时间:2018-04-14 05:56:56

标签: php phpseclib asn1

我有一个编码ASN1的地图,就像我正在使用phpseclip进行编码:

IdentityIdentificationDataObjectMap = array('type' => FILE_ASN1_TYPE_SEQUENCE,
    'children' => array(
        'identityIdentificationDataObject' => array('type' => FILE_ASN1_TYPE_SEQUENCE,
            'children' => array(
                'identityIdentificationData' => array('type' => FILE_ASN1_TYPE_SEQUENCE,
                    'children' => array(
                        'version' => array('type' => FILE_ASN1_TYPE_IA5_STRING),
                        'staticData' => array('type' => FILE_ASN1_TYPE_SEQUENCE,
                            'children' => array(
                                'acceptedPolicyVersion' => array('type' => FILE_ASN1_TYPE_IA5_STRING),
                                'cardHolderID' => array('type' => FILE_ASN1_TYPE_INTEGER),
                                'deviceSerialNumber' => array('type' => FILE_ASN1_TYPE_SEQUENCE,
                                    'children' => array(
                                        'deviceType' => array('type' => FILE_ASN1_TYPE_INTEGER),
                                        'deviceUniqueID' => array('type' => FILE_ASN1_TYPE_OCTET_STRING)
                                    ),
                                ),
                                'appLabel' => array('type' => FILE_ASN1_TYPE_UTF8_STRING),
                                'requestorRole' => array('type' => FILE_ASN1_TYPE_ENUMERATED,
                                    'mapping' => array(
                                        'roleClient',
                                        'roleParticipant'
                                    ),
                                ),
                                .
                                .
                                .

当我用我的数组编码时,输出就像这个网站上的输出:https://lapo.it/asn1js/

SEQUENCE(1 elem)
  SEQUENCE(2 elem)
    SEQUENCE(2 elem)
      IA5String 2.0
      SEQUENCE(6 elem)
        IA5String 2
        INTEGER(37 bit) 90000100526
        SEQUENCE(2 elem)
          INTEGER 3
          .
          .
          .

但是我想将Application 7标签添加到我的ASN1中,我希望输出像那样。

Application 7(2 elem)
  [0](2 elem)
    [0]2.0
    [1](6 elem)
      [0]2
      [1]90000100526
      [2](2 elem)
        [0](1 byte) 3
          .
          .
          .

我想知道是否可以使用phpseclip提供Application X标记。我的PHP版本5.5。就像没有任何标签的那样只需要[0] [1]标签就可以将我的数组编码为ASN1。

首先有可能如何? TNX。寻求答案。

1 个答案:

答案 0 :(得分:1)

如果您使用最新版本的phpseclib 1.0(本文中的1.0.11),则应该可以。 https://github.com/phpseclib/phpseclib/blob/1.0.11/tests/Unit/File/ASN1Test.php#L303提供了一个示例:

public function testApplicationTag()
{
    $map = array(
        'type'     => FILE_ASN1_TYPE_SEQUENCE,
        'children' => array(
            // technically, default implies optional, but we'll define it as being optional, none-the-less, just to
            // reenforce that fact
            'version'             => array(
                // if class isn't present it's assumed to be FILE_ASN1_CLASS_UNIVERSAL or
                // (if constant is present) FILE_ASN1_CLASS_CONTEXT_SPECIFIC
                'class'    => FILE_ASN1_CLASS_APPLICATION,
                'cast'     => 2,
                'optional' => true,
                'explicit' => true,
                'default'  => 'v1',
                'type'     => FILE_ASN1_TYPE_INTEGER,
                'mapping' => array('v1', 'v2', 'v3')
            )
        )
    );
    $data = array('version' => 'v3');
    $asn1 = new File_ASN1();
    $str = $asn1->encodeDER($data, $map);
    $decoded = $asn1->decodeBER($str);
    $arr = $asn1->asn1map($decoded[0], $map);
    $this->assertSame($data, $arr);
}