@@ -12,20 +12,20 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class ContextSpecificTaggedType extends TaggedType |
14 | 14 | { |
15 | - /** |
|
16 | - * Wrapped element. |
|
17 | - * |
|
18 | - * @var \ASN1\Element $_element |
|
19 | - */ |
|
20 | - protected $_element; |
|
15 | + /** |
|
16 | + * Wrapped element. |
|
17 | + * |
|
18 | + * @var \ASN1\Element $_element |
|
19 | + */ |
|
20 | + protected $_element; |
|
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @see \ASN1\Element::typeClass() |
|
25 | - * @return int |
|
26 | - */ |
|
27 | - public function typeClass(): int |
|
28 | - { |
|
29 | - return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @see \ASN1\Element::typeClass() |
|
25 | + * @return int |
|
26 | + */ |
|
27 | + public function typeClass(): int |
|
28 | + { |
|
29 | + return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | + } |
|
31 | 31 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | interface ExplicitTagging extends ElementBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Get explicitly tagged wrapped element. |
|
17 | - * |
|
18 | - * NOTE! Expectation checking is deprecated and shall be done |
|
19 | - * with UnspecifiedType. |
|
20 | - * |
|
21 | - * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * @return \ASN1\Type\UnspecifiedType |
|
24 | - */ |
|
25 | - public function explicit($expectedTag = null): UnspecifiedType; |
|
15 | + /** |
|
16 | + * Get explicitly tagged wrapped element. |
|
17 | + * |
|
18 | + * NOTE! Expectation checking is deprecated and shall be done |
|
19 | + * with UnspecifiedType. |
|
20 | + * |
|
21 | + * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * @return \ASN1\Type\UnspecifiedType |
|
24 | + */ |
|
25 | + public function explicit($expectedTag = null): UnspecifiedType; |
|
26 | 26 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | trait PrimitiveType |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * @see \ASN1\Element::isConstructed() |
|
15 | - * @return boolean |
|
16 | - */ |
|
17 | - public function isConstructed(): bool |
|
18 | - { |
|
19 | - return false; |
|
20 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * @see \ASN1\Element::isConstructed() |
|
15 | + * @return boolean |
|
16 | + */ |
|
17 | + public function isConstructed(): bool |
|
18 | + { |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | } |
@@ -11,130 +11,130 @@ |
||
11 | 11 | */ |
12 | 12 | class Flags |
13 | 13 | { |
14 | - /** |
|
15 | - * Flag octets. |
|
16 | - * |
|
17 | - * @var string $_flags |
|
18 | - */ |
|
19 | - protected $_flags; |
|
14 | + /** |
|
15 | + * Flag octets. |
|
16 | + * |
|
17 | + * @var string $_flags |
|
18 | + */ |
|
19 | + protected $_flags; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number of flags. |
|
23 | - * |
|
24 | - * @var int $_width |
|
25 | - */ |
|
26 | - protected $_width; |
|
21 | + /** |
|
22 | + * Number of flags. |
|
23 | + * |
|
24 | + * @var int $_width |
|
25 | + */ |
|
26 | + protected $_width; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param number $flags Flags |
|
32 | - * @param int $width The number of flags. If width is larger than number of |
|
33 | - * bits in $flags, zeroes are prepended to flag field. |
|
34 | - */ |
|
35 | - public function __construct($flags, int $width) |
|
36 | - { |
|
37 | - if (!$width) { |
|
38 | - $this->_flags = ""; |
|
39 | - } else { |
|
40 | - // calculate number of unused bits in last octet |
|
41 | - $last_octet_bits = $width % 8; |
|
42 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | - $num = gmp_init($flags); |
|
44 | - // mask bits outside bitfield width |
|
45 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | - $num &= $mask; |
|
47 | - // shift towards MSB if needed |
|
48 | - $data = gmp_export($num << $unused_bits, 1, |
|
49 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | - $octets = unpack("C*", $data); |
|
51 | - $bits = count($octets) * 8; |
|
52 | - // pad with zeroes |
|
53 | - while ($bits < $width) { |
|
54 | - array_unshift($octets, 0); |
|
55 | - $bits += 8; |
|
56 | - } |
|
57 | - $this->_flags = pack("C*", ...$octets); |
|
58 | - } |
|
59 | - $this->_width = $width; |
|
60 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param number $flags Flags |
|
32 | + * @param int $width The number of flags. If width is larger than number of |
|
33 | + * bits in $flags, zeroes are prepended to flag field. |
|
34 | + */ |
|
35 | + public function __construct($flags, int $width) |
|
36 | + { |
|
37 | + if (!$width) { |
|
38 | + $this->_flags = ""; |
|
39 | + } else { |
|
40 | + // calculate number of unused bits in last octet |
|
41 | + $last_octet_bits = $width % 8; |
|
42 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | + $num = gmp_init($flags); |
|
44 | + // mask bits outside bitfield width |
|
45 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | + $num &= $mask; |
|
47 | + // shift towards MSB if needed |
|
48 | + $data = gmp_export($num << $unused_bits, 1, |
|
49 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | + $octets = unpack("C*", $data); |
|
51 | + $bits = count($octets) * 8; |
|
52 | + // pad with zeroes |
|
53 | + while ($bits < $width) { |
|
54 | + array_unshift($octets, 0); |
|
55 | + $bits += 8; |
|
56 | + } |
|
57 | + $this->_flags = pack("C*", ...$octets); |
|
58 | + } |
|
59 | + $this->_width = $width; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Initialize from BitString. |
|
64 | - * |
|
65 | - * @param BitString $bs |
|
66 | - * @param int $width |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromBitString(BitString $bs, int $width): self |
|
70 | - { |
|
71 | - $num_bits = $bs->numBits(); |
|
72 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | - $num >>= $bs->unusedBits(); |
|
74 | - if ($num_bits < $width) { |
|
75 | - $num <<= ($width - $num_bits); |
|
76 | - } |
|
77 | - return new self(gmp_strval($num, 10), $width); |
|
78 | - } |
|
62 | + /** |
|
63 | + * Initialize from BitString. |
|
64 | + * |
|
65 | + * @param BitString $bs |
|
66 | + * @param int $width |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromBitString(BitString $bs, int $width): self |
|
70 | + { |
|
71 | + $num_bits = $bs->numBits(); |
|
72 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | + $num >>= $bs->unusedBits(); |
|
74 | + if ($num_bits < $width) { |
|
75 | + $num <<= ($width - $num_bits); |
|
76 | + } |
|
77 | + return new self(gmp_strval($num, 10), $width); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check whether a bit at given index is set. |
|
82 | - * Index 0 is the leftmost bit. |
|
83 | - * |
|
84 | - * @param int $idx |
|
85 | - * @throws \OutOfBoundsException |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - public function test(int $idx): bool |
|
89 | - { |
|
90 | - if ($idx >= $this->_width) { |
|
91 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | - } |
|
93 | - // octet index |
|
94 | - $oi = (int) floor($idx / 8); |
|
95 | - $byte = $this->_flags[$oi]; |
|
96 | - // bit index |
|
97 | - $bi = $idx % 8; |
|
98 | - // index 0 is the most significant bit in byte |
|
99 | - $mask = 0x01 << (7 - $bi); |
|
100 | - return (ord($byte) & $mask) > 0; |
|
101 | - } |
|
80 | + /** |
|
81 | + * Check whether a bit at given index is set. |
|
82 | + * Index 0 is the leftmost bit. |
|
83 | + * |
|
84 | + * @param int $idx |
|
85 | + * @throws \OutOfBoundsException |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + public function test(int $idx): bool |
|
89 | + { |
|
90 | + if ($idx >= $this->_width) { |
|
91 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | + } |
|
93 | + // octet index |
|
94 | + $oi = (int) floor($idx / 8); |
|
95 | + $byte = $this->_flags[$oi]; |
|
96 | + // bit index |
|
97 | + $bi = $idx % 8; |
|
98 | + // index 0 is the most significant bit in byte |
|
99 | + $mask = 0x01 << (7 - $bi); |
|
100 | + return (ord($byte) & $mask) > 0; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get flags as an octet string. |
|
105 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function string(): string |
|
110 | - { |
|
111 | - return $this->_flags; |
|
112 | - } |
|
103 | + /** |
|
104 | + * Get flags as an octet string. |
|
105 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function string(): string |
|
110 | + { |
|
111 | + return $this->_flags; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Get flags as a base 10 integer. |
|
116 | - * |
|
117 | - * @return string Integer as a string |
|
118 | - */ |
|
119 | - public function number(): string |
|
120 | - { |
|
121 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | - $last_octet_bits = $this->_width % 8; |
|
123 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | - $num >>= $unused_bits; |
|
125 | - return gmp_strval($num, 10); |
|
126 | - } |
|
114 | + /** |
|
115 | + * Get flags as a base 10 integer. |
|
116 | + * |
|
117 | + * @return string Integer as a string |
|
118 | + */ |
|
119 | + public function number(): string |
|
120 | + { |
|
121 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | + $last_octet_bits = $this->_width % 8; |
|
123 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | + $num >>= $unused_bits; |
|
125 | + return gmp_strval($num, 10); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get flags as a BitString. |
|
130 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
131 | - * |
|
132 | - * @return BitString |
|
133 | - */ |
|
134 | - public function bitString(): BitString |
|
135 | - { |
|
136 | - $last_octet_bits = $this->_width % 8; |
|
137 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
138 | - return new BitString($this->_flags, $unused_bits); |
|
139 | - } |
|
128 | + /** |
|
129 | + * Get flags as a BitString. |
|
130 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
131 | + * |
|
132 | + * @return BitString |
|
133 | + */ |
|
134 | + public function bitString(): BitString |
|
135 | + { |
|
136 | + $last_octet_bits = $this->_width % 8; |
|
137 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
138 | + return new BitString($this->_flags, $unused_bits); |
|
139 | + } |
|
140 | 140 | } |
@@ -12,79 +12,79 @@ |
||
12 | 12 | */ |
13 | 13 | interface ElementBase extends Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Get the class of the ASN.1 type. |
|
17 | - * |
|
18 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | - * |
|
20 | - * @return int |
|
21 | - */ |
|
22 | - public function typeClass(): int; |
|
15 | + /** |
|
16 | + * Get the class of the ASN.1 type. |
|
17 | + * |
|
18 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | + * |
|
20 | + * @return int |
|
21 | + */ |
|
22 | + public function typeClass(): int; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Check whether the element is constructed. |
|
26 | - * |
|
27 | - * Otherwise it's primitive. |
|
28 | - * |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isConstructed(): bool; |
|
24 | + /** |
|
25 | + * Check whether the element is constructed. |
|
26 | + * |
|
27 | + * Otherwise it's primitive. |
|
28 | + * |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isConstructed(): bool; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get the tag of the element. |
|
35 | - * |
|
36 | - * Interpretation of the tag depends on the context. For example it may |
|
37 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | - * tagged type. |
|
39 | - * |
|
40 | - * @return int |
|
41 | - */ |
|
42 | - public function tag(): int; |
|
33 | + /** |
|
34 | + * Get the tag of the element. |
|
35 | + * |
|
36 | + * Interpretation of the tag depends on the context. For example it may |
|
37 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | + * tagged type. |
|
39 | + * |
|
40 | + * @return int |
|
41 | + */ |
|
42 | + public function tag(): int; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Check whether the element is a type of a given tag. |
|
46 | - * |
|
47 | - * @param int $tag Type tag |
|
48 | - * @return boolean |
|
49 | - */ |
|
50 | - public function isType($tag): bool; |
|
44 | + /** |
|
45 | + * Check whether the element is a type of a given tag. |
|
46 | + * |
|
47 | + * @param int $tag Type tag |
|
48 | + * @return boolean |
|
49 | + */ |
|
50 | + public function isType($tag): bool; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Check whether the element is a type of a given tag. |
|
54 | - * |
|
55 | - * Throws an exception if expectation fails. |
|
56 | - * |
|
57 | - * @param int $tag Type tag |
|
58 | - * @throws \UnexpectedValueException If the element type differs from the |
|
59 | - * expected |
|
60 | - * @return ElementBase |
|
61 | - */ |
|
62 | - public function expectType($tag): ElementBase; |
|
52 | + /** |
|
53 | + * Check whether the element is a type of a given tag. |
|
54 | + * |
|
55 | + * Throws an exception if expectation fails. |
|
56 | + * |
|
57 | + * @param int $tag Type tag |
|
58 | + * @throws \UnexpectedValueException If the element type differs from the |
|
59 | + * expected |
|
60 | + * @return ElementBase |
|
61 | + */ |
|
62 | + public function expectType($tag): ElementBase; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Check whether the element is tagged (context specific). |
|
66 | - * |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function isTagged(): bool; |
|
64 | + /** |
|
65 | + * Check whether the element is tagged (context specific). |
|
66 | + * |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function isTagged(): bool; |
|
70 | 70 | |
71 | - /** |
|
72 | - * Check whether the element is tagged (context specific) and optionally has |
|
73 | - * a given tag. |
|
74 | - * |
|
75 | - * Throws an exception if the element is not tagged or tag differs from |
|
76 | - * the expected. |
|
77 | - * |
|
78 | - * @param int|null $tag Optional type tag |
|
79 | - * @throws \UnexpectedValueException If expectation fails |
|
80 | - * @return \ASN1\Type\TaggedType |
|
81 | - */ |
|
82 | - public function expectTagged($tag = null): TaggedType; |
|
71 | + /** |
|
72 | + * Check whether the element is tagged (context specific) and optionally has |
|
73 | + * a given tag. |
|
74 | + * |
|
75 | + * Throws an exception if the element is not tagged or tag differs from |
|
76 | + * the expected. |
|
77 | + * |
|
78 | + * @param int|null $tag Optional type tag |
|
79 | + * @throws \UnexpectedValueException If expectation fails |
|
80 | + * @return \ASN1\Type\TaggedType |
|
81 | + */ |
|
82 | + public function expectTagged($tag = null): TaggedType; |
|
83 | 83 | |
84 | - /** |
|
85 | - * Get the object as an abstract Element instance. |
|
86 | - * |
|
87 | - * @return \ASN1\Element |
|
88 | - */ |
|
89 | - public function asElement(): Element; |
|
84 | + /** |
|
85 | + * Get the object as an abstract Element instance. |
|
86 | + * |
|
87 | + * @return \ASN1\Element |
|
88 | + */ |
|
89 | + public function asElement(): Element; |
|
90 | 90 | } |
@@ -12,285 +12,285 @@ |
||
12 | 12 | */ |
13 | 13 | class Identifier implements Encodable |
14 | 14 | { |
15 | - // Type class enumerations |
|
16 | - const CLASS_UNIVERSAL = 0b00; |
|
17 | - const CLASS_APPLICATION = 0b01; |
|
18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | - const CLASS_PRIVATE = 0b11; |
|
15 | + // Type class enumerations |
|
16 | + const CLASS_UNIVERSAL = 0b00; |
|
17 | + const CLASS_APPLICATION = 0b01; |
|
18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | + const CLASS_PRIVATE = 0b11; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Mapping from type class to human readable name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | - self::CLASS_APPLICATION => "APPLICATION", |
|
31 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | - self::CLASS_PRIVATE => "PRIVATE", |
|
33 | - /* @formatter:on */ |
|
34 | - ]; |
|
21 | + /** |
|
22 | + * Mapping from type class to human readable name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | + self::CLASS_APPLICATION => "APPLICATION", |
|
31 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | + self::CLASS_PRIVATE => "PRIVATE", |
|
33 | + /* @formatter:on */ |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var int|string |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var int|string |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = $tag; |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = $tag; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param int|null $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. Variable is updated to |
|
81 | - * the offset next to the parsed identifier. If null, start from |
|
82 | - * offset 0. |
|
83 | - * @throws DecodeException If decoding fails |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromDER(string $data, int &$offset = null): self |
|
87 | - { |
|
88 | - $idx = $offset ? $offset : 0; |
|
89 | - $datalen = strlen($data); |
|
90 | - if ($idx >= $datalen) { |
|
91 | - throw new DecodeException("Invalid offset."); |
|
92 | - } |
|
93 | - $byte = ord($data[$idx++]); |
|
94 | - // bits 8 and 7 (class) |
|
95 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | - $class = (0b11000000 & $byte) >> 6; |
|
97 | - // bit 6 (0 = primitive / 1 = constructed) |
|
98 | - $pc = (0b00100000 & $byte) >> 5; |
|
99 | - // bits 5 to 1 (tag number) |
|
100 | - $tag = (0b00011111 & $byte); |
|
101 | - // long-form identifier |
|
102 | - if (0x1f == $tag) { |
|
103 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | - } |
|
105 | - if (isset($offset)) { |
|
106 | - $offset = $idx; |
|
107 | - } |
|
108 | - return new self($class, $pc, $tag); |
|
109 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param int|null $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. Variable is updated to |
|
81 | + * the offset next to the parsed identifier. If null, start from |
|
82 | + * offset 0. |
|
83 | + * @throws DecodeException If decoding fails |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromDER(string $data, int &$offset = null): self |
|
87 | + { |
|
88 | + $idx = $offset ? $offset : 0; |
|
89 | + $datalen = strlen($data); |
|
90 | + if ($idx >= $datalen) { |
|
91 | + throw new DecodeException("Invalid offset."); |
|
92 | + } |
|
93 | + $byte = ord($data[$idx++]); |
|
94 | + // bits 8 and 7 (class) |
|
95 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | + $class = (0b11000000 & $byte) >> 6; |
|
97 | + // bit 6 (0 = primitive / 1 = constructed) |
|
98 | + $pc = (0b00100000 & $byte) >> 5; |
|
99 | + // bits 5 to 1 (tag number) |
|
100 | + $tag = (0b00011111 & $byte); |
|
101 | + // long-form identifier |
|
102 | + if (0x1f == $tag) { |
|
103 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | + } |
|
105 | + if (isset($offset)) { |
|
106 | + $offset = $idx; |
|
107 | + } |
|
108 | + return new self($class, $pc, $tag); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Parse long form tag. |
|
113 | - * |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the variable containing offset to data |
|
116 | - * @throws DecodeException If decoding fails |
|
117 | - * @return string Tag number |
|
118 | - */ |
|
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | - { |
|
121 | - $datalen = strlen($data); |
|
122 | - $tag = gmp_init(0, 10); |
|
123 | - while (true) { |
|
124 | - if ($offset >= $datalen) { |
|
125 | - throw new DecodeException( |
|
126 | - "Unexpected end of data while decoding" . |
|
127 | - " long form identifier."); |
|
128 | - } |
|
129 | - $byte = ord($data[$offset++]); |
|
130 | - $tag <<= 7; |
|
131 | - $tag |= 0x7f & $byte; |
|
132 | - // last byte has bit 8 set to zero |
|
133 | - if (!(0x80 & $byte)) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - return gmp_strval($tag, 10); |
|
138 | - } |
|
111 | + /** |
|
112 | + * Parse long form tag. |
|
113 | + * |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the variable containing offset to data |
|
116 | + * @throws DecodeException If decoding fails |
|
117 | + * @return string Tag number |
|
118 | + */ |
|
119 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | + { |
|
121 | + $datalen = strlen($data); |
|
122 | + $tag = gmp_init(0, 10); |
|
123 | + while (true) { |
|
124 | + if ($offset >= $datalen) { |
|
125 | + throw new DecodeException( |
|
126 | + "Unexpected end of data while decoding" . |
|
127 | + " long form identifier."); |
|
128 | + } |
|
129 | + $byte = ord($data[$offset++]); |
|
130 | + $tag <<= 7; |
|
131 | + $tag |= 0x7f & $byte; |
|
132 | + // last byte has bit 8 set to zero |
|
133 | + if (!(0x80 & $byte)) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + return gmp_strval($tag, 10); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @see Encodable::toDER() |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function toDER(): string |
|
146 | - { |
|
147 | - $bytes = []; |
|
148 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | - $tag = gmp_init($this->_tag, 10); |
|
150 | - if ($tag < 0x1f) { |
|
151 | - $bytes[] = $byte | $tag; |
|
152 | - } else { // long-form identifier |
|
153 | - $bytes[] = $byte | 0x1f; |
|
154 | - $octets = []; |
|
155 | - for (; $tag > 0; $tag >>= 7) { |
|
156 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | - } |
|
158 | - // last octet has bit 8 set to zero |
|
159 | - $octets[0] &= 0x7f; |
|
160 | - foreach (array_reverse($octets) as $octet) { |
|
161 | - $bytes[] = $octet; |
|
162 | - } |
|
163 | - } |
|
164 | - return pack("C*", ...$bytes); |
|
165 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @see Encodable::toDER() |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function toDER(): string |
|
146 | + { |
|
147 | + $bytes = []; |
|
148 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | + $tag = gmp_init($this->_tag, 10); |
|
150 | + if ($tag < 0x1f) { |
|
151 | + $bytes[] = $byte | $tag; |
|
152 | + } else { // long-form identifier |
|
153 | + $bytes[] = $byte | 0x1f; |
|
154 | + $octets = []; |
|
155 | + for (; $tag > 0; $tag >>= 7) { |
|
156 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | + } |
|
158 | + // last octet has bit 8 set to zero |
|
159 | + $octets[0] &= 0x7f; |
|
160 | + foreach (array_reverse($octets) as $octet) { |
|
161 | + $bytes[] = $octet; |
|
162 | + } |
|
163 | + } |
|
164 | + return pack("C*", ...$bytes); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get class of the type. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function typeClass(): int |
|
173 | - { |
|
174 | - return $this->_class; |
|
175 | - } |
|
167 | + /** |
|
168 | + * Get class of the type. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function typeClass(): int |
|
173 | + { |
|
174 | + return $this->_class; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get P/C. |
|
179 | - * |
|
180 | - * @return int |
|
181 | - */ |
|
182 | - public function pc(): int |
|
183 | - { |
|
184 | - return $this->_pc; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get P/C. |
|
179 | + * |
|
180 | + * @return int |
|
181 | + */ |
|
182 | + public function pc(): int |
|
183 | + { |
|
184 | + return $this->_pc; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the tag number. |
|
189 | - * |
|
190 | - * @return int|string |
|
191 | - */ |
|
192 | - public function tag() |
|
193 | - { |
|
194 | - return $this->_tag; |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the tag number. |
|
189 | + * |
|
190 | + * @return int|string |
|
191 | + */ |
|
192 | + public function tag() |
|
193 | + { |
|
194 | + return $this->_tag; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Check whether type is of an universal class. |
|
199 | - * |
|
200 | - * @return boolean |
|
201 | - */ |
|
202 | - public function isUniversal(): bool |
|
203 | - { |
|
204 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
205 | - } |
|
197 | + /** |
|
198 | + * Check whether type is of an universal class. |
|
199 | + * |
|
200 | + * @return boolean |
|
201 | + */ |
|
202 | + public function isUniversal(): bool |
|
203 | + { |
|
204 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Check whether type is of an application class. |
|
209 | - * |
|
210 | - * @return boolean |
|
211 | - */ |
|
212 | - public function isApplication(): bool |
|
213 | - { |
|
214 | - return self::CLASS_APPLICATION == $this->_class; |
|
215 | - } |
|
207 | + /** |
|
208 | + * Check whether type is of an application class. |
|
209 | + * |
|
210 | + * @return boolean |
|
211 | + */ |
|
212 | + public function isApplication(): bool |
|
213 | + { |
|
214 | + return self::CLASS_APPLICATION == $this->_class; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Check whether type is of a context specific class. |
|
219 | - * |
|
220 | - * @return boolean |
|
221 | - */ |
|
222 | - public function isContextSpecific(): bool |
|
223 | - { |
|
224 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
225 | - } |
|
217 | + /** |
|
218 | + * Check whether type is of a context specific class. |
|
219 | + * |
|
220 | + * @return boolean |
|
221 | + */ |
|
222 | + public function isContextSpecific(): bool |
|
223 | + { |
|
224 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Check whether type is of a private class. |
|
229 | - * |
|
230 | - * @return boolean |
|
231 | - */ |
|
232 | - public function isPrivate(): bool |
|
233 | - { |
|
234 | - return self::CLASS_PRIVATE == $this->_class; |
|
235 | - } |
|
227 | + /** |
|
228 | + * Check whether type is of a private class. |
|
229 | + * |
|
230 | + * @return boolean |
|
231 | + */ |
|
232 | + public function isPrivate(): bool |
|
233 | + { |
|
234 | + return self::CLASS_PRIVATE == $this->_class; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether content is primitive type. |
|
239 | - * |
|
240 | - * @return boolean |
|
241 | - */ |
|
242 | - public function isPrimitive(): bool |
|
243 | - { |
|
244 | - return self::PRIMITIVE == $this->_pc; |
|
245 | - } |
|
237 | + /** |
|
238 | + * Check whether content is primitive type. |
|
239 | + * |
|
240 | + * @return boolean |
|
241 | + */ |
|
242 | + public function isPrimitive(): bool |
|
243 | + { |
|
244 | + return self::PRIMITIVE == $this->_pc; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Check hether content is constructed type. |
|
249 | - * |
|
250 | - * @return boolean |
|
251 | - */ |
|
252 | - public function isConstructed(): bool |
|
253 | - { |
|
254 | - return self::CONSTRUCTED == $this->_pc; |
|
255 | - } |
|
247 | + /** |
|
248 | + * Check hether content is constructed type. |
|
249 | + * |
|
250 | + * @return boolean |
|
251 | + */ |
|
252 | + public function isConstructed(): bool |
|
253 | + { |
|
254 | + return self::CONSTRUCTED == $this->_pc; |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Get self with given type class. |
|
259 | - * |
|
260 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
261 | - * @return self |
|
262 | - */ |
|
263 | - public function withClass(int $class): self |
|
264 | - { |
|
265 | - $obj = clone $this; |
|
266 | - $obj->_class = $class; |
|
267 | - return $obj; |
|
268 | - } |
|
257 | + /** |
|
258 | + * Get self with given type class. |
|
259 | + * |
|
260 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
261 | + * @return self |
|
262 | + */ |
|
263 | + public function withClass(int $class): self |
|
264 | + { |
|
265 | + $obj = clone $this; |
|
266 | + $obj->_class = $class; |
|
267 | + return $obj; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Get self with given type tag. |
|
272 | - * |
|
273 | - * @param int|string $tag Tag number |
|
274 | - * @return self |
|
275 | - */ |
|
276 | - public function withTag(int $tag): self |
|
277 | - { |
|
278 | - $obj = clone $this; |
|
279 | - $obj->_tag = $tag; |
|
280 | - return $obj; |
|
281 | - } |
|
270 | + /** |
|
271 | + * Get self with given type tag. |
|
272 | + * |
|
273 | + * @param int|string $tag Tag number |
|
274 | + * @return self |
|
275 | + */ |
|
276 | + public function withTag(int $tag): self |
|
277 | + { |
|
278 | + $obj = clone $this; |
|
279 | + $obj->_tag = $tag; |
|
280 | + return $obj; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Get human readable name of the type class. |
|
285 | - * |
|
286 | - * @param int $class |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - public static function classToName(int $class): string |
|
290 | - { |
|
291 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
292 | - return "CLASS $class"; |
|
293 | - } |
|
294 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
295 | - } |
|
283 | + /** |
|
284 | + * Get human readable name of the type class. |
|
285 | + * |
|
286 | + * @param int $class |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + public static function classToName(int $class): string |
|
290 | + { |
|
291 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
292 | + return "CLASS $class"; |
|
293 | + } |
|
294 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
295 | + } |
|
296 | 296 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
28 | + const MAP_CLASS_TO_NAME = [/* @formatter:off */ |
|
29 | 29 | self::CLASS_UNIVERSAL => "UNIVERSAL", |
30 | 30 | self::CLASS_APPLICATION => "APPLICATION", |
31 | 31 | self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @throws DecodeException If decoding fails |
84 | 84 | * @return self |
85 | 85 | */ |
86 | - public static function fromDER(string $data, int &$offset = null): self |
|
86 | + public static function fromDER(string $data, int & $offset = null): self |
|
87 | 87 | { |
88 | 88 | $idx = $offset ? $offset : 0; |
89 | 89 | $datalen = strlen($data); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * @throws DecodeException If decoding fails |
117 | 117 | * @return string Tag number |
118 | 118 | */ |
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
119 | + private static function _decodeLongFormTag(string $data, int & $offset): string |
|
120 | 120 | { |
121 | 121 | $datalen = strlen($data); |
122 | 122 | $tag = gmp_init(0, 10); |
@@ -12,192 +12,192 @@ |
||
12 | 12 | */ |
13 | 13 | class Length implements Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Length. |
|
17 | - * |
|
18 | - * @var int|string |
|
19 | - */ |
|
20 | - private $_length; |
|
15 | + /** |
|
16 | + * Length. |
|
17 | + * |
|
18 | + * @var int|string |
|
19 | + */ |
|
20 | + private $_length; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Whether length is indefinite. |
|
24 | - * |
|
25 | - * @var boolean |
|
26 | - */ |
|
27 | - private $_indefinite; |
|
22 | + /** |
|
23 | + * Whether length is indefinite. |
|
24 | + * |
|
25 | + * @var boolean |
|
26 | + */ |
|
27 | + private $_indefinite; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param int|string $length Length |
|
33 | - * @param boolean $indefinite Whether length is indefinite |
|
34 | - */ |
|
35 | - public function __construct($length, bool $indefinite = false) |
|
36 | - { |
|
37 | - $this->_length = $length; |
|
38 | - $this->_indefinite = $indefinite; |
|
39 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param int|string $length Length |
|
33 | + * @param boolean $indefinite Whether length is indefinite |
|
34 | + */ |
|
35 | + public function __construct($length, bool $indefinite = false) |
|
36 | + { |
|
37 | + $this->_length = $length; |
|
38 | + $this->_indefinite = $indefinite; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Decode length component from DER data. |
|
43 | - * |
|
44 | - * @param string $data DER encoded data |
|
45 | - * @param int|null $offset Reference to the variable that contains offset |
|
46 | - * into the data where to start parsing. Variable is updated to |
|
47 | - * the offset next to the parsed length component. If null, start |
|
48 | - * from offset 0. |
|
49 | - * @throws DecodeException If decoding fails |
|
50 | - * @return self |
|
51 | - */ |
|
52 | - public static function fromDER(string $data, int &$offset = null): self |
|
53 | - { |
|
54 | - $idx = $offset ? $offset : 0; |
|
55 | - $datalen = strlen($data); |
|
56 | - if ($idx >= $datalen) { |
|
57 | - throw new DecodeException("Invalid offset."); |
|
58 | - } |
|
59 | - $indefinite = false; |
|
60 | - $byte = ord($data[$idx++]); |
|
61 | - // bits 7 to 1 |
|
62 | - $length = (0x7f & $byte); |
|
63 | - // long form |
|
64 | - if (0x80 & $byte) { |
|
65 | - if (!$length) { |
|
66 | - $indefinite = true; |
|
67 | - } else { |
|
68 | - if ($idx + $length > $datalen) { |
|
69 | - throw new DecodeException("Too many length octets."); |
|
70 | - } |
|
71 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
72 | - } |
|
73 | - } |
|
74 | - if (isset($offset)) { |
|
75 | - $offset = $idx; |
|
76 | - } |
|
77 | - return new self($length, $indefinite); |
|
78 | - } |
|
41 | + /** |
|
42 | + * Decode length component from DER data. |
|
43 | + * |
|
44 | + * @param string $data DER encoded data |
|
45 | + * @param int|null $offset Reference to the variable that contains offset |
|
46 | + * into the data where to start parsing. Variable is updated to |
|
47 | + * the offset next to the parsed length component. If null, start |
|
48 | + * from offset 0. |
|
49 | + * @throws DecodeException If decoding fails |
|
50 | + * @return self |
|
51 | + */ |
|
52 | + public static function fromDER(string $data, int &$offset = null): self |
|
53 | + { |
|
54 | + $idx = $offset ? $offset : 0; |
|
55 | + $datalen = strlen($data); |
|
56 | + if ($idx >= $datalen) { |
|
57 | + throw new DecodeException("Invalid offset."); |
|
58 | + } |
|
59 | + $indefinite = false; |
|
60 | + $byte = ord($data[$idx++]); |
|
61 | + // bits 7 to 1 |
|
62 | + $length = (0x7f & $byte); |
|
63 | + // long form |
|
64 | + if (0x80 & $byte) { |
|
65 | + if (!$length) { |
|
66 | + $indefinite = true; |
|
67 | + } else { |
|
68 | + if ($idx + $length > $datalen) { |
|
69 | + throw new DecodeException("Too many length octets."); |
|
70 | + } |
|
71 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
72 | + } |
|
73 | + } |
|
74 | + if (isset($offset)) { |
|
75 | + $offset = $idx; |
|
76 | + } |
|
77 | + return new self($length, $indefinite); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Decode long form length. |
|
82 | - * |
|
83 | - * @param int $length Number of octets |
|
84 | - * @param string $data Data |
|
85 | - * @param int $offset Reference to the variable containing offset to the |
|
86 | - * data. |
|
87 | - * @throws DecodeException If decoding fails |
|
88 | - * @return string Integer as a string |
|
89 | - */ |
|
90 | - private static function _decodeLongFormLength(int $length, string $data, |
|
91 | - int &$offset): string |
|
92 | - { |
|
93 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
94 | - if ($length == 127) { |
|
95 | - throw new DecodeException("Invalid number of length octets."); |
|
96 | - } |
|
97 | - $num = gmp_init(0, 10); |
|
98 | - while (--$length >= 0) { |
|
99 | - $byte = ord($data[$offset++]); |
|
100 | - $num <<= 8; |
|
101 | - $num |= $byte; |
|
102 | - } |
|
80 | + /** |
|
81 | + * Decode long form length. |
|
82 | + * |
|
83 | + * @param int $length Number of octets |
|
84 | + * @param string $data Data |
|
85 | + * @param int $offset Reference to the variable containing offset to the |
|
86 | + * data. |
|
87 | + * @throws DecodeException If decoding fails |
|
88 | + * @return string Integer as a string |
|
89 | + */ |
|
90 | + private static function _decodeLongFormLength(int $length, string $data, |
|
91 | + int &$offset): string |
|
92 | + { |
|
93 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
94 | + if ($length == 127) { |
|
95 | + throw new DecodeException("Invalid number of length octets."); |
|
96 | + } |
|
97 | + $num = gmp_init(0, 10); |
|
98 | + while (--$length >= 0) { |
|
99 | + $byte = ord($data[$offset++]); |
|
100 | + $num <<= 8; |
|
101 | + $num |= $byte; |
|
102 | + } |
|
103 | 103 | |
104 | - return gmp_strval($num); |
|
105 | - } |
|
104 | + return gmp_strval($num); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Decode length from DER. |
|
109 | - * |
|
110 | - * Throws an exception if length doesn't match with expected or if data |
|
111 | - * doesn't contain enough bytes. |
|
112 | - * |
|
113 | - * @see self::fromDER |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the offset variable |
|
116 | - * @param int|null $expected Expected length, null to bypass checking |
|
117 | - * @throws DecodeException If decoding or expectation fails |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public static function expectFromDER(string $data, int &$offset, |
|
121 | - int $expected = null): self |
|
122 | - { |
|
123 | - $idx = $offset; |
|
124 | - $length = self::fromDER($data, $idx); |
|
125 | - // DER encoding must have definite length (spec 10.1) |
|
126 | - if ($length->isIndefinite()) { |
|
127 | - throw new DecodeException("DER encoding must have definite length."); |
|
128 | - } |
|
129 | - // if certain length was expected |
|
130 | - if (isset($expected) && $expected != $length->_length) { |
|
131 | - throw new DecodeException( |
|
132 | - sprintf("Expected length %d, got %d.", $expected, |
|
133 | - $length->_length)); |
|
134 | - } |
|
135 | - // check that enough data is available |
|
136 | - if (strlen($data) < $idx + $length->_length) { |
|
137 | - throw new DecodeException( |
|
138 | - sprintf("Length %d overflows data, %d bytes left.", |
|
139 | - $length->_length, strlen($data) - $idx)); |
|
140 | - } |
|
141 | - $offset = $idx; |
|
142 | - return $length; |
|
143 | - } |
|
107 | + /** |
|
108 | + * Decode length from DER. |
|
109 | + * |
|
110 | + * Throws an exception if length doesn't match with expected or if data |
|
111 | + * doesn't contain enough bytes. |
|
112 | + * |
|
113 | + * @see self::fromDER |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the offset variable |
|
116 | + * @param int|null $expected Expected length, null to bypass checking |
|
117 | + * @throws DecodeException If decoding or expectation fails |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public static function expectFromDER(string $data, int &$offset, |
|
121 | + int $expected = null): self |
|
122 | + { |
|
123 | + $idx = $offset; |
|
124 | + $length = self::fromDER($data, $idx); |
|
125 | + // DER encoding must have definite length (spec 10.1) |
|
126 | + if ($length->isIndefinite()) { |
|
127 | + throw new DecodeException("DER encoding must have definite length."); |
|
128 | + } |
|
129 | + // if certain length was expected |
|
130 | + if (isset($expected) && $expected != $length->_length) { |
|
131 | + throw new DecodeException( |
|
132 | + sprintf("Expected length %d, got %d.", $expected, |
|
133 | + $length->_length)); |
|
134 | + } |
|
135 | + // check that enough data is available |
|
136 | + if (strlen($data) < $idx + $length->_length) { |
|
137 | + throw new DecodeException( |
|
138 | + sprintf("Length %d overflows data, %d bytes left.", |
|
139 | + $length->_length, strlen($data) - $idx)); |
|
140 | + } |
|
141 | + $offset = $idx; |
|
142 | + return $length; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * |
|
147 | - * @see Encodable::toDER() |
|
148 | - * @throws \DomainException If length is too large to encode |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function toDER(): string |
|
152 | - { |
|
153 | - $bytes = []; |
|
154 | - if ($this->_indefinite) { |
|
155 | - $bytes[] = 0x80; |
|
156 | - } else { |
|
157 | - $num = gmp_init($this->_length, 10); |
|
158 | - // long form |
|
159 | - if ($num > 127) { |
|
160 | - $octets = []; |
|
161 | - for (; $num > 0; $num >>= 8) { |
|
162 | - $octets[] = gmp_intval(0xff & $num); |
|
163 | - } |
|
164 | - $count = count($octets); |
|
165 | - // first octet must not be 0xff |
|
166 | - if ($count >= 127) { |
|
167 | - throw new \DomainException("Too many length octets."); |
|
168 | - } |
|
169 | - $bytes[] = 0x80 | $count; |
|
170 | - foreach (array_reverse($octets) as $octet) { |
|
171 | - $bytes[] = $octet; |
|
172 | - } |
|
173 | - } else { // short form |
|
174 | - $bytes[] = gmp_intval($num); |
|
175 | - } |
|
176 | - } |
|
177 | - return pack("C*", ...$bytes); |
|
178 | - } |
|
145 | + /** |
|
146 | + * |
|
147 | + * @see Encodable::toDER() |
|
148 | + * @throws \DomainException If length is too large to encode |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function toDER(): string |
|
152 | + { |
|
153 | + $bytes = []; |
|
154 | + if ($this->_indefinite) { |
|
155 | + $bytes[] = 0x80; |
|
156 | + } else { |
|
157 | + $num = gmp_init($this->_length, 10); |
|
158 | + // long form |
|
159 | + if ($num > 127) { |
|
160 | + $octets = []; |
|
161 | + for (; $num > 0; $num >>= 8) { |
|
162 | + $octets[] = gmp_intval(0xff & $num); |
|
163 | + } |
|
164 | + $count = count($octets); |
|
165 | + // first octet must not be 0xff |
|
166 | + if ($count >= 127) { |
|
167 | + throw new \DomainException("Too many length octets."); |
|
168 | + } |
|
169 | + $bytes[] = 0x80 | $count; |
|
170 | + foreach (array_reverse($octets) as $octet) { |
|
171 | + $bytes[] = $octet; |
|
172 | + } |
|
173 | + } else { // short form |
|
174 | + $bytes[] = gmp_intval($num); |
|
175 | + } |
|
176 | + } |
|
177 | + return pack("C*", ...$bytes); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Get the length. |
|
182 | - * |
|
183 | - * @throws \LogicException If length is indefinite |
|
184 | - * @return int|string |
|
185 | - */ |
|
186 | - public function length() |
|
187 | - { |
|
188 | - if ($this->_indefinite) { |
|
189 | - throw new \LogicException("Length is indefinite."); |
|
190 | - } |
|
191 | - return $this->_length; |
|
192 | - } |
|
180 | + /** |
|
181 | + * Get the length. |
|
182 | + * |
|
183 | + * @throws \LogicException If length is indefinite |
|
184 | + * @return int|string |
|
185 | + */ |
|
186 | + public function length() |
|
187 | + { |
|
188 | + if ($this->_indefinite) { |
|
189 | + throw new \LogicException("Length is indefinite."); |
|
190 | + } |
|
191 | + return $this->_length; |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Whether length is indefinite. |
|
196 | - * |
|
197 | - * @return boolean |
|
198 | - */ |
|
199 | - public function isIndefinite(): bool |
|
200 | - { |
|
201 | - return $this->_indefinite; |
|
202 | - } |
|
194 | + /** |
|
195 | + * Whether length is indefinite. |
|
196 | + * |
|
197 | + * @return boolean |
|
198 | + */ |
|
199 | + public function isIndefinite(): bool |
|
200 | + { |
|
201 | + return $this->_indefinite; |
|
202 | + } |
|
203 | 203 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @throws DecodeException If decoding fails |
50 | 50 | * @return self |
51 | 51 | */ |
52 | - public static function fromDER(string $data, int &$offset = null): self |
|
52 | + public static function fromDER(string $data, int & $offset = null): self |
|
53 | 53 | { |
54 | 54 | $idx = $offset ? $offset : 0; |
55 | 55 | $datalen = strlen($data); |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | * @return string Integer as a string |
89 | 89 | */ |
90 | 90 | private static function _decodeLongFormLength(int $length, string $data, |
91 | - int &$offset): string |
|
91 | + int & $offset): string |
|
92 | 92 | { |
93 | 93 | // first octet must not be 0xff (spec 8.1.3.5c) |
94 | 94 | if ($length == 127) { |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * @throws DecodeException If decoding or expectation fails |
118 | 118 | * @return self |
119 | 119 | */ |
120 | - public static function expectFromDER(string $data, int &$offset, |
|
120 | + public static function expectFromDER(string $data, int & $offset, |
|
121 | 121 | int $expected = null): self |
122 | 122 | { |
123 | 123 | $idx = $offset; |
@@ -17,85 +17,85 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class TaggedType extends Element |
19 | 19 | { |
20 | - /** |
|
21 | - * |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | - int &$offset): ElementBase |
|
26 | - { |
|
27 | - $idx = $offset; |
|
28 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | - $length = Length::expectFromDER($data, $idx); |
|
30 | - $offset = $idx + $length->length(); |
|
31 | - return $type; |
|
32 | - } |
|
20 | + /** |
|
21 | + * |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | + int &$offset): ElementBase |
|
26 | + { |
|
27 | + $idx = $offset; |
|
28 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | + $length = Length::expectFromDER($data, $idx); |
|
30 | + $offset = $idx + $length->length(); |
|
31 | + return $type; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Check whether element supports explicit tagging. |
|
36 | - * |
|
37 | - * @param int|null $expectedTag Optional outer tag expectation |
|
38 | - * @throws \UnexpectedValueException If expectation fails |
|
39 | - * @return ExplicitTagging |
|
40 | - */ |
|
41 | - public function expectExplicit(int $expectedTag = null): ExplicitTagging |
|
42 | - { |
|
43 | - $el = $this; |
|
44 | - if (!$el instanceof ExplicitTagging) { |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Element doesn't implement explicit tagging."); |
|
47 | - } |
|
48 | - if (isset($expectedTag)) { |
|
49 | - $el->expectTagged($expectedTag); |
|
50 | - } |
|
51 | - return $el; |
|
52 | - } |
|
34 | + /** |
|
35 | + * Check whether element supports explicit tagging. |
|
36 | + * |
|
37 | + * @param int|null $expectedTag Optional outer tag expectation |
|
38 | + * @throws \UnexpectedValueException If expectation fails |
|
39 | + * @return ExplicitTagging |
|
40 | + */ |
|
41 | + public function expectExplicit(int $expectedTag = null): ExplicitTagging |
|
42 | + { |
|
43 | + $el = $this; |
|
44 | + if (!$el instanceof ExplicitTagging) { |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Element doesn't implement explicit tagging."); |
|
47 | + } |
|
48 | + if (isset($expectedTag)) { |
|
49 | + $el->expectTagged($expectedTag); |
|
50 | + } |
|
51 | + return $el; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the wrapped inner element employing explicit tagging. |
|
56 | - * |
|
57 | - * @param int|null $expectedTag Optional outer tag expectation |
|
58 | - * @throws \UnexpectedValueException If expectation fails |
|
59 | - * @return UnspecifiedType |
|
60 | - */ |
|
61 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | - { |
|
63 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | - } |
|
54 | + /** |
|
55 | + * Get the wrapped inner element employing explicit tagging. |
|
56 | + * |
|
57 | + * @param int|null $expectedTag Optional outer tag expectation |
|
58 | + * @throws \UnexpectedValueException If expectation fails |
|
59 | + * @return UnspecifiedType |
|
60 | + */ |
|
61 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | + { |
|
63 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Check whether element supports implicit tagging. |
|
68 | - * |
|
69 | - * @param int|null $expectedTag Optional outer tag expectation |
|
70 | - * @throws \UnexpectedValueException If expectation fails |
|
71 | - * @return ImplicitTagging |
|
72 | - */ |
|
73 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | - { |
|
75 | - $el = $this; |
|
76 | - if (!$el instanceof ImplicitTagging) { |
|
77 | - throw new \UnexpectedValueException( |
|
78 | - "Element doesn't implement implicit tagging."); |
|
79 | - } |
|
80 | - if (isset($expectedTag)) { |
|
81 | - $el->expectTagged($expectedTag); |
|
82 | - } |
|
83 | - return $el; |
|
84 | - } |
|
66 | + /** |
|
67 | + * Check whether element supports implicit tagging. |
|
68 | + * |
|
69 | + * @param int|null $expectedTag Optional outer tag expectation |
|
70 | + * @throws \UnexpectedValueException If expectation fails |
|
71 | + * @return ImplicitTagging |
|
72 | + */ |
|
73 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | + { |
|
75 | + $el = $this; |
|
76 | + if (!$el instanceof ImplicitTagging) { |
|
77 | + throw new \UnexpectedValueException( |
|
78 | + "Element doesn't implement implicit tagging."); |
|
79 | + } |
|
80 | + if (isset($expectedTag)) { |
|
81 | + $el->expectTagged($expectedTag); |
|
82 | + } |
|
83 | + return $el; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the wrapped inner element employing implicit tagging. |
|
88 | - * |
|
89 | - * @param int $tag Type tag of the inner element |
|
90 | - * @param int|null $expectedTag Optional outer tag expectation |
|
91 | - * @param int $expectedClass Optional inner type class expectation |
|
92 | - * @throws \UnexpectedValueException If expectation fails |
|
93 | - * @return UnspecifiedType |
|
94 | - */ |
|
95 | - public function asImplicit($tag, $expectedTag = null, |
|
96 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | - { |
|
98 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | - $expectedClass); |
|
100 | - } |
|
86 | + /** |
|
87 | + * Get the wrapped inner element employing implicit tagging. |
|
88 | + * |
|
89 | + * @param int $tag Type tag of the inner element |
|
90 | + * @param int|null $expectedTag Optional outer tag expectation |
|
91 | + * @param int $expectedClass Optional inner type class expectation |
|
92 | + * @throws \UnexpectedValueException If expectation fails |
|
93 | + * @return UnspecifiedType |
|
94 | + */ |
|
95 | + public function asImplicit($tag, $expectedTag = null, |
|
96 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | + { |
|
98 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | + $expectedClass); |
|
100 | + } |
|
101 | 101 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * {@inheritdoc} |
23 | 23 | */ |
24 | 24 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
25 | - int &$offset): ElementBase |
|
25 | + int & $offset): ElementBase |
|
26 | 26 | { |
27 | 27 | $idx = $offset; |
28 | 28 | $type = new DERTaggedType($identifier, $data, $idx); |
@@ -12,54 +12,54 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor |
|
17 | - * |
|
18 | - * @param Element[] $elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor |
|
17 | + * |
|
18 | + * @param Element[] $elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * Used for DER encoding of SET type. |
|
29 | - * |
|
30 | - * @return self |
|
31 | - */ |
|
32 | - public function sortedSet(): self |
|
33 | - { |
|
34 | - $obj = clone $this; |
|
35 | - usort($obj->_elements, |
|
36 | - function (Element $a, Element $b) { |
|
37 | - if ($a->typeClass() != $b->typeClass()) { |
|
38 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | - } |
|
40 | - if ($a->tag() == $b->tag()) { |
|
41 | - return 0; |
|
42 | - } |
|
43 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | - }); |
|
45 | - return $obj; |
|
46 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * Used for DER encoding of SET type. |
|
29 | + * |
|
30 | + * @return self |
|
31 | + */ |
|
32 | + public function sortedSet(): self |
|
33 | + { |
|
34 | + $obj = clone $this; |
|
35 | + usort($obj->_elements, |
|
36 | + function (Element $a, Element $b) { |
|
37 | + if ($a->typeClass() != $b->typeClass()) { |
|
38 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
39 | + } |
|
40 | + if ($a->tag() == $b->tag()) { |
|
41 | + return 0; |
|
42 | + } |
|
43 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
44 | + }); |
|
45 | + return $obj; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Sort by encoding ascending order. |
|
50 | - * Used for DER encoding of SET OF type. |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public function sortedSetOf(): self |
|
55 | - { |
|
56 | - $obj = clone $this; |
|
57 | - usort($obj->_elements, |
|
58 | - function (Element $a, Element $b) { |
|
59 | - $a_der = $a->toDER(); |
|
60 | - $b_der = $b->toDER(); |
|
61 | - return strcmp($a_der, $b_der); |
|
62 | - }); |
|
63 | - return $obj; |
|
64 | - } |
|
48 | + /** |
|
49 | + * Sort by encoding ascending order. |
|
50 | + * Used for DER encoding of SET OF type. |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public function sortedSetOf(): self |
|
55 | + { |
|
56 | + $obj = clone $this; |
|
57 | + usort($obj->_elements, |
|
58 | + function (Element $a, Element $b) { |
|
59 | + $a_der = $a->toDER(); |
|
60 | + $b_der = $b->toDER(); |
|
61 | + return strcmp($a_der, $b_der); |
|
62 | + }); |
|
63 | + return $obj; |
|
64 | + } |
|
65 | 65 | } |