phpseclib asn1编码结构

时间:2018-03-22 06:07:38

标签: php phpseclib asn1

我试图使用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);

1 个答案:

答案 0 :(得分:0)

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