phpseclib asn1编码结构

phpseclib asn1 encoding structure

我正在尝试使用 phpseclib ASN1.php 并且我有如下图;

$IdentityObjectMap = 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,
                                'roleClient'=> array('mapping' =>0),
                                'roleParticipant' =>array('mapping' =>1)
                            ),
                            'creationTime' => array('type' =>FILE_ASN1_TYPE_UTC_TIME)
                        )
                    )
                )
            )
        )
);

我有一个 json 并使用 json_decode(IdentityObject,true) 作为这张地图,如下所示;

json:

{
 \"identityIdentificationData\":{
      \"version\":\"2.0\",
      \"staticData\":{
         \"acceptedPolicyVersion\":\"2\",
         \"cardHolderID\":11111111111,
         \"deviceSerialNumber\":{
            \"deviceType\":3,
            \"deviceUniqueID\":\"11111111\"
          },
         \"appLabel\":\"examination\",
         \"requestorRole\": \"roleClient\",
         \"creationTime\": \"180319141236Z\"
       }
    }
}";

而这个 jsons 输出数组:

array 
  'identityIdentificationData' => 
    array 
      'version' => '2.0'
      'staticData' => 
    array
      'acceptedPolicyVersion' => '2'
      'cardHolderID' => 11111111111
      'deviceSerialNumber' => 
        array 
          'deviceType' => 3
          'deviceUniqueID' => '11111111'
      'appLabel' => 'examination' 
      'requestorRole' => 'roleClient' 
      'creationTime' => '180319141236Z' 

这个数组应该是什么结构才能编译成功

出现此错误的最终代码

Undefined index: children .../ASN1.php on line 950.

最终代码:

$asn1->encodeDER($IdentityObject,$IdentityObjectMap);

In File/X509.php 只有一种枚举类型,它是这样定义的:

    $this->CRLReason = array('type' => FILE_ASN1_TYPE_ENUMERATED,
       'mapping' => array(
                        'unspecified',
                        'keyCompromise',
                        'cACompromise',
                        'affiliationChanged',
                        'superseded',
                        'cessationOfOperation',
                        'certificateHold',
                        // Value 7 is not used.
                        8 => 'removeFromCRL',
                        'privilegeWithdrawn',
                        'aACompromise'
        )
    );

您的枚举类型定义不包含映射键。这可能就是你需要的。例如

                        'requestorRole' => array('type' => FILE_ASN1_TYPE_ENUMERATED,
                            'mapping' => array(
                                'roleClient',
                                'roleParticipant'
                            )
                        ),

也就是说,您使用的是哪个版本的 phpseclib?我用 1.0.10(我认为)尝试了你的代码,得到了一个与你报告的错误不同的错误:

Fatal error: Uncaught Error: Call to a member function toBytes() on string

当我使用我的替代定义的 requestorRole 定义时,我收到了这个错误消息:

Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (180319141236Z) at position 11 (6)

我能够通过将 'creationTime' => '180319141236Z' 替换为 'creationTime': 'January 1, 2018' 来修复最后一个错误。 180319141236Z 更接近 X.509 证书使用的格式,但 phpseclib 在 运行 之后通过 DateTimestrtotime(根据 https://github.com/phpseclib/phpseclib/blob/1.0.10/phpseclib/File/X509.php#L3420)自行生成该值,并且然后适当地格式化它。如果您想自己直接设置,请查看:

https://github.com/phpseclib/phpseclib/blob/1.0.10/phpseclib/File/X509.php#L3952

这是我的代码:

https://pastebin.com/cmmSf6S6