- Notifications
You must be signed in to change notification settings - Fork50
A PHP library to encode and decode arbitrary ASN.1 structures using ITU-T X.690 encoding rules.
License
fgrosse/PHPASN1
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
Another option is to fork this repository or useone of the existing forks.
⚠If you are using another fork, please make sure you trust the author and validate the code you are relying upon!
A PHP Framework that allows you to encode and decode arbitraryASN.1 structuresusing theITU-T X.690 Encoding Rules.This encoding is very frequently used inX.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 certificatesigning requests (CSR), X.509 certificates or certificate revocation lists (CRL).PHPASN1 can also readBER encoded binary data into separate PHP objects that can be manipulated by the user and reencoded afterwards.
Thechangelog can now be found atCHANGELOG.md.
PHPASN1 requires at leastPHP 7.0
and either thegmp
orbcmath
extension.Support for older PHP versions (i.e. PHP 5.6) was dropped starting withv2.0
.If you must use an outdated PHP version consider usingPHPASN v1.5.
For the loading of object identifier names directly from the webcurl is used.
The preferred way to install this library is to rely onComposer:
$ composer require fgrosse/phpasn1
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 usingDER encoding
useFG\ASN1\OID;useFG\ASN1\Universal\Integer;useFG\ASN1\Universal\Boolean;useFG\ASN1\Universal\Enumerated;useFG\ASN1\Universal\IA5String;useFG\ASN1\Universal\ObjectIdentifier;useFG\ASN1\Universal\PrintableString;useFG\ASN1\Universal\Sequence;useFG\ASN1\Universal\Set;useFG\ASN1\Universal\NullObject;$integer =newInteger(123456);$boolean =newBoolean(true);$enum =newEnumerated(1);$ia5String =newIA5String('Hello world');$asnNull =newNullObject();$objectIdentifier1 =newObjectIdentifier('1.2.250.1.16.9');$objectIdentifier2 =newObjectIdentifier(OID::RSA_ENCRYPTION);$printableString =newPrintableString('Foo bar');$sequence =newSequence($integer,$boolean,$enum,$ia5String);$set =newSet($sequence,$asnNull,$objectIdentifier1,$objectIdentifier2,$printableString);$myBinary =$sequence->getBinary();$myBinary .=$set->getBinary();echobase64_encode($myBinary);
Decoding BER encoded binary data is just as easy as encoding it:
useFG\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 theFG\ASN1\TemplateParser
:
useFG\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 =newTemplateParser();$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.
All constructed classes (i.e.Sequence
andSet
) can be navigated by array access or using an iterator.You can find exampleshere,here andhere.
To see some example usage of the API classes or some generated output check out theexamples.
This project is no longer maintained and thus does not accept any new contributions.
Toall contributors so far!
This library is distributed under theMIT License.