All Projects → fgrosse → Phpasn1

fgrosse / Phpasn1

Licence: mit
A PHP library to encode and decode arbitrary ASN.1 structures using ITU-T X.690 encoding rules.

Projects that are alternatives of or similar to Phpasn1

Scodec
Scala combinator library for working with binary data
Stars: ✭ 709 (+421.32%)
Mutual labels:  encoding, binary, decoding
X509
A PHP library for X.509 public key certificates, attribute certificates, certification requests and certification path validation.
Stars: ✭ 27 (-80.15%)
Mutual labels:  encoding, decoding, x509
Binary
Generic and fast binary serializer for Go
Stars: ✭ 86 (-36.76%)
Mutual labels:  encoding, binary, decoding
sirdez
Glorious Binary Serialization and Deserialization for TypeScript.
Stars: ✭ 20 (-85.29%)
Mutual labels:  encoding, binary, decoding
Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (-39.71%)
Mutual labels:  encoding, binary, decoding
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (+354.41%)
Mutual labels:  encoding, binary, decoding
Stegify
🔍 Go tool for LSB steganography, capable of hiding any file within an image.
Stars: ✭ 927 (+581.62%)
Mutual labels:  encoding, decoding
Rust Multibase
Multibase in rust
Stars: ✭ 30 (-77.94%)
Mutual labels:  encoding, decoding
Vxg.media.sdk.android
Market leading Android SDK with encoding, streaming & playback functionality
Stars: ✭ 119 (-12.5%)
Mutual labels:  encoding, decoding
Certlint
X.509 certificate linter, written in Go
Stars: ✭ 60 (-55.88%)
Mutual labels:  x509, pki
Decodify
Detect and decode encoded strings, recursively.
Stars: ✭ 670 (+392.65%)
Mutual labels:  encoding, decoding
Fast ber
A C++11 ASN.1 BER Encoding and Decoding Library
Stars: ✭ 54 (-60.29%)
Mutual labels:  encoding, decoding
X509
Elixir package for working with X.509 certificates, Certificate Signing Requests (CSRs), Certificate Revocation Lists (CRLs) and RSA/ECC key pairs
Stars: ✭ 68 (-50%)
Mutual labels:  x509, pki
Anycodable
Type-erased wrappers for Encodable, Decodable, and Codable values
Stars: ✭ 811 (+496.32%)
Mutual labels:  encoding, decoding
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+418.38%)
Mutual labels:  encoding, decoding
Libbrotli
meta project to build libraries from the brotli source code
Stars: ✭ 110 (-19.12%)
Mutual labels:  encoding, decoding
Bincode
A binary encoder / decoder implementation in Rust.
Stars: ✭ 1,100 (+708.82%)
Mutual labels:  encoding, binary
Flac
Free Lossless Audio Codec
Stars: ✭ 593 (+336.03%)
Mutual labels:  encoding, decoding
Codability
Useful helpers for working with Codable types in Swift
Stars: ✭ 125 (-8.09%)
Mutual labels:  encoding, decoding
Ldpc
C and MATLAB implementation for LDPC encoding and decoding
Stars: ✭ 76 (-44.12%)
Mutual labels:  encoding, decoding

PHPASN1

Build Status PHP 7 ready Coverage Status

Latest Stable Version Total Downloads Latest Unstable Version License

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.

Dependencies

PHPASN1 requires at least PHP 7.0 and either the gmp or bcmath extension. Support for older PHP versions (i.e. PHP 5.6) was dropped starting with v2.0. 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.

Installation

The preferred way to install this library is to rely on Composer:

$ composer require fgrosse/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\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('1.2.250.1.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 FG\ASN1\TemplateParser:

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. Sequence and Set) can be navigated by array access or using an iterator. You can find examples here, here and here.

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?

If you found an issue or have a question submit a github issue with detailed information.

In case you already know what caused the issue and feel in the mood to fix it, your code contributions are always welcome. Just fork the repository, implement your changes and make sure that you covered everything with tests. Afterwards submit a pull request via github and be a little patient :) I usually try to comment and/or merge as soon as possible.

Mailing list

New features or questions can be discussed in this google group/mailing list.

Thanks

To all contributors so far!

License

This library is distributed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].