genkgo/php-asn1
Encode and decode arbitrary ASN.1 structures in PHP using ITU-T X.690 (DER/BER). Build or parse X.509/PKI data like CSRs, certificates, and CRLs, manipulate objects, then re-encode. Requires supported PHP plus gmp or bcmath.
Installation:
composer require genkgo/php-asn1
Ensure your PHP version is ≥8.1 and includes either gmp or bcmath.
First Use Case: Encode a simple ASN.1 structure (e.g., a DER-encoded X.509 certificate component):
use FG\ASN1\Universal\Integer;
use FG\ASN1\Universal\Sequence;
$version = new Integer(2); // Version 2
$binary = $version->getBinary();
echo base64_encode($binary); // Output: MIIA
Where to Look First:
FG\ASN1\Universal\* classes (e.g., Integer, Sequence, ObjectIdentifier) for common ASN.1 constructs.FG\ASN1\TemplateParser for strict validation of decoded binary data.Building Complex Structures:
Use Sequence or Set to group primitive types (e.g., Integer, IA5String):
use FG\ASN1\Universal\Sequence;
use FG\ASN1\Universal\Integer;
use FG\ASN1\Universal\IA5String;
$sequence = new Sequence(
new Integer(123),
new IA5String('test@example.com')
);
$binary = $sequence->getBinary();
Reusing OIDs:
Leverage predefined OIDs (e.g., OID::RSA_ENCRYPTION) to avoid hardcoding:
use FG\ASN1\OID;
$oid = new ObjectIdentifier(OID::RSA_ENCRYPTION);
Context-Specific Constraints:
Use FG\ASN1\ContextSpecific\* for tagged data (e.g., in PKCS#7 or CMS):
use FG\ASN1\ContextSpecific\ContextSpecific;
$taggedData = new ContextSpecific(0, new Integer(42)); // Tag 0
Flexible Parsing: Parse binary data without a schema:
use FG\ASN1\ASNObject;
$asnObject = ASNObject::fromBinary($binaryData);
Strict Validation:
Use TemplateParser to enforce structure:
use FG\ASN1\TemplateParser;
$template = [
Identifier::SEQUENCE => [
Identifier::INTEGER,
Identifier::IA5_STRING,
]
];
$parser = new TemplateParser();
$object = $parser->parseBinary($binaryData, $template);
Iterative Navigation: Traverse decoded objects with array access or iterators:
foreach ($asnObject as $child) {
if ($child instanceof Integer) {
echo $child->getValue(); // Output: 123
}
}
PKI Use Cases:
Sequence, Set, and ObjectIdentifier.SubjectPublicKeyInfo structure for X.509 certificates.Binary Data Handling:
base64_encode()/base64_decode() for transport/storage (e.g., in databases or APIs).Error Handling:
try-catch blocks to handle malformed ASN.1 data:
try {
$object = ASNObject::fromBinary($binaryData);
} catch (\Exception $e) {
log::error("Invalid ASN.1 data: " . $e->getMessage());
}
Encoding Rules:
ASNObject::fromBinary() with explicit tagging.OCTET_STRING), which may fail if data exceeds 127 bytes without proper handling.OID Resolution:
1.2.3 are case-sensitive. Use OID::* constants (e.g., OID::PKCS7_DATA) where available.TemplateParser Strictness:
try-catch or validate templates against known schemas (e.g., RFC 5280 for X.509).PHP Extensions:
gmp/bcmath: Large integers (e.g., RSA moduli) will fail. Install extensions or use bcmath as a fallback.Binary Data Corruption:
substr() on raw ASN.1 blobs). Re-encode if changes are needed.Inspect Raw Binary:
Use bin2hex() to debug binary data:
echo bin2hex($binaryData); // Hex dump for manual inspection
Validate with OpenSSL: Cross-check generated DER with OpenSSL:
openssl asn1parse -in certificate.der
Template Debugging: Test templates incrementally:
$template = [Identifier::SEQUENCE => [Identifier::INTEGER]]; // Start simple
Custom Types:
Extend FG\ASN1\ASNObject to support domain-specific ASN.1 constructs:
class CustomType extends ASNObject {
protected $value;
public function __construct($value) { $this->value = $value; }
public function getBinary() { /* Implement custom encoding */ }
}
OID Registry:
Extend FG\ASN1\OID with custom OIDs:
class ExtendedOID extends OID {
const CUSTOM_OID = '1.3.6.1.4.1.12345';
}
Performance:
CURL for OID Names:
2.5.4.3 → cn). Disable this behavior by setting:
FG\ASN1\OID::setFetchOidNames(false);
curl extension if enabled.Strict Mode: Enable strict ASN.1 validation (e.g., for security-sensitive data):
FG\ASN1\ASNObject::setStrictMode(true);
How can I help you explore Laravel packages today?