1

我正在尝试使用 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);
4

1 回答 1

0

在 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 在运行后会自行生成该值,DateTime或者strtotime(根据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

于 2018-03-22T12:38:18.667 回答