sop/asn1
sop/asn1 is a pure-PHP library for encoding and decoding ASN.1 data (BER/DER), with an object-oriented API for building and parsing elements. Useful for X.509 certificates, PKCS, SNMP, LDAP, and other binary protocols.
Install with Composer:
composer require sop/asn1
Start by exploring the src/Type directory (primitive and constructed types) and the DER/Decoder class for parsing. The quickest first use case is converting an ECDSA signature between DER and raw formats—a common need when integrating with JWT libraries that expect raw signatures.
Minimal example:
use Sop\Asn1\Type\Primitive\Integer;
use Sop\Asn1\Type\Constructed\Sequence;
use Sop\Asn1\DER;
$seq = new Sequence(new Integer(42), new Integer(99));
$der = $seq->toDER();
$parsed = DER::fromDER($der); // returns Element
Sequence, Set, Integer, OctetString, ObjectIdentifier, and BitString. Call toDER() on the root element to serialize.DER::fromDER($binary) returns an Element. Use instanceof checks or ->as() methods to access concrete types. For sequences, iterate with ->asSequence()->elements().ExplicitTagging / ImplicitTagging wrappers around elements to add context-specific tags.Sequence of two Integers, extract binary values, left-pad to curve size, concatenate r || s.r and s, create Integer from binary, wrap in Sequence, call toDER().openssl_sign with ECDSA produces DER. Use this library to convert to raw for JWT signing/verification.Sop\Asn1\Exception\DecodeException (or the library’s exception hierarchy) when parsing untrusted input.openssl asn1parse or known test vectors to assert round-trip correctness.Integer::fromDER or construct with gmp for large values. Negative numbers require two’s complement.0 for public keys). OctetString and BitString are not interchangeable.1.2.840.113549.1.1.1. The library may validate known OIDs.UTCTime is YYMMDDHHMMSSZ; GeneralizedTime is YYYYMMDDHHMMSSZ. Use the provided classes to avoid format errors.bin2hex($der) with openssl asn1parse -inform DER -in file.der. Inspect toDER() output for round-trip mismatches.Element or using Type wrappers if your protocol needs non-standard tags.ext-gmp or ext-bcmath; big integer operations may require them.How can I help you explore Laravel packages today?