Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
asn1.php
1<?php
2/*
3 * SimpleJWT
4 *
5 * Copyright (C) Kelvin Mo 2015
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36namespace Bitrix\Sale\PaySystem;
37
44class ASN1 {
45 const UNIVERSAL_CLASS = 0x00;
46 const APPLICATION_CLASS = 0x40;
47 const CONTEXT_CLASS = 0x80;
48 const PRIVATE_CLASS = 0xC0;
49
50 const INTEGER_TYPE = 0x02;
51 const BIT_STRING = 0x03;
52 const OCTET_STRING = 0x04;
53 const NULL_TYPE = 0x05;
54 const OID = 0x06;
55 const SEQUENCE = 0x10;
56
69 static function readDER($der, $offset, &$data, $ignore_bit_strings = FALSE) {
70 $pos = $offset;
71
72 $size = mb_strlen($der);
73
74 if ($size < 2) return 0;
75
76 // Tag/Type
77 $constructed = (ord($der[$pos]) >> 5) & 0x01;
78 $type = ord($der[$pos++]) & 0x1f;
79 if ($type == 0x1f) return 0; // Long-form type: not supported
80 if ($pos >= $size) return 0;
81
82 // Length
83 $len = ord($der[$pos++]);
84 if ($len & 0x80) {
85 $n = $len & 0x1f;
86 $len = 0;
87 while ($n-- && $pos < $size) {
88 $len = ($len << 8) | ord($der[$pos++]);
89 }
90 }
91 if ($pos >= $size || $len > $size - $pos) return 0;
92
93 // Value
94 if ($type == self::BIT_STRING) { // BIT STRING
95 $pos++; // Skip the first contents octet (padding indicator)
96 $data = mb_substr($der, $pos, $len - 1);
97 if (!$ignore_bit_strings) $pos += $len - 1;
98 } elseif (!$constructed /*&& ($type != 0x04)*/) {
99 $data = mb_substr($der, $pos, $len);
100 $pos += $len;
101 }
102
103 return $pos - $offset;
104 }
105
115 static function encodeDER($type, $value = '', $primitive = true, $class = 0) {
116 $tag_header = $class;
117 if (!$primitive) $tag_header |= 0x20;
118
119 // Type
120 if ($type < 0x1f) {
121 $der = chr($tag_header | $type);
122 } else {
123 return NULL; // Long form required. not supported.
124 }
125
126 // Length
127 $len = mb_strlen($value);
128 if ($len <= 0x7f) {
129 $der .= chr($len);
130 } else {
131 $pack = '';
132 $n = 0;
133 while ($len) {
134 $pack .= chr($len & 0xff);
135 $len >>= 8;
136 $n++;
137 }
138
139 $der .= chr($n | 0x80);
140
141 if (pack('V', 65534) == pack('L', 65534)) {
142 $der .= strrev($pack); // Little endian machine - need to convert to big endian
143 } else {
144 $der = $pack;
145 }
146 }
147
148 return $der . $value;
149 }
150
151
158 static function decodeOID($oid) {
159 $pos = 0;
160 $size = mb_strlen($oid);
161
162 // First octet
163 $oct = ord($oid[$pos++]);
164 $str = floor($oct / 40) . '.' . ($oct % 40);
165
166 // Subsequent octets
167 while ($pos < $size) {
168 $num = 0;
169
170 do {
171 $oct = ord($oid[$pos++]);
172 $num = ($num << 7) + ($oct & 0x7F);
173 } while (($oct & 0x80) && ($pos < $size));
174
175 $str .= '.' . $num;
176 }
177
178 return $str;
179 }
180
187 static function encodeOID($str) {
188 $numbers = explode('.', $str);
189
190 // First octet
191 $oid = chr(array_shift($numbers) * 40 + array_shift($numbers));
192
193 // Subsequent octets
194 foreach ($numbers as $num) {
195 if ($num == 0) {
196 $oid .= chr(0x00);
197 continue;
198 }
199 $pack = '';
200
201 while ($num) {
202 $pack .= chr(0x80 | ($num & 0x7f));
203 $num >>= 7;
204 }
205 $pack[0] = $pack[0] & chr(0x7f);
206
207 if (pack('V', 65534) == pack('L', 65534)) {
208 $oid .= strrev($pack); // Little endian machine - need to convert to big endian
209 } else {
210 $oid .= $pack;
211 }
212 }
213
214 return $oid;
215 }
216}
217
218?>
static encodeDER($type, $value='', $primitive=true, $class=0)
Definition asn1.php:115
static encodeOID($str)
Definition asn1.php:187
static readDER($der, $offset, &$data, $ignore_bit_strings=FALSE)
Definition asn1.php:69
static decodeOID($oid)
Definition asn1.php:158