php asn.1,A PHP library to encode and decode arbitrary ASN.1 structures using ITU-T X.690 encoding r...

PHPASN1是一款PHP框架,允许用户使用ITU-T X.690编码规则来编码和解码任意的ASN.1结构。该工具广泛应用于X.509 PKI环境和异构计算机系统之间的通信。它支持创建证书签名请求、X.509证书等,并能读取BER编码的二进制数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHPASN1

46cb4a80b3941dda42ba499c02722213.png

687474703a2f2f7068703772656164792e74696d6573706c696e7465722e63682f6667726f7373652f50485041534e312f62616467652e737667

68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6667726f7373652f50485041534e312f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562

68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f762f737461626c652e706e67

68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f646f776e6c6f6164732e706e67

68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f762f756e737461626c652e706e67

68747470733a2f2f706f7365722e707567782e6f72672f6667726f7373652f70687061736e312f6c6963656e73652e706e67

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

License

This library is distributed under the MIT License.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值