Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Asn1 Laravel Package

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.

Deep Wiki
Context7

Getting Started

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

Implementation Patterns

  • Building structures: compose types such as Sequence, Set, Integer, OctetString, ObjectIdentifier, and BitString. Call toDER() on the root element to serialize.
  • Parsing: DER::fromDER($binary) returns an Element. Use instanceof checks or ->as() methods to access concrete types. For sequences, iterate with ->asSequence()->elements().
  • Tagged types: for X.509/PKCS structures use ExplicitTagging / ImplicitTagging wrappers around elements to add context-specific tags.
  • ECDSA signature conversion:
    • DER → raw: parse Sequence of two Integers, extract binary values, left-pad to curve size, concatenate r || s.
    • Raw → DER: split raw into r and s, create Integer from binary, wrap in Sequence, call toDER().
  • OpenSSL integration: openssl_sign with ECDSA produces DER. Use this library to convert to raw for JWT signing/verification.
  • Error handling: catch Sop\Asn1\Exception\DecodeException (or the library’s exception hierarchy) when parsing untrusted input.
  • Testing: use hex strings from openssl asn1parse or known test vectors to assert round-trip correctness.

Gotchas and Tips

  • DER strictness: indefinite lengths, non-minimal integer encodings, and wrong tags will throw. Validate before parsing if you must accept BER.
  • Integer handling: ASN.1 INTEGER is signed big-endian. Use Integer::fromDER or construct with gmp for large values. Negative numbers require two’s complement.
  • BitString: always specify unused bits (usually 0 for public keys). OctetString and BitString are not interchangeable.
  • OID registry: use dotted strings like 1.2.840.113549.1.1.1. The library may validate known OIDs.
  • Time formats: UTCTime is YYMMDDHHMMSSZ; GeneralizedTime is YYYYMMDDHHMMSSZ. Use the provided classes to avoid format errors.
  • Performance: parsing large certificates can be slow. Cache parsed results in Laravel’s cache or a local property.
  • Debugging: compare bin2hex($der) with openssl asn1parse -inform DER -in file.der. Inspect toDER() output for round-trip mismatches.
  • Extension points: implement custom types by extending Element or using Type wrappers if your protocol needs non-standard tags.
  • Dependencies: check for ext-gmp or ext-bcmath; big integer operations may require them.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings
coleus/apps
seampact/laravel
emmyconceptng/laravel-ng-postcode
jjsquady/round-robin