A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.
Installs: 17 991 345
- php: ^7.1 || ^8.0
- php-coveralls/php-coveralls: ~2.0
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
- ext-bcmath: BCmath is the fallback extension for big integer calculations
- ext-curl: For loading OID information from the web if they have not bee defined statically
- ext-gmp: GMP is the preferred extension for big integer calculations
- phpseclib/bcmath_compat: BCmath polyfill for servers where neither GMP nor BCmath is available
- dev-master / 2.0.x-dev
- dev-compat/php5-5 / 1.5.x-dev
This package is not auto-updated.
Last update: 2022-12-19 11:15:06 UTC
Notice: This library is no longer actively maintained!If you are currently using PHPASN1, this might not be an immediate problem for you, since this library was always rather stable.
However, you are advised to migrate to alternative packages to ensure that your applications remain functional also with newer PHP versions.
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.
The changelog can now be found at CHANGELOG.md.
PHPASN1 requires at least
PHP 7.0 and either the
Support for older PHP versions (i.e. PHP 5.6) was dropped starting with
If you must use an outdated PHP version consider using PHPASN v1.5.
For the loading of object identifier names directly from the web curl is used.
The preferred way to install this library is to rely on Composer:
$ composer require fgrosse/phpasn1
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\OID; 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 = new Integer(123456); $boolean = new Boolean(true); $enum = new Enumerated(1); $ia5String = new IA5String('Hello world'); $asnNull = new NullObject(); $objectIdentifier1 = new ObjectIdentifier('184.108.40.206.16.9'); $objectIdentifier2 = new ObjectIdentifier(OID::RSA_ENCRYPTION); $printableString = new PrintableString('Foo bar'); $sequence = new Sequence($integer, $boolean, $enum, $ia5String); $set = new 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\ASNObject; $base64String = ... $binaryData = base64_decode($base64String); $asnObject = ASNObject::fromBinary($binaryData); // do stuff
If you already know exactly how your expected data should look like you can use the
use FG\ASN1\TemplateParser; // first define your template $template = [ Identifier::SEQUENCE => [ Identifier::SET => [ Identifier::OBJECT_IDENTIFIER, Identifier::SEQUENCE => [ Identifier::INTEGER, Identifier::BITSTRING, ] ] ] ]; // if your binary data is not matching the template you provided this will throw an `\Exception`: $parser = new TemplateParser(); $object = $parser->parseBinary($data, $template); // there is also a convenience function if you parse binary data from base64: $object = $parser->parseBase64($data, $template);
You can use this function to make sure your data has exactly the format you are expecting.
Navigating decoded data
All constructed classes (i.e.
Set) can be navigated by array access or using an iterator.
You can find examples
Give me more examples!
To see some example usage of the API classes or some generated output check out the examples.
How do I contribute?
This project is no longer maintained and thus does not accept any new contributions.
To all contributors so far!
This library is distributed under the MIT License.