ASN1   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Importance

Changes 15
Bugs 5 Features 5
Metric Value
eloc 289
c 15
b 5
f 5
dl 0
loc 321
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A OIDtoText() 0 11 6
A TextToOID() 0 7 2
A toBase256() 0 10 2
A fromBase256() 0 7 2
1
<?php
2
3
namespace vakata\asn1;
4
5
class ASN1
6
{
7
    const CLASS_UNIVERSAL        = 0;
8
    const CLASS_APPLICATION      = 1;
9
    const CLASS_CONTEXT_SPECIFIC = 2;
10
    const CLASS_PRIVATE          = 3;
11
    const TYPE_BOOLEAN           = 1;
12
    const TYPE_INTEGER           = 2;
13
    const TYPE_BIT_STRING        = 3;
14
    const TYPE_OCTET_STRING      = 4;
15
    const TYPE_NULL              = 5;
16
    const TYPE_OBJECT_IDENTIFIER = 6;
17
    const TYPE_OBJECT_DESCRIPTOR = 7;
18
    const TYPE_INSTANCE_OF       = 8; // EXTERNAL
19
    const TYPE_REAL              = 9;
20
    const TYPE_ENUMERATED        = 10;
21
    const TYPE_EMBEDDED          = 11;
22
    const TYPE_UTF8_STRING       = 12;
23
    const TYPE_RELATIVE_OID      = 13;
24
    const TYPE_SEQUENCE          = 16; // SEQUENCE OF
25
    const TYPE_SET               = 17; // SET OF
26
    const TYPE_NUMERIC_STRING    = 18;
27
    const TYPE_PRINTABLE_STRING  = 19;
28
    const TYPE_TELETEX_STRING    = 20; // T61String
29
    const TYPE_VIDEOTEX_STRING   = 21;
30
    const TYPE_IA5_STRING        = 22;
31
    const TYPE_UTC_TIME          = 23;
32
    const TYPE_GENERALIZED_TIME  = 24;
33
    const TYPE_GRAPHIC_STRING    = 25;
34
    const TYPE_VISIBLE_STRING    = 26; // ISO646String
35
    const TYPE_GENERAL_STRING    = 27;
36
    const TYPE_UNIVERSAL_STRING  = 28;
37
    const TYPE_CHARACTER_STRING  = 29;
38
    const TYPE_BMP_STRING        = 30;
39
    const TYPE_CHOICE            = -1;
40
    const TYPE_ANY               = -2;
41
    const TYPE_ANY_RAW           = -3;
42
    const TYPE_ANY_SKIP          = -4;
43
    const TYPE_ANY_DER           = -5;
44
45
    /**
46
     * Convert a number to base256
47
     * @param  integer|string    $number the number to convert
48
     * @param  integer           $base   the current base of the number (optional, defaults to 10)
49
     * @return string            the number in base256
50
     */
51
    public static function toBase256($number, $base = 10)
52
    {
53
        $bin = base_convert($number, $base, 2);
54
        $res = "";
55
        $len = (int)ceil(strlen($bin) / 8) * 8;
56
        $bin = str_pad($bin, $len, "0", STR_PAD_LEFT);
57
        for ($i = ($len-8); $i >= 0; $i -= 8) {
58
            $res = chr((int)base_convert(substr($bin, $i, 8), 2, 10)) . $res;
59
        }
60
        return $res;
61
    }
62
    /**
63
     * Convert a number from base256
64
     * @param  string      $string the number to convert
65
     * @return integer|string      the converted number
66
     */
67
    public static function fromBase256($string)
68
    {
69
        $number = "";
70
        for ($i = 0; $i < strlen($string); $i++) {
71
            $number .= str_pad(base_convert(ord($string[$i]), 10, 2), 8, "0", STR_PAD_LEFT);
72
        }
73
        return $number;
74
    }
75
76
    public static $oids = [
77
        'sha1' =>                 '1.3.14.3.2.26',
78
        'sha256' =>               '2.16.840.1.101.3.4.2.1',
79
        'sha384' =>               '2.16.840.1.101.3.4.2.2',
80
        'sha512' =>               '2.16.840.1.101.3.4.2.3',
81
        'sha224' =>               '2.16.840.1.101.3.4.2.4',
82
        'md5' =>                  '1.2.840.113549.2.5',
83
        'md2' =>                  '1.3.14.7.2.2.1',
84
        'ripemd160' =>            '1.3.36.3.2.1',
85
        'MD4withRSA' =>           '1.2.840.113549.1.1.3',
86
        'SHA1withECDSA' =>        '1.2.840.10045.4.1',
87
        'SHA224withECDSA' =>      '1.2.840.10045.4.3.1',
88
        'SHA256withECDSA' =>      '1.2.840.10045.4.3.2',
89
        'SHA384withECDSA' =>      '1.2.840.10045.4.3.3',
90
        'SHA512withECDSA' =>      '1.2.840.10045.4.3.4',
91
        'dsa' =>                  '1.2.840.10040.4.1',
92
        'SHA1withDSA' =>          '1.2.840.10040.4.3',
93
        'SHA224withDSA' =>        '2.16.840.1.101.3.4.3.1',
94
        'SHA256withDSA' =>        '2.16.840.1.101.3.4.3.2',
95
        'rsaEncryption' =>        '1.2.840.113549.1.1.1',
96
        'countryName' =>          '2.5.4.6',
97
        'organization' =>         '2.5.4.10',
98
        'organizationalUnit' =>   '2.5.4.11',
99
        'stateOrProvinceName' =>  '2.5.4.8',
100
        'locality' =>             '2.5.4.7',
101
        'commonName' =>           '2.5.4.3',
102
        'subjectKeyIdentifier' => '2.5.29.14',
103
        'keyUsage' =>             '2.5.29.15',
104
        'subjectAltName' =>       '2.5.29.17',
105
        'basicConstraints' =>     '2.5.29.19',
106
        'nameConstraints' =>      '2.5.29.30',
107
        'cRLDistributionPoints' =>'2.5.29.31',
108
        'certificatePolicies' =>  '2.5.29.32',
109
        'authorityKeyIdentifier'=>'2.5.29.35',
110
        'policyConstraints' =>    '2.5.29.36',
111
        'extKeyUsage' =>          '2.5.29.37',
112
        'authorityInfoAccess' =>  '1.3.6.1.5.5.7.1.1',
113
        'anyExtendedKeyUsage' =>  '2.5.29.37.0',
114
        'serverAuth' =>           '1.3.6.1.5.5.7.3.1',
115
        'clientAuth' =>           '1.3.6.1.5.5.7.3.2',
116
        'codeSigning' =>          '1.3.6.1.5.5.7.3.3',
117
        'emailProtection' =>      '1.3.6.1.5.5.7.3.4',
118
        'timeStamping' =>         '1.3.6.1.5.5.7.3.8',
119
        'ocspSigning' =>          '1.3.6.1.5.5.7.3.9',
120
        'ecPublicKey' =>          '1.2.840.10045.2.1',
121
        'secp256r1' =>            '1.2.840.10045.3.1.7',
122
        'secp256k1' =>            '1.3.132.0.10',
123
        'secp384r1' =>            '1.3.132.0.34',
124
        'pkcs5PBES2' =>           '1.2.840.113549.1.5.13',
125
        'pkcs5PBKDF2' =>          '1.2.840.113549.1.5.12',
126
        'des-EDE3-CBC' =>         '1.2.840.113549.3.7',
127
        'data' =>                 '1.2.840.113549.1.7.1', // CMS data
128
        'signed-data' =>          '1.2.840.113549.1.7.2', // CMS signed-data
129
        'enveloped-data' =>       '1.2.840.113549.1.7.3', // CMS enveloped-data
130
        'digested-data' =>        '1.2.840.113549.1.7.5', // CMS digested-data
131
        'encrypted-data' =>       '1.2.840.113549.1.7.6', // CMS encrypted-data
132
        'authenticated-data' =>   '1.2.840.113549.1.9.16.1.2', // CMS authenticated-data
133
        'tstinfo' =>              '1.2.840.113549.1.9.16.1.4', // RFC3161 TSTInfo,
134
        'pkix' => '1.3.6.1.5.5.7',
135
        'pe' => '1.3.6.1.5.5.7.1',
136
        'qt' => '1.3.6.1.5.5.7.2',
137
        'kp' => '1.3.6.1.5.5.7.3',
138
        'ad' => '1.3.6.1.5.5.7.48',
139
        'cps' => '1.3.6.1.5.5.7.2.1',
140
        'unotice' => '1.3.6.1.5.5.7.2.2',
141
        'ocsp' =>'1.3.6.1.5.5.7.48.1',
142
        'caIssuers' => '1.3.6.1.5.5.7.48.2',
143
        'timeStamping' => '1.3.6.1.5.5.7.48.3',
144
        'caRepository' => '1.3.6.1.5.5.7.48.5',
145
        'at' => '2.5.4',
146
        'name' => '2.5.4.41',
147
        'surname' => '2.5.4.4',
148
        'givenName' => '2.5.4.42',
149
        'initials' => '2.5.4.43',
150
        'generationQualifier' => '2.5.4.44',
151
        'commonName' => '2.5.4.3',
152
        'localityName' => '2.5.4.7',
153
        'stateOrProvinceName' => '2.5.4.8',
154
        'organizationName' => '2.5.4.10',
155
        'organizationalUnitName' => '2.5.4.11',
156
        'title' => '2.5.4.12',
157
        'description' => '2.5.4.13',
158
        'dnQualifier' => '2.5.4.46',
159
        'countryName' => '2.5.4.6',
160
        'serialNumber' => '2.5.4.5',
161
        'pseudonym' => '2.5.4.65',
162
        'postalCode' => '2.5.4.17',
163
        'streetAddress' => '2.5.4.9',
164
        'uniqueIdentifier' => '2.5.4.45',
165
        'role' => '2.5.4.72',
166
        'postalAddress' => '2.5.4.16',
167
        'domainComponent' => '0.9.2342.19200300.100.1.25',
168
        'pkcs-9' => '1.2.840.113549.1.9',
169
        'emailAddress' => '1.2.840.113549.1.9.1',
170
        'ce' => '2.5.29',
171
        'authorityKeyIdentifier' => '2.5.29.35',
172
        'subjectKeyIdentifier' => '2.5.29.14',
173
        'keyUsage' => '2.5.29.15',
174
        'privateKeyUsagePeriod' => '2.5.29.16',
175
        'certificatePolicies' => '2.5.29.32',
176
        'anyPolicy' => '2.5.29.32.0',
177
        'policyMappings' => '2.5.29.33',
178
        'subjectAltName' => '2.5.29.17',
179
        'issuerAltName' => '2.5.29.18',
180
        'subjectDirectoryAttributes' => '2.5.29.9',
181
        'basicConstraints' => '2.5.29.19',
182
        'nameConstraints' => '2.5.29.30',
183
        'policyConstraints' => '2.5.29.36',
184
        'cRLDistributionPoints' => '2.5.29.31',
185
        'extKeyUsage' => '2.5.29.37',
186
        'anyExtendedKeyUsage' => '2.5.29.37.0',
187
        'kp-serverAuth' => '1.3.6.1.5.5.7.3.1',
188
        'kp-clientAuth' => '1.3.6.1.5.5.7.3.2',
189
        'kp-codeSigning' => '1.3.6.1.5.5.7.3.3',
190
        'kp-emailProtection' => '1.3.6.1.5.5.7.3.4',
191
        'kp-timeStamping' => '1.3.6.1.5.5.7.3.8',
192
        'kp-OCSPSigning' => '1.3.6.1.5.5.7.3.9',
193
        'inhibitAnyPolicy' => '2.5.29.54',
194
        'freshestCRL' => '2.5.29.46',
195
        'pe-authorityInfoAccess' => '1.3.6.1.5.5.7.1.1',
196
        'pe-subjectInfoAccess' => '1.3.6.1.5.5.7.1.11',
197
        'cRLNumber' => '2.5.29.20',
198
        'issuingDistributionPoint' => '2.5.29.28',
199
        'deltaCRLIndicator' => '2.5.29.27',
200
        'cRLReasons' => '2.5.29.21',
201
        'certificateIssuer' => '2.5.29.29',
202
        'holdInstructionCode' => '2.5.29.23',
203
        'holdInstruction' => '1.2.840.10040.2',
204
        'holdinstruction-none' => '1.2.840.10040.2.1',
205
        'holdinstruction-callissuer' => '1.2.840.10040.2.2',
206
        'holdinstruction-reject' => '1.2.840.10040.2.3',
207
        'invalidityDate' => '2.5.29.24',
208
        'md2' => '1.2.840.113549.2.2',
209
        'md5' => '1.2.840.113549.2.5',
210
        'sha1' => '1.3.14.3.2.26',
211
        'dsa' => '1.2.840.10040.4.1',
212
        'dsa-with-sha1' => '1.2.840.10040.4.3',
213
        'pkcs-1' => '1.2.840.113549.1.1',
214
        'rsaEncryption' => '1.2.840.113549.1.1.1',
215
        'md2WithRSAEncryption' => '1.2.840.113549.1.1.2',
216
        'md5WithRSAEncryption' => '1.2.840.113549.1.1.4',
217
        'sha1WithRSAEncryption' => ['1.2.840.113549.1.1.5', '1.3.14.3.2.29'],
218
        'dhpublicnumber' => '1.2.840.10046.2.1',
219
        'keyExchangeAlgorithm' => '2.16.840.1.101.2.1.1.22',
220
        'ansi-X9-62' => '1.2.840.10045',
221
        'ecSigType' => '1.2.840.10045.4',
222
        'ecdsa-with-SHA1' => '1.2.840.10045.4.1',
223
        'fieldType' => '1.2.840.10045.1',
224
        'prime-field' => '1.2.840.10045.1.1',
225
        'characteristic-two-field' => '1.2.840.10045.1.2',
226
        'characteristic-two-basis' => '1.2.840.10045.1.2.3',
227
        'gnBasis' => '1.2.840.10045.1.2.3.1',
228
        'tpBasis' => '1.2.840.10045.1.2.3.2',
229
        'ppBasis' => '1.2.840.10045.1.2.3.3',
230
        'publicKeyType' => '1.2.840.10045.2',
231
        'ecPublicKey' => '1.2.840.10045.2.1',
232
        'ellipticCurve' => '1.2.840.10045.3',
233
        'c-TwoCurve' => '1.2.840.10045.3.0',
234
        'c2pnb163v1' => '1.2.840.10045.3.0.1',
235
        'c2pnb163v2' => '1.2.840.10045.3.0.2',
236
        'c2pnb163v3' => '1.2.840.10045.3.0.3',
237
        'c2pnb176w1' => '1.2.840.10045.3.0.4',
238
        'c2pnb191v1' => '1.2.840.10045.3.0.5',
239
        'c2pnb191v2' => '1.2.840.10045.3.0.6',
240
        'c2pnb191v3' => '1.2.840.10045.3.0.7',
241
        'c2pnb191v4' => '1.2.840.10045.3.0.8',
242
        'c2pnb191v5' => '1.2.840.10045.3.0.9',
243
        'c2pnb208w1' => '1.2.840.10045.3.0.10',
244
        'c2pnb239v1' => '1.2.840.10045.3.0.11',
245
        'c2pnb239v2' => '1.2.840.10045.3.0.12',
246
        'c2pnb239v3' => '1.2.840.10045.3.0.13',
247
        'c2pnb239v4' => '1.2.840.10045.3.0.14',
248
        'c2pnb239v5' => '1.2.840.10045.3.0.15',
249
        'c2pnb272w1' => '1.2.840.10045.3.0.16',
250
        'c2pnb304w1' => '1.2.840.10045.3.0.17',
251
        'c2pnb359v1' => '1.2.840.10045.3.0.18',
252
        'c2pnb368w1' => '1.2.840.10045.3.0.19',
253
        'c2pnb431r1' => '1.2.840.10045.3.0.20',
254
        'primeCurve' => '1.2.840.10045.3.1',
255
        'prime192v1' => '1.2.840.10045.3.1.1',
256
        'prime192v2' => '1.2.840.10045.3.1.2',
257
        'prime192v3' => '1.2.840.10045.3.1.3',
258
        'prime239v1' => '1.2.840.10045.3.1.4',
259
        'prime239v2' => '1.2.840.10045.3.1.5',
260
        'prime239v3' => '1.2.840.10045.3.1.6',
261
        'prime256v1' => '1.2.840.10045.3.1.7',
262
        'RSAES-OAEP' => '1.2.840.113549.1.1.7',
263
        'pSpecified' => '1.2.840.113549.1.1.9',
264
        'RSASSA-PSS' => '1.2.840.113549.1.1.10',
265
        'mgf1' => '1.2.840.113549.1.1.8',
266
        'sha224WithRSAEncryption' => '1.2.840.113549.1.1.14',
267
        'sha256WithRSAEncryption' => '1.2.840.113549.1.1.11',
268
        'sha384WithRSAEncryption' => '1.2.840.113549.1.1.12',
269
        'sha512WithRSAEncryption' => '1.2.840.113549.1.1.13',
270
        'sha224' => '2.16.840.1.101.3.4.2.4',
271
        'sha256' => '2.16.840.1.101.3.4.2.1',
272
        'sha384' => '2.16.840.1.101.3.4.2.2',
273
        'sha512' => '2.16.840.1.101.3.4.2.3',
274
        'GostR3411-94-with-GostR3410-94' => '1.2.643.2.2.4',
275
        'GostR3411-94-with-GostR3410-2001' => '1.2.643.2.2.3',
276
        'GostR3410-2001' => '1.2.643.2.2.20',
277
        'GostR3410-94' => '1.2.643.2.2.19',
278
        'netscape' => '2.16.840.1.113730',
279
        'netscape-cert-extension' => '2.16.840.1.113730.1',
280
        'netscape-cert-type' => '2.16.840.1.113730.1.1',
281
        'netscape-comment' => '2.16.840.1.113730.1.13',
282
        'netscape-ca-policy-url' => '2.16.840.1.113730.1.8',
283
        'logotype' => '1.3.6.1.5.5.7.1.12',
284
        'entrustVersInfo' => '1.2.840.113533.7.65.0',
285
        'verisignPrivate' => '2.16.840.1.113733.1.6.9',
286
        'unstructuredName' => '1.2.840.113549.1.9.2',
287
        'challengePassword' => '1.2.840.113549.1.9.7',
288
        'extensionRequest' => '1.2.840.113549.1.9.14',
289
        'userid' => '0.9.2342.19200300.100.1.1',
290
        's/mime' => '1.2.840.113549.1.9.15',
291
        'unstructuredAddress' => '1.2.840.113549.1.9.8',
292
        'rc2-cbc' => '1.2.840.113549.3.2',
293
        'rc4' => '1.2.840.113549.3.4',
294
        'desCBC' => '1.3.14.3.2.7',
295
        'qcStatements' => '1.3.6.1.5.5.7.1.3',
296
        'pkixQCSyntax-v1' => '1.3.6.1.5.5.7.11.1',
297
        'pkixQCSyntax-v2' => '1.3.6.1.5.5.7.11.2',
298
        'ipsecEndSystem' => '1.3.6.1.5.5.7.3.5',
299
        'ipsecTunnel' => '1.3.6.1.5.5.7.3.6',
300
        'ipsecUser' => '1.3.6.1.5.5.7.3.7',
301
        'OCSP' => '1.3.6.1.5.5.7.48.1',
302
        'countryOfCitizenship' => '1.3.6.1.5.5.7.9.4',
303
        'IPSECProtection' => '1.3.6.1.5.5.8.2.2',
304
        'telephoneNumber' => '2.5.4.20',
305
        'organizationIdentifier' => '2.5.4.97'
306
    ];
307
    public static function OIDtoText($id)
308
    {
309
        foreach (static::$oids as $k => $v) {
310
            if (is_array($v) && in_array($id, $v)) {
311
                return $k;
312
            }
313
            if (!is_array($v) && $id === $v) {
314
                return $k;
315
            }
316
        }
317
        return $id;
318
    }
319
    public static function TextToOID($text)
320
    {
321
        $res = static::$oids[$text] ?? null;
322
        if (is_array($res)) {
323
            $res = $res[0];
324
        }
325
        return $res ?? $text;
326
    }
327
}
328