如何在 phpseclib 上设置 Application 7 tag 和 tagless ASN1

How to set Application 7 tag and tagless ASN1 on phpseclib

我有一个用于编码 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 给应用程序 X 标记。我的 php 版本 5.5。就像那样,没有任何标签,只有 [0][1] 个标签将我的数组编码为 ASN1。

首先可能然后怎么办?谢谢。寻找答案。

如果您使用最新版本的 phpseclib 1.0(从本 post 开始为 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);
}