.coveralls.yml 0000644 00000000015 15073675044 0007346 0 ustar 00 src_dir: lib
.gitignore 0000644 00000000177 15073675044 0006554 0 ustar 00 # Project and backup files
*~
*.project
.settings/*
.idea
# Composer
vendor
composer.lock
# Test coverage reports
clover.xml
.styleci.yml 0000644 00000000310 15073675044 0007026 0 ustar 00 preset: symfony
linting: true
disabled:
- empty_return
- phpdoc_align
- blankline_after_open_tag
- pre_increment
- return
- unalign_equals
- unalign_double_arrow
- phpdoc_separation
.travis.yml 0000644 00000000515 15073675044 0006671 0 ustar 00 language: php
sudo: false
php:
- 7.0
- 7.1
- 7.2
cache:
directories:
- vendor
before_script:
- composer install
script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
after_script:
# code coverage currently does not seem to be supported on travis php 7 or hhvm
- vendor/bin/coveralls --verbose
CHANGELOG.md 0000644 00000003062 15073675044 0006371 0 ustar 00 #### v.2.0.0 (2017-08)
* rename `FG\ASN1\Object` to `FG\ASN1\ASNObject` because `Object` is a special class name in the next major PHP release
- when you upgrade you have to adapt all corresponding `use` and `extends` statements as well as type hints and all
usages of `Object::fromBinary(…)`.
* generally drop PHP 5.6 support
#### v.1.5.2 (2016-10-29)
* allow empty octet strings
#### v.1.5.1 (2015-10-02)
* add keywords to composer.json (this is a version on its own so the keywords are found on a stable version at packagist.org)
#### v.1.5.0 (2015-10-30)
* fix a bug that would prevent you from decoding context specific tags on multiple objects [#57](https://github.com/fgrosse/PHPASN1/issues/57)
- `ExplicitlyTaggedObject::__construct` does now accept multiple objects to be tagged with a single tag
- `ExplicitlyTaggedObject::getContent` will now always return an array (even if only one object is tagged)
#### v.1.4.2 (2015-09-29)
* fix a bug that would prevent you from decoding empty tagged objects [#57](https://github.com/fgrosse/PHPASN1/issues/57)
#### v.1.4.1
* improve exception messages and general error handling [#55](https://github.com/fgrosse/PHPASN1/pull/55)
#### v.1.4.0
* **require PHP 5.6**
* support big integers (closes #1 and #37)
* enforce one code style via [styleci.io][9]
* track code coverage via [coveralls.io][10]
* replace obsolete `FG\ASN1\Exception\GeneralException` with `\Exception`
* `Construct` (`Sequence`, `Set`) does now implement `ArrayAccess`, `Countable` and `Iterator` so its easier to use
* add [`TemplateParser`][11]
LICENSE 0000644 00000002106 15073675044 0005563 0 ustar 00 Copyright (c) 2012-2015 Friedrich Große
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
README.md 0000644 00000014432 15073675044 0006042 0 ustar 00 PHPASN1
=======
[](http://travis-ci.org/fgrosse/PHPASN1)
[](https://travis-ci.org/fgrosse/PHPASN1)
[](https://coveralls.io/github/fgrosse/PHPASN1?branch=master)
[](https://packagist.org/packages/fgrosse/phpasn1)
[](https://packagist.org/packages/fgrosse/phpasn1)
[](https://packagist.org/packages/fgrosse/phpasn1)
[](https://packagist.org/packages/fgrosse/phpasn1)
A PHP Framework that allows you to encode and decode arbitrary [ASN.1][3] structures
using the [ITU-T X.690 Encoding Rules][4].
This encoding is very frequently used in [X.509 PKI environments][5] 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][6] 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](CHANGELOG.md).
## Dependencies
PHPASN1 requires at least `PHP 7.0` and the `gmp` 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][13].
For the loading of object identifier names directly from the web [curl][7] is used.
## Installation
The preferred way to install this library is to rely on [Composer][2]:
```bash
$ 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][8]
```php
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:
```php
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`:
```php
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](https://github.com/fgrosse/PHPASN1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L148-148),
[here](https://github.com/fgrosse/PHPASN1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/Universal/SequenceTest.php#L121) and
[here](https://github.com/fgrosse/PHPASN1/blob/f6442cadda9d36f3518c737e32f28300a588b777/tests/ASN1/TemplateParserTest.php#L45).
### Give me more examples!
To see some example usage of the API classes or some generated output check out the [examples](https://github.com/fgrosse/PHPASN1/tree/master/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][12].
### Thanks
To [all contributors][1] so far!
## License
This library is distributed under the [MIT License](LICENSE).
[1]: https://github.com/fgrosse/PHPASN1/graphs/contributors
[2]: https://getcomposer.org/
[3]: http://www.itu.int/ITU-T/asn1/
[4]: http://www.itu.int/ITU-T/recommendations/rec.aspx?rec=x.690
[5]: http://en.wikipedia.org/wiki/X.509
[6]: http://en.wikipedia.org/wiki/X.690#BER_encoding
[7]: http://php.net/manual/en/book.curl.php
[8]: http://en.wikipedia.org/wiki/X.690#DER_encoding
[9]: https://styleci.io
[10]: https://coveralls.io/github/fgrosse/PHPASN1
[11]: https://github.com/fgrosse/PHPASN1/blob/master/tests/ASN1/TemplateParserTest.php#L16
[12]: https://groups.google.com/d/forum/phpasn1
[13]: https://packagist.org/packages/fgrosse/phpasn1#1.5.2
composer.json 0000644 00000002456 15073675044 0007310 0 ustar 00 {
"name": "fgrosse/phpasn1",
"description": "A PHP Framework that allows you to encode and decode arbitrary ASN.1 structures using the ITU-T X.690 Encoding Rules.",
"type": "library",
"homepage": "https://github.com/FGrosse/PHPASN1",
"license": "MIT",
"authors": [
{
"name": "Friedrich Große",
"email": "friedrich.grosse@gmail.com",
"homepage": "https://github.com/FGrosse",
"role": "Author"
},
{
"name": "All contributors",
"homepage": "https://github.com/FGrosse/PHPASN1/contributors"
}
],
"keywords": [ "x690", "x.690", "x.509", "x509", "asn1", "asn.1", "ber", "der", "binary", "encoding", "decoding" ],
"require": {
"php": ">=7.0.0",
"ext-gmp": "*"
},
"require-dev": {
"phpunit/phpunit": "~6.3",
"satooshi/php-coveralls": "dev-master"
},
"suggest": {
"php-curl": "For loading OID information from the web if they have not bee defined statically"
},
"autoload": {
"psr-4": {
"FG\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"FG\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
examples/Issue14.php 0000644 00000014446 15073675044 0010354 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../vendor/autoload.php';
require_once 'shared.php';
use FG\ASN1\OID;
use FG\ASN1\ASNObject;
use FG\ASN1\Identifier;
use FG\ASN1\Universal\ObjectIdentifier;
use FG\ASN1\Universal\OctetString;
use FG\ASN1\Universal\Sequence;
$base64 = 'MIIINTCCBh2gAwIBAgIQbVVJc10C7UUjfYRHQ//7nTANBgkqhkiG9w0BAQsFADB0
MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDEtMCsGA1UECxMkQ2Vy
dGlzaWduIENlcnRpZmljYWRvcmEgRGlnaXRhb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LsXsUA5tRbzTwfXJFFOIgFDgrXl7V4JGjSI3GdyoiEECR9OfM/UFYtr/
pD194Xh6G0lSCuP9xOKpOJ7qk24mcfObcGxooMxmktkt0bevzx5w84RtXJfbube+
p6o7JP06T6JJTGfIUfNKYU8wJ6CV9eUHEMDLHMoH6wnYK7d8rHcAbSoTJGMFlSYZ
OqJrWAbKAPjraFNeNaH675u//Ck127kJVidcuRwtkLZV/OIlQaw/4QSp90QWQOQg
AsOWC9+h8v/RcCYTJpWM22MCq3Xk67nz+mXO8e7LKpzHEh2sjX3gkfw4h80zYfT7
kTsNXVdxAHXiIahKNeRUT8fGhxvyOA0RqAXQBUaOyLyGYWRJ7Is5IqAAU6XiBHYe
oJ3v8BTGYzIK2Ud5dZ23yzBp2ejdQzQX1ETpJoEdgELToHmfBXTkI+7ne59wSRkH
beQXoK5y0U3gh1vIz/53GG0QMuCvq9r9xTMERcFJcpQUxJ2RjwxIFHlIFbNwCeiW
I3DmZaXdR169kRoPIqkV3QIREs/yHBvkxXrwDt416stUnQ8KsxAEatE=';
try {
$binaryData = base64_decode($base64);
//hexdump($binaryData, false);
$rootObject = ASNObject::fromBinary($binaryData);
//printObject($rootObject);
// first navigate to the certificate extensions
// (see ITU-T X.509 section 7 "Public-keys and public-key certificates" for cert structure)
/* @var Sequence $rootObject */
assert($rootObject->getType() == Identifier::SEQUENCE);
$topLevelContainer = $rootObject->getChildren();
$certificateInfo = $topLevelContainer[0];
/* @var Sequence $certificateInfo */
assert($certificateInfo->getType() == Identifier::SEQUENCE);
// there need to be at least 8 child elements if the certificate extensions field is present
assert($certificateInfo->getNumberofChildren() >= 8);
$certInfoFields = $certificateInfo->getChildren();
$certExtensions = $certInfoFields[7];
// check if this is really the certificate extensions sequence
/* @var \FG\ASN1\ASNObject $certExtensions */
$certExtensionsType = $certExtensions->getType();
assert(Identifier::isContextSpecificClass($certExtensionsType));
assert(Identifier::getTagNumber($certExtensions->getType()) == 3);
// this should contain a sequence of extensions
$certExtensions = $certExtensions->getFirstChild();
assert($certExtensions->getType() == Identifier::SEQUENCE);
// now check all extensions and search for the SAN
/** @var \FG\ASN1\ASNObject $extensionSequence */
foreach ($certExtensions as $extensionSequence) {
assert($extensionSequence->getType() == Identifier::SEQUENCE);
assert($extensionSequence->getNumberofChildren() >= 2);
$extensionSequenceChildren = $extensionSequence->getChildren();
$objectIdentifier = $extensionSequenceChildren[0];
/* @var ObjectIdentifier $objectIdentifier */
assert($objectIdentifier->getType() == Identifier::OBJECT_IDENTIFIER);
if ($objectIdentifier->getContent() == OID::CERT_EXT_SUBJECT_ALT_NAME) {
// now we have the wanted octet string
$octetString = $extensionSequenceChildren[1];
/* @var OctetString $octetString */
$octetStringBinary = $octetString->getBinaryContent();
// At this point you may want to create the sequence from the binary value of
// the octet string and parse its structure like we did so far.
// However a more general approach would be to understand the format of the
// contained SAN fields and implement them in SubjectAlternativeNames.
$sequence = Sequence::fromBinary($octetStringBinary);
echo 'This is the parsed content of the SAN certificate extension field so far:'.PHP_EOL;
printObject($sequence);
// The following does not work yet because PHPASN1 SAn does only support DNS and IP
//SubjectAlternativeNames::fromBinary($octetStringBinary);
}
}
} catch (\Exception $exception) {
echo '[ERROR] Caught exception:'.PHP_EOL.$exception->getMessage().PHP_EOL;
}
examples/Issue29.php 0000644 00000001010 15073675044 0010341 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use FG\ASN1\ASNObject;
require_once __DIR__.'/../vendor/autoload.php';
require_once 'shared.php';
$hex = 'a02b302906092a864886f70d01090e311c301a30180603551d110411300f820d636f72766573706163652e6465';
$asn = ASNObject::fromBinary(hex2bin($hex));
printObject($asn);
examples/Issue43.php 0000644 00000006226 15073675044 0010353 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use FG\ASN1\ExplicitlyTaggedObject;
use FG\ASN1\Universal\Enumerated;
use FG\ASN1\Universal\ObjectIdentifier;
use FG\ASN1\Universal\Sequence;
require_once __DIR__.'/../vendor/autoload.php';
// $data has been generated using https://pkijs.org/examples/OCSP_resp_complex_example.html
$data = 'MIIEXgoBAKCCBFcwggRTBgkrBgEFBQcwAQEEggREMIIEQDBeoSAwHjEcMAkGA1UE
BhMCUlUwDwYDVQQDHggAVABlAHMAdBgPMjAxNTA3MTYxNzU0MjFaMCkwJzASMAcG
BSsOAwIaBAEBBAEBAgEBgAAYDzIwMTUwNzE2MTc1NDIxWjALBgkqhkiG9w0BAQUD
ggEBAG5B1xOnhgzgpsnspWd9c4eLIeOY1XXl7q2DUO2kGji4WbBXtWDMEv7QQO9/
8devmyWNTFlkScbhiMPIEfoES6AyW8lZu9aON2tMssgj/Ev9+H+A2Z2WHQdeDtv7
AS2mYa/7e3Ucsb63pAmBcxgDY55eHwmivBfZIWF6RjbzuJ0Uxz6NvrVEHlWz2KE0
1n/7qbcwbHx8nu1s/IdswFG9iePm5sTsHPsbvsMQ5ZfkvkuL/t5WlLONlRYunOA4
Nytf2lUIZbZznB0dvQG4vm1F9IQEcv1V0aU0D/ZU81I7lSPlPEv/DMTEo1vePTZv
Gv8S1K+keDD/bfwM22d663Vfmt+gggLKMIICxjCCAsIwggGsoAMCAQICAQEwCwYJ
KoZIhvcNAQEFMB4xHDAJBgNVBAYTAlJVMA8GA1UEAx4IAFQAZQBzAHQwHhcNMTMw
MjAxMDAwMDAwWhcNMTYwMjAxMDAwMDAwWjAeMRwwCQYDVQQGEwJSVTAPBgNVBAMe
CABUAGUAcwB0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsFlz/3yl
mk+8uP4/x61UWBliqW08/1jMA83Qj2yuCAvhMhmuq8BvyWP4jnXoxTy3iTZFDXIj
5Vg2FlHWna0TMsU5jlmv8W/mu9U3+q7i1+339JrEZkrDLNDpJ23gqKBgC1OaNH7+
vicBRmoP5kj2X62XMaKT5TMFBZzTtSF7IaUjt9SVXQjG3aHIy7D1mOypvEw4LSS+
RvvJTezSQeCoLpX7HziRnoVUNkJWZHL2wG5rb/SJzpXwHkRa7R250vN8TFtSSsp4
YXzK6aks/qkxRJ1UkBLUAZlRasUD+zu8gyTIz0sNgFrOtx8EOzjZRiJ/vBiVOn88
Brf4i0D+fVXmJQIDAQABow8wDTALBgNVHQ8EBAMCAAIwCwYJKoZIhvcNAQEFA4IB
AQBfubKPTvmDGrDxoqCbPwFoPRC0STwPL2GV8f5sD/Sbyc0NoJdygUO2DvquGGn5
6UJCfLo1u6Dn4zuuDs3m6if86HTpAf9Z3a72ok2Tor/NFwYt+vDOrFY5F4bXDZkf
u4zuDLmjpj26Dk4te3BVohsLTXbvJ5a/TT2VanwNOyx85lXPxy3V8Rr1AwlmHZoz
DDbUGbe/noUDJCgMjvaKKvLykIhIcW+g6W7SOcKRflw5H8kzDv816XFODSC3X1Uw
o3aVy9du/0mH+g4HvyVVplO90tdoHD1gHUMZwuen4dbTzhWv4dtLFelWM5lGWbLE
Wn7kJghclgIxv10nkGyfrowt';
// OCSP response status according to
// https://tools.ietf.org/html/rfc6960#section-4.2.1
$validResponseStatuses = array(
0 => 'Response has valid confirmations',
1 => 'Illegal confirmation request',
2 => 'Internal error in issuer',
3 => 'Try again later',
// (4) is not used
5 => 'Must sign the request',
6 => 'Request unauthorized',
);
$ocspResponse = Sequence::fromBinary(base64_decode($data));
/* @var Enumerated $responseStatus */
$elements = $ocspResponse->getChildren();
$responseStatus = $elements[0];
$responseStatusCode = $responseStatus->getContent();
echo PHP_EOL;
echo "OCSP response status: $responseStatusCode ({$validResponseStatuses[$responseStatusCode]})".PHP_EOL;
/** @var ExplicitlyTaggedObject $responseBytes */
$responseBytes = $elements[1];
/** @var Sequence $responseBytesSequence */
$responseBytesSequence = $responseBytes->getContent();
/** @var ObjectIdentifier $responseType */
$responseType = $responseBytesSequence->getChildren()[0];
echo "ResponseType: {$responseType}".PHP_EOL;
$response = $responseBytesSequence->getChildren()[1];
echo "Response (octet string): {$response}".PHP_EOL;
examples/Issue57.php 0000644 00000011165 15073675044 0010356 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use FG\ASN1\ExplicitlyTaggedObject;
use FG\ASN1\ASNObject;
use FG\ASN1\Universal\Enumerated;
use FG\ASN1\Universal\ObjectIdentifier;
use FG\ASN1\Universal\Sequence;
require_once __DIR__.'/../vendor/autoload.php';
require_once 'shared.php';
$input1 = '
MIICWDCCAhgCAQAwVzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5LmlvMQ0wCwYDVQQK
DARQeUNBMQswCQYDVQQGEwJVUzEOMAwGA1UECAwFVGV4YXMxDzANBgNVBAcMBkF1
c3RpbjCCAbYwggErBgcqhkjOOAQBMIIBHgKBgQCNf628CeKEqvppFUzqJBdwBJCe
UZ+LNdaFzeW07NyVg+dNNwoPiK2pjwJvJ3Yvs9XaeDb5ht/Ns1ieW5Jb6hFN78A+
+B2uMMJLvG3z1YjpNCe7pkID1KWxaHsrXjtkPUxhSXb4n5WjjT5MiQZfupdRTCLF
Ctu/KJFjp0tUhZs1twIVAINd5WvQfPf4LiAy/niUmu0ReqLvAoGAH3F7Wgd4L8Lk
5o4xH+qRpU7dNrhqxjTRTwWmipfq6dLvMfse895Cw9EA35ymT1vcKux7/ftHTPgx
/qBYU7XgWfLSSYCgrEY/HoGK81I+PLeaOdRfqScxiXdShCRpz4VAsBSRAk6q+85g
GOih9GWMND9Lp8CyHlN2oh9L64SRlh4DgYQAAoGABxPwdkH2Npu1qVRSdKLUwBmY
Nn+zcbueE0NjY2cu1o+CF0wt4FyOg5vG3laN1QuijY2dhxlCOq7FVX3xDXc6si1t
Zcu4eASml7yP2WW5Uvn36FDt8TyKzbXXU7bRDlngtXMuPIK6+hQDQrxKO7oWvQaB
yKai27t+/mziuEY7FwugADAJBgcqhkjOOAQDAy8AMCwCFGHVjcAo0BEIGKfYF9dC
NXJ8Ss/fAhQJe1LhmOzpXeFyc/CpJN8jzp2BiA==';
$input2 = '
MIIFGDCCAwACAQAwOjEWMBQGCgmSJomT8ixkAR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ulrP8LL97l//H2KSCvOvSfk1XeN7ADSyWMC6ZPBD/
7WVnQo7GgMAA0Qhz+nIIFA5AyuFEbRiff+L5EEl+Bg6R9tgx+QUt9JXxF260dkAb
iKBmPcJldl2/GEmz6RAXhMwJnN35tbdzw0LVLzsypr0CAwEAAaAsMBMGCSqGSIb3
DQEJAjEGDAR0ZXN0MBUGCSqGSIb3DQEJBzEIDAYxMjM0NTYwDQYJKoZIhvcNAQEF
BQADggEBACqZ5+3GP7QbTJAqnUloMRBJGYZl0/EyW6dAMoq7E+RiR2OlJGXgP+ga
DPv7Wa4jZiYmf2CZCWKP+WvTvcg4AQq5h7hhAN+u+s8kk0lIjYjnkKRAedGYKDUO
fPD9WowISru3RB1xyxlvgeZS6WoA6TD8REVa1UyFoNzUewvsrNVkKSHh1xk/+ePx
2Ovvrcg9pAWY4K8FvMRdFQKnEud9CAoMxqz3kszhxDW6rcr2mgFPSrKi5WNj+Scg
Tqod8xbB753JWjEbG6Hui9LIMihTX3ZJ0c2GB0buhEgz49y83X/byFHSGGSQpzxX
qXDFVov9UZ+sGy8CJ5ahII79yrfKpxY=
';
try {
echo 'Input 1:' . PHP_EOL;
printObject(ASNObject::fromBinary(base64_decode($input1)));
} catch (Exception $exception) {
echo "ERROR: " . $exception->getMessage();
}
echo PHP_EOL;
try {
echo 'Input 2:' . PHP_EOL;
printObject(ASNObject::fromBinary(base64_decode($input2)));
} catch (Exception $exception) {
echo "ERROR: " . $exception->getMessage();
}
echo PHP_EOL;
try {
echo 'Input 3:' . PHP_EOL;
printObject(ASNObject::fromBinary(base64_decode($input3)));
} catch (Exception $exception) {
echo "ERROR: " . $exception->getMessage();
}
examples/allClasses.php 0000644 00000017155 15073675044 0011205 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/hexdump.php';
require_once __DIR__.'/../vendor/autoload.php';
use FG\ASN1\OID;
use FG\ASN1\ASNObject;
use FG\ASN1\Universal\Boolean;
use FG\ASN1\Universal\Integer;
use FG\ASN1\Universal\Enumerated;
use FG\ASN1\Universal\PrintableString;
use FG\ASN1\Universal\NumericString;
use FG\ASN1\Universal\CharacterString;
use FG\ASN1\Universal\UTF8String;
use FG\ASN1\Universal\BitString;
use FG\ASN1\Universal\OctetString;
use FG\ASN1\Universal\ObjectIdentifier;
use FG\ASN1\Universal\NullObject;
use FG\ASN1\Universal\UTCTime;
use FG\ASN1\Universal\GeneralizedTime;
use FG\ASN1\Universal\Set;
use FG\ASN1\Universal\Sequence;
use FG\ASN1\Composite\AttributeTypeAndValue;
use FG\ASN1\Composite\RDNString;
use FG\ASN1\Composite\RelativeDistinguishedName;
use FG\X509\CertificateSubject;
use FG\X509\PublicKey;
use FG\X509\AlgorithmIdentifier;
use FG\X509\CSR\CSR;
// check if openssl is installed on this system
$openSSLVersionOutput = shell_exec('openssl version');
if (substr($openSSLVersionOutput, 0, 7) == 'OpenSSL') {
$openSSLisAvailable = true;
} else {
$openSSLisAvailable = false;
}
function printVariableInfo(ASNObject $variable)
{
$className = get_class($variable);
$stringValue = nl2br($variable->__toString());
$binaryData = $variable->getBinary();
$base64Binary = chunk_split(base64_encode($binaryData), 24);
echo '
';
echo "
{$className}
";
echo "
\"{$stringValue}\"
";
echo "
{$base64Binary}
";
echo '
'.hexdump($binaryData, true, true, true).'
';
global $openSSLisAvailable;
if ($openSSLisAvailable) {
$openSSLOutput = shell_exec("echo '{$base64Binary}' | openssl asn1parse -inform PEM -dump -i 2>&1");
echo "
{$openSSLOutput}
";
}
echo '
';
}
?>
PHPASN1 Classes
Examples for all available PHPASN1 classes
Note: OpenSSL could not be found on this system. This is absolutely no problem
for PHPASN1 but it could have been used to show you that the generated binaries are indeed correct :)
";
}
?>
Class
toString()
Binary (base64)
Binary (hex)
OpenSSL asn1parse output';
}
// Primitives
printVariableInfo(new Boolean(true));
printVariableInfo(new Boolean(false));
printVariableInfo(new Integer(123456));
printVariableInfo(new Integer(-546));
printVariableInfo(new Enumerated(1));
printVariableInfo(new PrintableString('Hello World'));
printVariableInfo(new NumericString('123456'));
printVariableInfo(new CharacterString('Foo'));
printVariableInfo(new UTF8String('Hello ♥♥♥ World'));
// there are more restricted character string classes available
printVariableInfo(new BitString('3082010a02820101009e2a7'));
printVariableInfo(new OctetString('x01020304AE123200A0'));
printVariableInfo(new ObjectIdentifier(OID::PKCS9_EMAIL));
printVariableInfo(new NullObject());
printVariableInfo(new UTCTime('2012-09-30 14:33'));
printVariableInfo(new GeneralizedTime('2008-07-01 22:35:17.024540+06:30'));
// Constructed
printVariableInfo(new Sequence(
new ObjectIdentifier(OID::COMMON_NAME),
new PrintableString('Friedrich')
));
printVariableInfo(new Set(
new ObjectIdentifier(OID::COUNTRY_NAME),
new PrintableString('Germany')
));
printVariableInfo(new AttributeTypeAndValue(
OID::ORGANIZATION_NAME,
new PrintableString('My Organization')
));
printVariableInfo(new RelativeDistinguishedName(
OID::RSA_ENCRYPTION,
new Integer(62342)
));
printVariableInfo(new RDNString(
OID::COMMON_NAME,
'Hello World'
));
// X509
printVariableInfo(new CertificateSubject(
'Friedrich Grosse',
'friedrich.grosse@gmail.com',
'PHPASN1',
'Berlin',
'Berlin',
'DE',
'Development Department'
));
printVariableInfo(new PublicKey(
'00 00 00 01 23 00 00 01 01 00 E7 40 F2 69 15 D5 4A DD 24 A1 C3
0C 93 38 03 1A 49 70 EB 76 B0 F5 1B BF 42 97 BA E1 F3 61 CC E1
63 0A BA 0F 66 6D 30 4E 31 67 65 39 E1 2E 93 A7 E7 3E 1E 76 10
81 5A 41 C6 DF 12 05 46 AC 61 A9 29 A3 BD 1A 16 35 38 DF 75 F6
0A 25 F7 3A F5 D3 C6 EB 84 7D 40 D4 1E 56 CA 0B 46 28 F7 79 D6
08 BB 1B 01 C7 2A 14 93 B2 F7 89 CB 11 F3 4B E6 5F 7A 2B 22 F4
14 CB 36 47 40 89 45 FC 14 03 1D 8B 31 AF 4C 57 63 7D 87 FD 95
0C ED 64 C1 BF 8F FC E2 76 6C 90 A3 4F 50 72 9D AB 01 1E 84 7A
63 95 9A 2E 0E 59 F1 AF A9 D0 6E AB 8E 45 96 80 AD 57 26 04 2F
B7 B0 41 69 2B 7A BD F9 2F FE 60 99 A9 81 FF 94 A5 8E 5B 7F A8
C0 63 4B F3 79 F4 F7 3C 27 81 FF 90 5C C6 04 BC C2 C1 62 2E 47
8A EF 6F 95 53 89 99 34 7C 4C B4 AE A0 E8 AD C1 04 B8 B6 E2 02
BB 65 93 39 42 F9 FD 69 20 E3 CF EF 49 E9'
));
printVariableInfo(new AlgorithmIdentifier(OID::SHA1_WITH_RSA_SIGNATURE));
printVariableInfo($csr = new CSR(
'Friedrich Grosse',
'friedrich.grosse@gmail.com',
'PHPASN1',
'Berlin',
'Berlin',
'DE',
'Development Department',
'3082010a02820101009e2a7defae93720c91c43c46ff4a1f2e8eef7949289e281f788f3a07d9b94da26fb2e721009caceddd0e6b59daa596df20f871fc30a43f4b80798f94fa3d13cb2db79eb6d8f07b4065d0b09a541564ba3baa1201e20ee923ea16be31fa785c300635c4e881df7acb5b52c7c3d923067902cc55e77c00694f319d2b9e81edbbfe70ef1a462aef4960c567f33aa5264a05fdf24cd7bc36941cd7746fb767631a241b7a97fc4cdc42a68692b906406403599380c7586ce6f22fac34949caf1072c724ba5397e6440f957e2678c3a4bc92268fe6815d41fa210ab45364c11e3731c6c039832b54f54b51fdaf6afb351e1da9720b3c322f7fbaefb72d96d4ce5ec07b0203010001',
'4adf270d347047192573cf245a94cd2e69594c1cdac1d7c99d7ed5856c926ee62c65188f21d893e634b213595cc4564d5a8d39bed0ca01e0b45e3182ab89310c129017f2a7a68d8603694ddc8d1c2ebfee39b3b5dfc9dbc2db667a089b1b51386f2cf7ec70140d185bae5c2f3b3148b9ef613ce068f94db13a230b1133e4b4a48ec5c8b4066d64a2199c0cfb6c4d0cfe105f21a89b2900d0a5c87bef5eded941ba93ae1b7e84aaeabcb46fa4a3844ffc683ebb4ee80717ff51cba5d82afe9d2633b760a66449e57e06d73eeeb151bc050a66825996d7f5ec821d31891c620a677c8271db13bbc22fcf91e1b7ac8f6f109eb8e3a2c61a3c8a4336b40a499e1404'
));
?>
examples/common.css 0000644 00000000555 15073675044 0010404 0 ustar 00 table {
border-collapse: collapse;
}
th {
text-align: left;
}
td {
border-top: black 1px solid;
padding-right: 30px;
}
td.ASNclass {
font-weight: bold;
width: 150px;
}
td.openSSL {
border-left: black 1px solid;
}
p.notice {
margin-bottom: 30px;
color: gray;
}
.red {
color: red;
}
.monospace {
font-family: monospace;
}
pre {
margin: 7px
}
examples/getObjectIdentifier.php 0000644 00000001727 15073675044 0013026 0 ustar 00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../vendor/autoload.php';
use FG\ASN1\OID;
function echoOIDRow($oidString)
{
$oidName = OID::getName($oidString);
echo "
{$oidString}
{$oidName}
";
}
?>
PHPASN1 Examples
examples/hexdump.php 0000644 00000004457 15073675044 0010572 0 ustar 00
* @author Peter Waller
*
* @link http://aidanlister.com/2004/04/viewing-binary-data-as-a-hexdump-in-php/
*
* @param string $data The string to be dumped
* @param bool $htmloutput Set to false for non-HTML output
* @param bool $uppercase Set to true for uppercase hex
* @param bool $return Set to true to return the dump
*
* @return string
*/
function hexdump($data, $htmloutput = true, $uppercase = false, $return = false)
{
// Init
$hexi = '';
$ascii = '';
$dump = ($htmloutput === true) ? '