aboutsummaryrefslogtreecommitdiffstats
path: root/library/ASNValue.class.php
blob: 7a3c5d20ef12bbba3f012680be997f260eb538e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
//-----------------------------------------------------------------------------
// ASNValue class by A.Oliinyk
// contact@pumka.net
//-----------------------------------------------------------------------------
class ASNValue
{
    const TAG_INTEGER   = 0x02;
    const TAG_BITSTRING = 0x03;
    const TAG_SEQUENCE  = 0x30;
    
    public $Tag;
    public $Value;
    
    function __construct($Tag=0x00, $Value='')
    {
        $this->Tag = $Tag;
        $this->Value = $Value;
    }
    
    function Encode()
    {   
        //Write type
        $result = chr($this->Tag);

        //Write size
        $size = strlen($this->Value);
        if ($size < 127) {
            //Write size as is
            $result .= chr($size);
        }
        else {
            //Prepare length sequence
            $sizeBuf = self::IntToBin($size);

            //Write length sequence
            $firstByte = 0x80 + strlen($sizeBuf);
            $result .= chr($firstByte) . $sizeBuf;
        }

        //Write value
        $result .= $this->Value;
        
        return $result;
    }
    
    function Decode(&$Buffer)
    {   
        //Read type
        $this->Tag = self::ReadByte($Buffer);

        //Read first byte
        $firstByte = self::ReadByte($Buffer);  

        if ($firstByte < 127) {
            $size = $firstByte;
        }
        else if ($firstByte > 127) {
            $sizeLen = $firstByte - 0x80;
            //Read length sequence
            $size = self::BinToInt(self::ReadBytes($Buffer, $sizeLen));
        }
        else {
            throw new Exception("Invalid ASN length value");
        }

        $this->Value = self::ReadBytes($Buffer, $size);
    }
    
    protected static function ReadBytes(&$Buffer, $Length)
    {
        $result = substr($Buffer, 0, $Length);
        $Buffer = substr($Buffer, $Length);
        
        return $result;
    }
    
    protected static function ReadByte(&$Buffer)
    {      
        return ord(self::ReadBytes($Buffer, 1));
    }
    
    protected static function BinToInt($Bin)
    {    
        $len = strlen($Bin);
        $result = 0;
        for ($i=0; $i<$len; $i++) {
            $curByte = self::ReadByte($Bin);
            $result += $curByte << (($len-$i-1)*8);
        }
        
        return $result;
    }
    
    protected static function IntToBin($Int)
    {
        $result = '';
        do {
            $curByte = $Int % 256;
            $result .= chr($curByte);

            $Int = ($Int - $curByte) / 256;
        } while ($Int > 0);

        $result = strrev($result);
        
        return $result;
    }
    
    function SetIntBuffer($Value)
    {
        if (strlen($Value) > 1) {
            $firstByte = ord($Value{0});
            if ($firstByte & 0x80) { //first bit set
                $Value = chr(0x00) . $Value;
            }
        }
        
        $this->Value = $Value;
    }
    
    function GetIntBuffer()    
    {        
        $result = $this->Value;
        if (ord($result{0}) == 0x00) {
            $result = substr($result, 1);
        }
        
        return $result;
    }
    
    function SetInt($Value)
    {
        $Value = self::IntToBin($Value);
        
        $this->SetIntBuffer($Value);
    }   
    
    function GetInt()
    {
        $result = $this->GetIntBuffer();
        $result = self::BinToInt($result);
        
        return $result;
    }
    
    function SetSequence($Values)
    {
        $result = '';
        foreach ($Values as $item) {
            $result .= $item->Encode();            
        }   
        
        $this->Value = $result;
    }   
    
    function GetSequence()
    {
        $result = array();
        $seq = $this->Value;
        while (strlen($seq)) {
            $val = new ASNValue();
            $val->Decode($seq);
            $result[] = $val;
        }  
        
        return $result;
    }    
}