Source for file ASN1.php
Documentation is available at ASN1.php
* @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
* This file is part of phpPoA2.
* phpPoA2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* phpPoA2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
* @author Jaime Perez <jaime.perez@rediris.es>
* Mostly based on code by A. Oliinyk.
define("TAG_BITSTRING", 0x03);
* This class implements ASN.1 encoding.
* Please, be aware that this class is INCOMPLETE, as it
* is intended for BER/DER encoding primarily.
* Build a new ASN1 from its tag and value.
public function __construct($tag = 0x00, $value = '') {
* Encode this object into ASN.1.
* Write the length of the contents.
* Refer to ITU-T X.609 section 8.1.3 for details on how length must be encoded.
if ($len < 127) { // store in one byte
} else { // store the length in separate bytes
// write the bytes with real length
// finally write the value
$encoded .= $this->value;
* Decode an object in ASN.1 format.
public function decode(&$buffer) {
// get the length of de data
* Refer to ITU-T X.609 section 8.1.3 for details on how length must be encoded.
// short length, stored in one byte
} else if ($byte === 128) {
// indefinite length, read until two zero bytes are found
} else if ($byte < 255) {
// long length, from 1 up to 127 next bytes
// 255 found, reserved value
throw new Exception("Long length of 0x7f cannot be used. Reserved value.");
// read until two zero bytes are found
if ($len > 1 && $this->value{$len - 2} === 0 && $this->value{$len - 1} === 0) {
// end-of-contents octets found, trim them
* Get the tag of this ASN.1.
* Get the binary value of this ASN.1.
// if the first byte is null, remove it
* Set the binary value of this ASN.1.
* It is mandatory in ASN.1 to precede data by a null byte
* if the first bit of the data is set.
$value = chr(0x00). $value;
* Get the integer value of this ASN.1.
* Set the value of this ASN.1 from an integer.
* Get the sequence of values stored in this ASN.1.
* Set the value of this ASN.1 to be a sequence of values.
foreach ($values as $value) {
* Read n bytes from a buffer and mov the internal pointer.
protected function readBytes(&$buffer, $length) {
$result = substr($buffer, 0, $length);
$buffer = substr($buffer, $length);
* Read first byte from the buffer and move the internal pointer.
* Write bytes to a buffer.
for ($i = 0; $i < strlen($bytes); $i++ ) {
* Write a byte to a buffer.
protected function writeByte(&$buffer, $byte) {
* Decode an integer from its binary representation.
for ($i = 0; $i < $len; $i++ ) {
$result += $byte << (($len - $i - 1) * 8);
* Encode an integer in binary representation.
$result = chr($byte). $result;
$int = ($int - $byte) / 256;
|