phpseclib asn1 encoding structure

夙愿已清 提交于 2020-01-15 12:07:18

问题


I'm trying to use phpseclib ASN1.php and i have a map like below;

$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)
                        )
                    )
                )
            )
        )
);

And i have a json and using json_decode(IdentityObject,true) for this map like below;

json:

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

And this jsons output array :

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

What structure this array should be to i can successfully compile.

Final code which gives this error

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

Final code:

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

回答1:


In File/X509.php there's only one enumerated type and it' defined thusly:

    $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'
        )
    );

Your enumerated type definition doesn't include a mapping key. That's probably what you need. eg.

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

That said, what version of phpseclib are you using? I tried your code with 1.0.10 (I think) and got a different error than the one you reported:

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

When I used my alternatively defined requestorRole definition I got this error message:

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

I was able to fix that last error by replacing 'creationTime' => '180319141236Z' with 'creationTime': 'January 1, 2018'. 180319141236Z is closer to the format that X.509 certs use but phpseclib generates that value itself after running it through DateTime or strtotime (per https://github.com/phpseclib/phpseclib/blob/1.0.10/phpseclib/File/X509.php#L3420) and then formatting it appropriately. If you wanted to set it directly, yourself, check this out:

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

Here's my code:

https://pastebin.com/cmmSf6S6



来源:https://stackoverflow.com/questions/49421661/phpseclib-asn1-encoding-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!