如何在phpseclib中设置Application 7标签及无标签ASN.1
Great question! Working with custom ASN.1 tags in phpseclib is straightforward once you know where to add the tag definitions. Let's break this down step by step.
Adding Application 7 Tags
In phpseclib's ASN.1 mapping format, you specify custom tags using the 'tag' key in your array definition. For an Application 7 tag, you'll use an array with two keys:
'class': Set toFILE_ASN1_CLASS_APPLICATION(this tells phpseclib it's an application-class tag)'number': Set to7(the tag number you need)
Let's apply this to your existing map. I'll fill in the truncated part of your original structure (assuming staticData is a sequence, which is common in such objects) and add the Application 7 tag to a typical target point. For example, if you want to tag the outer identityIdentificationDataObject sequence:
$IdentityIdentificationDataObjectMap = array( 'type' => FILE_ASN1_TYPE_SEQUENCE, 'children' => array( 'identityIdentificationDataObject' => array( // Add Application 7 tag here 'tag' => array( 'class' => FILE_ASN1_CLASS_APPLICATION, 'number' => 7 ), '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) // Add your staticData child fields here if needed ) ) ) ) ) );
If you need to tag a different element (like the identityIdentificationData sequence instead), just move the 'tag' key to that array entry.
Handling Untagged Elements
Untagged elements are the default in phpseclib—you don't need any special configuration for them. If an element in your map doesn't have a 'tag' key, it will be encoded as an untagged ASN.1 type.
For example, your version field (an IA5String) is already untagged in the original map, and it will stay that way unless you explicitly add a tag to its definition. If you ever need to override a parent's tagging context and force an element to be untagged, simply omit the 'tag' key from that element's array.
Verifying the Encoding
After setting up your map, you can verify the ASN.1 output using phpseclib's ASN1::encode() method, then parse it back with ASN1::decode() or a tool like openssl asn1parse to ensure the tags are applied correctly.
内容的提问来源于stack exchange,提问作者BMErEr

