adapik / phpasn1
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.
Requires
- php: >=8.1
- adapik/gmp-polyfill: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
Suggests
- ext-gmp: For faster arbitrary-precision arithmetic; falls back to adapik/gmp-polyfill when unavailable
Provides
None
Conflicts
None
Replaces
None
- dev-master
- 2.6.0
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3
- 2.2.1
- 2.2
- 2.1
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- dev-compat/php5-5 / 1.5.x-dev
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.9.0
- dev-fix-php-errors-on-invalid-input
- dev-fixes-for-mapper
- dev-add-gmp-polyfill
- dev-integer-performance-improvement
- dev-expose-gmp-value
- dev-adopt-to-modern-phpunit
- dev-add-lazy-get-value
- dev-encoder
- dev-cleanup
- dev-dev-parser
This package is auto-updated.
Last update: 2026-10-08 16:54:23 UTC
README
A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules. This encoding is very frequently used in X.509 PKI environments or the communication between heterogeneous computer systems.
The API allows you to encode ASN.1 structures to create binary data such as certificate signing requests (CSR), X.509 certificates or certificate revocation lists (CRL). PHPASN1 can also read BER encoded binary data into separate PHP objects that can be manipulated by the user and reencoded afterwards.
Unlike the original fgrosse/PHPASN1 library, this library can:
- decode BER data that uses indefinite length encoding
- modify the structure of decoded ASN.1 objects (add, replace or remove nested elements) and encode them again
The changelog can now be found at CHANGELOG.md.
Dependencies
PHPASN1 requires at least PHP 8.1.
It is strongly recommended to install the gmp extension,
which is significantly faster for big integer arithmetic.
If the extension is not available, the pure PHP adapik/gmp-polyfill
(installed automatically via Composer) is used instead.
The polyfill should only be relied on in environments where installing the extension is not possible,
or for demonstration purposes.
If you have the extension installed, you can avoid downloading the polyfill by excluding it
in the composer.json of your project using the replace section:
{
"replace": {
"adapik/gmp-polyfill": "*"
}
}
Installation
The preferred way to install this library is to rely on Composer:
$ composer require adapik/phpasn1
Usage
Encoding ASN.1 Structures
PHPASN1 offers you a class for each of the implemented ASN.1 universal types. The constructors should be pretty self explanatory so you should have no big trouble getting started. All data will be encoded using DER encoding
use FG\ASN1\Universal\Integer; use FG\ASN1\Universal\Boolean; use FG\ASN1\Universal\Enumerated; use FG\ASN1\Universal\IA5String; use FG\ASN1\Universal\ObjectIdentifier; use FG\ASN1\Universal\PrintableString; use FG\ASN1\Universal\Sequence; use FG\ASN1\Universal\Set; use FG\ASN1\Universal\NullObject; $integer = Integer::create(123456); $boolean = Boolean::create(true); $enum = Enumerated::create(1); $asnNull = NullObject::create(); $objectIdentifier1 = ObjectIdentifier('1.2.250.1.16.9'); $printableString = PrintableString::createFromString('Foo bar'); $sequence = Sequence::create([$integer, $boolean, $enum, $ia5String]); $set = Set([$sequence, $asnNull, $objectIdentifier1, $objectIdentifier2, $printableString]); $myBinary = $sequence->getBinary(); $myBinary .= $set->getBinary(); echo base64_encode($myBinary);
Decoding binary data
Decoding BER encoded binary data is just as easy as encoding it:
use FG\ASN1\Object; $base64String = ... $binaryData = base64_decode($base64String); $asnObject = Object::fromBinary($binaryData); // do stuff
You can use this function to make sure your data has exactly the format you are expecting.
Thanks
To all contributors so far!
License
This library is distributed under the MIT License.