@@ -16,57 +16,57 @@ |
||
16 | 16 | */ |
17 | 17 | class TargetGroup extends Target |
18 | 18 | { |
19 | - /** |
|
20 | - * Group name. |
|
21 | - * |
|
22 | - * @var GeneralName |
|
23 | - */ |
|
24 | - protected $_name; |
|
19 | + /** |
|
20 | + * Group name. |
|
21 | + * |
|
22 | + * @var GeneralName |
|
23 | + */ |
|
24 | + protected $_name; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param GeneralName $name |
|
30 | - */ |
|
31 | - public function __construct(GeneralName $name) |
|
32 | - { |
|
33 | - $this->_name = $name; |
|
34 | - $this->_type = self::TYPE_GROUP; |
|
35 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param GeneralName $name |
|
30 | + */ |
|
31 | + public function __construct(GeneralName $name) |
|
32 | + { |
|
33 | + $this->_name = $name; |
|
34 | + $this->_type = self::TYPE_GROUP; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - * |
|
40 | - * @return self |
|
41 | - */ |
|
42 | - public static function fromChosenASN1(TaggedType $el): Target |
|
43 | - { |
|
44 | - return new self(GeneralName::fromASN1($el)); |
|
45 | - } |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + * |
|
40 | + * @return self |
|
41 | + */ |
|
42 | + public static function fromChosenASN1(TaggedType $el): Target |
|
43 | + { |
|
44 | + return new self(GeneralName::fromASN1($el)); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function string(): string |
|
51 | - { |
|
52 | - return $this->_name->string(); |
|
53 | - } |
|
47 | + /** |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function string(): string |
|
51 | + { |
|
52 | + return $this->_name->string(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get group name. |
|
57 | - * |
|
58 | - * @return GeneralName |
|
59 | - */ |
|
60 | - public function name(): GeneralName |
|
61 | - { |
|
62 | - return $this->_name; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get group name. |
|
57 | + * |
|
58 | + * @return GeneralName |
|
59 | + */ |
|
60 | + public function name(): GeneralName |
|
61 | + { |
|
62 | + return $this->_name; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - public function toASN1(): Element |
|
69 | - { |
|
70 | - return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
71 | - } |
|
65 | + /** |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + public function toASN1(): Element |
|
69 | + { |
|
70 | + return new ExplicitlyTaggedType($this->_type, $this->_name->toASN1()); |
|
71 | + } |
|
72 | 72 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension\Target; |
6 | 6 |
@@ -14,92 +14,92 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class Target |
16 | 16 | { |
17 | - const TYPE_NAME = 0; |
|
18 | - const TYPE_GROUP = 1; |
|
19 | - const TYPE_CERT = 2; |
|
17 | + const TYPE_NAME = 0; |
|
18 | + const TYPE_GROUP = 1; |
|
19 | + const TYPE_CERT = 2; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Type tag. |
|
23 | - * |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $_type; |
|
21 | + /** |
|
22 | + * Type tag. |
|
23 | + * |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $_type; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Generate ASN.1 element. |
|
30 | - * |
|
31 | - * @return Element |
|
32 | - */ |
|
33 | - abstract public function toASN1(): Element; |
|
28 | + /** |
|
29 | + * Generate ASN.1 element. |
|
30 | + * |
|
31 | + * @return Element |
|
32 | + */ |
|
33 | + abstract public function toASN1(): Element; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Get string value of the target. |
|
37 | - * |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - abstract public function string(): string; |
|
35 | + /** |
|
36 | + * Get string value of the target. |
|
37 | + * |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + abstract public function string(): string; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Initialize concrete object from the chosen ASN.1 element. |
|
44 | - * |
|
45 | - * @param TaggedType $el |
|
46 | - * |
|
47 | - * @return self |
|
48 | - */ |
|
49 | - public static function fromChosenASN1(TaggedType $el): Target |
|
50 | - { |
|
51 | - throw new \BadMethodCallException( |
|
52 | - __FUNCTION__ . ' must be implemented in the derived class.'); |
|
53 | - } |
|
42 | + /** |
|
43 | + * Initialize concrete object from the chosen ASN.1 element. |
|
44 | + * |
|
45 | + * @param TaggedType $el |
|
46 | + * |
|
47 | + * @return self |
|
48 | + */ |
|
49 | + public static function fromChosenASN1(TaggedType $el): Target |
|
50 | + { |
|
51 | + throw new \BadMethodCallException( |
|
52 | + __FUNCTION__ . ' must be implemented in the derived class.'); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Parse from ASN.1. |
|
57 | - * |
|
58 | - * @param TaggedType $el |
|
59 | - * |
|
60 | - * @throws \UnexpectedValueException |
|
61 | - * |
|
62 | - * @return self |
|
63 | - */ |
|
64 | - public static function fromASN1(TaggedType $el): self |
|
65 | - { |
|
66 | - switch ($el->tag()) { |
|
67 | - case self::TYPE_NAME: |
|
68 | - return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
69 | - case self::TYPE_GROUP: |
|
70 | - return TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()); |
|
71 | - case self::TYPE_CERT: |
|
72 | - throw new \RuntimeException('targetCert not supported.'); |
|
73 | - } |
|
74 | - throw new \UnexpectedValueException( |
|
75 | - 'Target type ' . $el->tag() . ' not supported.'); |
|
76 | - } |
|
55 | + /** |
|
56 | + * Parse from ASN.1. |
|
57 | + * |
|
58 | + * @param TaggedType $el |
|
59 | + * |
|
60 | + * @throws \UnexpectedValueException |
|
61 | + * |
|
62 | + * @return self |
|
63 | + */ |
|
64 | + public static function fromASN1(TaggedType $el): self |
|
65 | + { |
|
66 | + switch ($el->tag()) { |
|
67 | + case self::TYPE_NAME: |
|
68 | + return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
69 | + case self::TYPE_GROUP: |
|
70 | + return TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()); |
|
71 | + case self::TYPE_CERT: |
|
72 | + throw new \RuntimeException('targetCert not supported.'); |
|
73 | + } |
|
74 | + throw new \UnexpectedValueException( |
|
75 | + 'Target type ' . $el->tag() . ' not supported.'); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get type tag. |
|
80 | - * |
|
81 | - * @return int |
|
82 | - */ |
|
83 | - public function type(): int |
|
84 | - { |
|
85 | - return $this->_type; |
|
86 | - } |
|
78 | + /** |
|
79 | + * Get type tag. |
|
80 | + * |
|
81 | + * @return int |
|
82 | + */ |
|
83 | + public function type(): int |
|
84 | + { |
|
85 | + return $this->_type; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Check whether target is equal to another. |
|
90 | - * |
|
91 | - * @param Target $other |
|
92 | - * |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - public function equals(Target $other): bool |
|
96 | - { |
|
97 | - if ($this->_type !== $other->_type) { |
|
98 | - return false; |
|
99 | - } |
|
100 | - if ($this->toASN1()->toDER() !== $other->toASN1()->toDER()) { |
|
101 | - return false; |
|
102 | - } |
|
103 | - return true; |
|
104 | - } |
|
88 | + /** |
|
89 | + * Check whether target is equal to another. |
|
90 | + * |
|
91 | + * @param Target $other |
|
92 | + * |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + public function equals(Target $other): bool |
|
96 | + { |
|
97 | + if ($this->_type !== $other->_type) { |
|
98 | + return false; |
|
99 | + } |
|
100 | + if ($this->toASN1()->toDER() !== $other->toASN1()->toDER()) { |
|
101 | + return false; |
|
102 | + } |
|
103 | + return true; |
|
104 | + } |
|
105 | 105 | } |
@@ -64,12 +64,12 @@ |
||
64 | 64 | public static function fromASN1(TaggedType $el): self |
65 | 65 | { |
66 | 66 | switch ($el->tag()) { |
67 | - case self::TYPE_NAME: |
|
68 | - return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
69 | - case self::TYPE_GROUP: |
|
70 | - return TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()); |
|
71 | - case self::TYPE_CERT: |
|
72 | - throw new \RuntimeException('targetCert not supported.'); |
|
67 | + case self::TYPE_NAME: |
|
68 | + return TargetName::fromChosenASN1($el->asExplicit()->asTagged()); |
|
69 | + case self::TYPE_GROUP: |
|
70 | + return TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()); |
|
71 | + case self::TYPE_CERT: |
|
72 | + throw new \RuntimeException('targetCert not supported.'); |
|
73 | 73 | } |
74 | 74 | throw new \UnexpectedValueException( |
75 | 75 | 'Target type ' . $el->tag() . ' not supported.'); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension\Target; |
6 | 6 |
@@ -21,134 +21,134 @@ |
||
21 | 21 | */ |
22 | 22 | class TargetInformationExtension extends Extension implements \Countable, \IteratorAggregate |
23 | 23 | { |
24 | - /** |
|
25 | - * Targets elements. |
|
26 | - * |
|
27 | - * @var Targets[] |
|
28 | - */ |
|
29 | - protected $_targets; |
|
24 | + /** |
|
25 | + * Targets elements. |
|
26 | + * |
|
27 | + * @var Targets[] |
|
28 | + */ |
|
29 | + protected $_targets; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Targets[] merged to single Targets. |
|
33 | - * |
|
34 | - * @var null|Targets |
|
35 | - */ |
|
36 | - private $_merged; |
|
31 | + /** |
|
32 | + * Targets[] merged to single Targets. |
|
33 | + * |
|
34 | + * @var null|Targets |
|
35 | + */ |
|
36 | + private $_merged; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param bool $critical |
|
42 | - * @param Targets ...$targets |
|
43 | - */ |
|
44 | - public function __construct(bool $critical, Targets ...$targets) |
|
45 | - { |
|
46 | - parent::__construct(self::OID_TARGET_INFORMATION, $critical); |
|
47 | - $this->_targets = $targets; |
|
48 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param bool $critical |
|
42 | + * @param Targets ...$targets |
|
43 | + */ |
|
44 | + public function __construct(bool $critical, Targets ...$targets) |
|
45 | + { |
|
46 | + parent::__construct(self::OID_TARGET_INFORMATION, $critical); |
|
47 | + $this->_targets = $targets; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Reset internal state on clone. |
|
52 | - */ |
|
53 | - public function __clone() |
|
54 | - { |
|
55 | - $this->_merged = null; |
|
56 | - } |
|
50 | + /** |
|
51 | + * Reset internal state on clone. |
|
52 | + */ |
|
53 | + public function __clone() |
|
54 | + { |
|
55 | + $this->_merged = null; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Initialize from one or more Target objects. |
|
60 | - * |
|
61 | - * Extension criticality shall be set to true as specified by RFC 5755. |
|
62 | - * |
|
63 | - * @param Target ...$target |
|
64 | - * |
|
65 | - * @return TargetInformationExtension |
|
66 | - */ |
|
67 | - public static function fromTargets(Target ...$target): self |
|
68 | - { |
|
69 | - return new self(true, new Targets(...$target)); |
|
70 | - } |
|
58 | + /** |
|
59 | + * Initialize from one or more Target objects. |
|
60 | + * |
|
61 | + * Extension criticality shall be set to true as specified by RFC 5755. |
|
62 | + * |
|
63 | + * @param Target ...$target |
|
64 | + * |
|
65 | + * @return TargetInformationExtension |
|
66 | + */ |
|
67 | + public static function fromTargets(Target ...$target): self |
|
68 | + { |
|
69 | + return new self(true, new Targets(...$target)); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get all targets. |
|
74 | - * |
|
75 | - * @return Targets |
|
76 | - */ |
|
77 | - public function targets(): Targets |
|
78 | - { |
|
79 | - if (!isset($this->_merged)) { |
|
80 | - $a = []; |
|
81 | - foreach ($this->_targets as $targets) { |
|
82 | - $a = array_merge($a, $targets->all()); |
|
83 | - } |
|
84 | - $this->_merged = new Targets(...$a); |
|
85 | - } |
|
86 | - return $this->_merged; |
|
87 | - } |
|
72 | + /** |
|
73 | + * Get all targets. |
|
74 | + * |
|
75 | + * @return Targets |
|
76 | + */ |
|
77 | + public function targets(): Targets |
|
78 | + { |
|
79 | + if (!isset($this->_merged)) { |
|
80 | + $a = []; |
|
81 | + foreach ($this->_targets as $targets) { |
|
82 | + $a = array_merge($a, $targets->all()); |
|
83 | + } |
|
84 | + $this->_merged = new Targets(...$a); |
|
85 | + } |
|
86 | + return $this->_merged; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get all name targets. |
|
91 | - * |
|
92 | - * @return Target[] |
|
93 | - */ |
|
94 | - public function names(): array |
|
95 | - { |
|
96 | - return $this->targets()->nameTargets(); |
|
97 | - } |
|
89 | + /** |
|
90 | + * Get all name targets. |
|
91 | + * |
|
92 | + * @return Target[] |
|
93 | + */ |
|
94 | + public function names(): array |
|
95 | + { |
|
96 | + return $this->targets()->nameTargets(); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get all group targets. |
|
101 | - * |
|
102 | - * @return Target[] |
|
103 | - */ |
|
104 | - public function groups(): array |
|
105 | - { |
|
106 | - return $this->targets()->groupTargets(); |
|
107 | - } |
|
99 | + /** |
|
100 | + * Get all group targets. |
|
101 | + * |
|
102 | + * @return Target[] |
|
103 | + */ |
|
104 | + public function groups(): array |
|
105 | + { |
|
106 | + return $this->targets()->groupTargets(); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @see \Countable::count() |
|
111 | - * |
|
112 | - * @return int |
|
113 | - */ |
|
114 | - public function count(): int |
|
115 | - { |
|
116 | - return count($this->targets()); |
|
117 | - } |
|
109 | + /** |
|
110 | + * @see \Countable::count() |
|
111 | + * |
|
112 | + * @return int |
|
113 | + */ |
|
114 | + public function count(): int |
|
115 | + { |
|
116 | + return count($this->targets()); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Get iterator for targets. |
|
121 | - * |
|
122 | - * @see \IteratorAggregate::getIterator() |
|
123 | - * |
|
124 | - * @return \ArrayIterator |
|
125 | - */ |
|
126 | - public function getIterator(): \ArrayIterator |
|
127 | - { |
|
128 | - return new \ArrayIterator($this->targets()->all()); |
|
129 | - } |
|
119 | + /** |
|
120 | + * Get iterator for targets. |
|
121 | + * |
|
122 | + * @see \IteratorAggregate::getIterator() |
|
123 | + * |
|
124 | + * @return \ArrayIterator |
|
125 | + */ |
|
126 | + public function getIterator(): \ArrayIterator |
|
127 | + { |
|
128 | + return new \ArrayIterator($this->targets()->all()); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * {@inheritdoc} |
|
133 | - */ |
|
134 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
135 | - { |
|
136 | - $targets = array_map( |
|
137 | - function (UnspecifiedType $el) { |
|
138 | - return Targets::fromASN1($el->asSequence()); |
|
139 | - }, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
|
140 | - return new self($critical, ...$targets); |
|
141 | - } |
|
131 | + /** |
|
132 | + * {@inheritdoc} |
|
133 | + */ |
|
134 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
135 | + { |
|
136 | + $targets = array_map( |
|
137 | + function (UnspecifiedType $el) { |
|
138 | + return Targets::fromASN1($el->asSequence()); |
|
139 | + }, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
|
140 | + return new self($critical, ...$targets); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * {@inheritdoc} |
|
145 | - */ |
|
146 | - protected function _valueASN1(): Element |
|
147 | - { |
|
148 | - $elements = array_map( |
|
149 | - function (Targets $targets) { |
|
150 | - return $targets->toASN1(); |
|
151 | - }, $this->_targets); |
|
152 | - return new Sequence(...$elements); |
|
153 | - } |
|
143 | + /** |
|
144 | + * {@inheritdoc} |
|
145 | + */ |
|
146 | + protected function _valueASN1(): Element |
|
147 | + { |
|
148 | + $elements = array_map( |
|
149 | + function (Targets $targets) { |
|
150 | + return $targets->toASN1(); |
|
151 | + }, $this->_targets); |
|
152 | + return new Sequence(...$elements); |
|
153 | + } |
|
154 | 154 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension; |
6 | 6 | |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | protected static function _fromDER(string $data, bool $critical): Extension |
135 | 135 | { |
136 | 136 | $targets = array_map( |
137 | - function (UnspecifiedType $el) { |
|
137 | + function(UnspecifiedType $el) { |
|
138 | 138 | return Targets::fromASN1($el->asSequence()); |
139 | 139 | }, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
140 | 140 | return new self($critical, ...$targets); |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | protected function _valueASN1(): Element |
147 | 147 | { |
148 | 148 | $elements = array_map( |
149 | - function (Targets $targets) { |
|
149 | + function(Targets $targets) { |
|
150 | 150 | return $targets->toASN1(); |
151 | 151 | }, $this->_targets); |
152 | 152 | return new Sequence(...$elements); |
@@ -15,50 +15,50 @@ |
||
15 | 15 | */ |
16 | 16 | class IssuerAlternativeNameExtension extends Extension |
17 | 17 | { |
18 | - /** |
|
19 | - * Names. |
|
20 | - * |
|
21 | - * @var GeneralNames |
|
22 | - */ |
|
23 | - protected $_names; |
|
18 | + /** |
|
19 | + * Names. |
|
20 | + * |
|
21 | + * @var GeneralNames |
|
22 | + */ |
|
23 | + protected $_names; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param bool $critical |
|
29 | - * @param GeneralNames $names |
|
30 | - */ |
|
31 | - public function __construct(bool $critical, GeneralNames $names) |
|
32 | - { |
|
33 | - parent::__construct(self::OID_ISSUER_ALT_NAME, $critical); |
|
34 | - $this->_names = $names; |
|
35 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param bool $critical |
|
29 | + * @param GeneralNames $names |
|
30 | + */ |
|
31 | + public function __construct(bool $critical, GeneralNames $names) |
|
32 | + { |
|
33 | + parent::__construct(self::OID_ISSUER_ALT_NAME, $critical); |
|
34 | + $this->_names = $names; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get names. |
|
39 | - * |
|
40 | - * @return GeneralNames |
|
41 | - */ |
|
42 | - public function names(): GeneralNames |
|
43 | - { |
|
44 | - return $this->_names; |
|
45 | - } |
|
37 | + /** |
|
38 | + * Get names. |
|
39 | + * |
|
40 | + * @return GeneralNames |
|
41 | + */ |
|
42 | + public function names(): GeneralNames |
|
43 | + { |
|
44 | + return $this->_names; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
51 | - { |
|
52 | - return new self($critical, |
|
53 | - GeneralNames::fromASN1( |
|
54 | - UnspecifiedType::fromDER($data)->asSequence())); |
|
55 | - } |
|
47 | + /** |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
51 | + { |
|
52 | + return new self($critical, |
|
53 | + GeneralNames::fromASN1( |
|
54 | + UnspecifiedType::fromDER($data)->asSequence())); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * {@inheritdoc} |
|
59 | - */ |
|
60 | - protected function _valueASN1(): Element |
|
61 | - { |
|
62 | - return $this->_names->toASN1(); |
|
63 | - } |
|
57 | + /** |
|
58 | + * {@inheritdoc} |
|
59 | + */ |
|
60 | + protected function _valueASN1(): Element |
|
61 | + { |
|
62 | + return $this->_names->toASN1(); |
|
63 | + } |
|
64 | 64 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension; |
6 | 6 |
@@ -19,207 +19,207 @@ |
||
19 | 19 | */ |
20 | 20 | class AAControlsExtension extends Extension |
21 | 21 | { |
22 | - /** |
|
23 | - * Path length contraint. |
|
24 | - * |
|
25 | - * @var null|int |
|
26 | - */ |
|
27 | - protected $_pathLenConstraint; |
|
28 | - |
|
29 | - /** |
|
30 | - * Permitted attributes. |
|
31 | - * |
|
32 | - * Array of OID's. |
|
33 | - * |
|
34 | - * @var null|string[] |
|
35 | - */ |
|
36 | - protected $_permittedAttrs; |
|
37 | - |
|
38 | - /** |
|
39 | - * Excluded attributes. |
|
40 | - * |
|
41 | - * Array of OID's. |
|
42 | - * |
|
43 | - * @var null|string[] |
|
44 | - */ |
|
45 | - protected $_excludedAttrs; |
|
46 | - |
|
47 | - /** |
|
48 | - * Whether to permit unspecified attributes. |
|
49 | - * |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - protected $_permitUnSpecified; |
|
53 | - |
|
54 | - /** |
|
55 | - * Constructor. |
|
56 | - * |
|
57 | - * @param bool $critical |
|
58 | - * @param null|int $path_len |
|
59 | - * @param null|string[] $permitted |
|
60 | - * @param null|string[] $excluded |
|
61 | - * @param bool $permit_unspecified |
|
62 | - */ |
|
63 | - public function __construct(bool $critical, ?int $path_len = null, |
|
64 | - ?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true) |
|
65 | - { |
|
66 | - parent::__construct(self::OID_AA_CONTROLS, $critical); |
|
67 | - $this->_pathLenConstraint = $path_len; |
|
68 | - $this->_permittedAttrs = $permitted; |
|
69 | - $this->_excludedAttrs = $excluded; |
|
70 | - $this->_permitUnSpecified = $permit_unspecified; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Check whether path length constraint is present. |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function hasPathLen(): bool |
|
79 | - { |
|
80 | - return isset($this->_pathLenConstraint); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get path length constraint. |
|
85 | - * |
|
86 | - * @throws \LogicException If not set |
|
87 | - * |
|
88 | - * @return int |
|
89 | - */ |
|
90 | - public function pathLen(): int |
|
91 | - { |
|
92 | - if (!$this->hasPathLen()) { |
|
93 | - throw new \LogicException('pathLen not set.'); |
|
94 | - } |
|
95 | - return $this->_pathLenConstraint; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Check whether permitted attributes are present. |
|
100 | - * |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function hasPermittedAttrs(): bool |
|
104 | - { |
|
105 | - return isset($this->_permittedAttrs); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get OID's of permitted attributes. |
|
110 | - * |
|
111 | - * @throws \LogicException If not set |
|
112 | - * |
|
113 | - * @return string[] |
|
114 | - */ |
|
115 | - public function permittedAttrs(): array |
|
116 | - { |
|
117 | - if (!$this->hasPermittedAttrs()) { |
|
118 | - throw new \LogicException('permittedAttrs not set.'); |
|
119 | - } |
|
120 | - return $this->_permittedAttrs; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Check whether excluded attributes are present. |
|
125 | - * |
|
126 | - * @return bool |
|
127 | - */ |
|
128 | - public function hasExcludedAttrs(): bool |
|
129 | - { |
|
130 | - return isset($this->_excludedAttrs); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Get OID's of excluded attributes. |
|
135 | - * |
|
136 | - * @throws \LogicException If not set |
|
137 | - * |
|
138 | - * @return string[] |
|
139 | - */ |
|
140 | - public function excludedAttrs(): array |
|
141 | - { |
|
142 | - if (!$this->hasExcludedAttrs()) { |
|
143 | - throw new \LogicException('excludedAttrs not set.'); |
|
144 | - } |
|
145 | - return $this->_excludedAttrs; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Whether to permit attributes that are not explicitly specified in |
|
150 | - * neither permitted nor excluded list. |
|
151 | - * |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - public function permitUnspecified(): bool |
|
155 | - { |
|
156 | - return $this->_permitUnSpecified; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * {@inheritdoc} |
|
161 | - */ |
|
162 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
163 | - { |
|
164 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
165 | - $path_len = null; |
|
166 | - $permitted = null; |
|
167 | - $excluded = null; |
|
168 | - $permit_unspecified = true; |
|
169 | - $idx = 0; |
|
170 | - if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
171 | - $path_len = $seq->at($idx++)->asInteger()->intNumber(); |
|
172 | - } |
|
173 | - if ($seq->hasTagged(0)) { |
|
174 | - $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
175 | - ->asSequence(); |
|
176 | - $permitted = array_map( |
|
177 | - function (UnspecifiedType $el) { |
|
178 | - return $el->asObjectIdentifier()->oid(); |
|
179 | - }, $attr_seq->elements()); |
|
180 | - ++$idx; |
|
181 | - } |
|
182 | - if ($seq->hasTagged(1)) { |
|
183 | - $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
|
184 | - ->asSequence(); |
|
185 | - $excluded = array_map( |
|
186 | - function (UnspecifiedType $el) { |
|
187 | - return $el->asObjectIdentifier()->oid(); |
|
188 | - }, $attr_seq->elements()); |
|
189 | - ++$idx; |
|
190 | - } |
|
191 | - if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
192 | - $permit_unspecified = $seq->at($idx++)->asBoolean()->value(); |
|
193 | - } |
|
194 | - return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * {@inheritdoc} |
|
199 | - */ |
|
200 | - protected function _valueASN1(): Element |
|
201 | - { |
|
202 | - $elements = []; |
|
203 | - if (isset($this->_pathLenConstraint)) { |
|
204 | - $elements[] = new Integer($this->_pathLenConstraint); |
|
205 | - } |
|
206 | - if (isset($this->_permittedAttrs)) { |
|
207 | - $oids = array_map( |
|
208 | - function ($oid) { |
|
209 | - return new ObjectIdentifier($oid); |
|
210 | - }, $this->_permittedAttrs); |
|
211 | - $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
|
212 | - } |
|
213 | - if (isset($this->_excludedAttrs)) { |
|
214 | - $oids = array_map( |
|
215 | - function ($oid) { |
|
216 | - return new ObjectIdentifier($oid); |
|
217 | - }, $this->_excludedAttrs); |
|
218 | - $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
|
219 | - } |
|
220 | - if (true !== $this->_permitUnSpecified) { |
|
221 | - $elements[] = new Boolean(false); |
|
222 | - } |
|
223 | - return new Sequence(...$elements); |
|
224 | - } |
|
22 | + /** |
|
23 | + * Path length contraint. |
|
24 | + * |
|
25 | + * @var null|int |
|
26 | + */ |
|
27 | + protected $_pathLenConstraint; |
|
28 | + |
|
29 | + /** |
|
30 | + * Permitted attributes. |
|
31 | + * |
|
32 | + * Array of OID's. |
|
33 | + * |
|
34 | + * @var null|string[] |
|
35 | + */ |
|
36 | + protected $_permittedAttrs; |
|
37 | + |
|
38 | + /** |
|
39 | + * Excluded attributes. |
|
40 | + * |
|
41 | + * Array of OID's. |
|
42 | + * |
|
43 | + * @var null|string[] |
|
44 | + */ |
|
45 | + protected $_excludedAttrs; |
|
46 | + |
|
47 | + /** |
|
48 | + * Whether to permit unspecified attributes. |
|
49 | + * |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + protected $_permitUnSpecified; |
|
53 | + |
|
54 | + /** |
|
55 | + * Constructor. |
|
56 | + * |
|
57 | + * @param bool $critical |
|
58 | + * @param null|int $path_len |
|
59 | + * @param null|string[] $permitted |
|
60 | + * @param null|string[] $excluded |
|
61 | + * @param bool $permit_unspecified |
|
62 | + */ |
|
63 | + public function __construct(bool $critical, ?int $path_len = null, |
|
64 | + ?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true) |
|
65 | + { |
|
66 | + parent::__construct(self::OID_AA_CONTROLS, $critical); |
|
67 | + $this->_pathLenConstraint = $path_len; |
|
68 | + $this->_permittedAttrs = $permitted; |
|
69 | + $this->_excludedAttrs = $excluded; |
|
70 | + $this->_permitUnSpecified = $permit_unspecified; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Check whether path length constraint is present. |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function hasPathLen(): bool |
|
79 | + { |
|
80 | + return isset($this->_pathLenConstraint); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get path length constraint. |
|
85 | + * |
|
86 | + * @throws \LogicException If not set |
|
87 | + * |
|
88 | + * @return int |
|
89 | + */ |
|
90 | + public function pathLen(): int |
|
91 | + { |
|
92 | + if (!$this->hasPathLen()) { |
|
93 | + throw new \LogicException('pathLen not set.'); |
|
94 | + } |
|
95 | + return $this->_pathLenConstraint; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Check whether permitted attributes are present. |
|
100 | + * |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function hasPermittedAttrs(): bool |
|
104 | + { |
|
105 | + return isset($this->_permittedAttrs); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get OID's of permitted attributes. |
|
110 | + * |
|
111 | + * @throws \LogicException If not set |
|
112 | + * |
|
113 | + * @return string[] |
|
114 | + */ |
|
115 | + public function permittedAttrs(): array |
|
116 | + { |
|
117 | + if (!$this->hasPermittedAttrs()) { |
|
118 | + throw new \LogicException('permittedAttrs not set.'); |
|
119 | + } |
|
120 | + return $this->_permittedAttrs; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Check whether excluded attributes are present. |
|
125 | + * |
|
126 | + * @return bool |
|
127 | + */ |
|
128 | + public function hasExcludedAttrs(): bool |
|
129 | + { |
|
130 | + return isset($this->_excludedAttrs); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Get OID's of excluded attributes. |
|
135 | + * |
|
136 | + * @throws \LogicException If not set |
|
137 | + * |
|
138 | + * @return string[] |
|
139 | + */ |
|
140 | + public function excludedAttrs(): array |
|
141 | + { |
|
142 | + if (!$this->hasExcludedAttrs()) { |
|
143 | + throw new \LogicException('excludedAttrs not set.'); |
|
144 | + } |
|
145 | + return $this->_excludedAttrs; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Whether to permit attributes that are not explicitly specified in |
|
150 | + * neither permitted nor excluded list. |
|
151 | + * |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + public function permitUnspecified(): bool |
|
155 | + { |
|
156 | + return $this->_permitUnSpecified; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * {@inheritdoc} |
|
161 | + */ |
|
162 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
163 | + { |
|
164 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
165 | + $path_len = null; |
|
166 | + $permitted = null; |
|
167 | + $excluded = null; |
|
168 | + $permit_unspecified = true; |
|
169 | + $idx = 0; |
|
170 | + if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
171 | + $path_len = $seq->at($idx++)->asInteger()->intNumber(); |
|
172 | + } |
|
173 | + if ($seq->hasTagged(0)) { |
|
174 | + $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
175 | + ->asSequence(); |
|
176 | + $permitted = array_map( |
|
177 | + function (UnspecifiedType $el) { |
|
178 | + return $el->asObjectIdentifier()->oid(); |
|
179 | + }, $attr_seq->elements()); |
|
180 | + ++$idx; |
|
181 | + } |
|
182 | + if ($seq->hasTagged(1)) { |
|
183 | + $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
|
184 | + ->asSequence(); |
|
185 | + $excluded = array_map( |
|
186 | + function (UnspecifiedType $el) { |
|
187 | + return $el->asObjectIdentifier()->oid(); |
|
188 | + }, $attr_seq->elements()); |
|
189 | + ++$idx; |
|
190 | + } |
|
191 | + if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
192 | + $permit_unspecified = $seq->at($idx++)->asBoolean()->value(); |
|
193 | + } |
|
194 | + return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * {@inheritdoc} |
|
199 | + */ |
|
200 | + protected function _valueASN1(): Element |
|
201 | + { |
|
202 | + $elements = []; |
|
203 | + if (isset($this->_pathLenConstraint)) { |
|
204 | + $elements[] = new Integer($this->_pathLenConstraint); |
|
205 | + } |
|
206 | + if (isset($this->_permittedAttrs)) { |
|
207 | + $oids = array_map( |
|
208 | + function ($oid) { |
|
209 | + return new ObjectIdentifier($oid); |
|
210 | + }, $this->_permittedAttrs); |
|
211 | + $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
|
212 | + } |
|
213 | + if (isset($this->_excludedAttrs)) { |
|
214 | + $oids = array_map( |
|
215 | + function ($oid) { |
|
216 | + return new ObjectIdentifier($oid); |
|
217 | + }, $this->_excludedAttrs); |
|
218 | + $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
|
219 | + } |
|
220 | + if (true !== $this->_permitUnSpecified) { |
|
221 | + $elements[] = new Boolean(false); |
|
222 | + } |
|
223 | + return new Sequence(...$elements); |
|
224 | + } |
|
225 | 225 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension; |
6 | 6 | |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
175 | 175 | ->asSequence(); |
176 | 176 | $permitted = array_map( |
177 | - function (UnspecifiedType $el) { |
|
177 | + function(UnspecifiedType $el) { |
|
178 | 178 | return $el->asObjectIdentifier()->oid(); |
179 | 179 | }, $attr_seq->elements()); |
180 | 180 | ++$idx; |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
184 | 184 | ->asSequence(); |
185 | 185 | $excluded = array_map( |
186 | - function (UnspecifiedType $el) { |
|
186 | + function(UnspecifiedType $el) { |
|
187 | 187 | return $el->asObjectIdentifier()->oid(); |
188 | 188 | }, $attr_seq->elements()); |
189 | 189 | ++$idx; |
@@ -205,14 +205,14 @@ discard block |
||
205 | 205 | } |
206 | 206 | if (isset($this->_permittedAttrs)) { |
207 | 207 | $oids = array_map( |
208 | - function ($oid) { |
|
208 | + function($oid) { |
|
209 | 209 | return new ObjectIdentifier($oid); |
210 | 210 | }, $this->_permittedAttrs); |
211 | 211 | $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
212 | 212 | } |
213 | 213 | if (isset($this->_excludedAttrs)) { |
214 | 214 | $oids = array_map( |
215 | - function ($oid) { |
|
215 | + function($oid) { |
|
216 | 216 | return new ObjectIdentifier($oid); |
217 | 217 | }, $this->_excludedAttrs); |
218 | 218 | $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
@@ -17,99 +17,99 @@ |
||
17 | 17 | */ |
18 | 18 | class BasicConstraintsExtension extends Extension |
19 | 19 | { |
20 | - /** |
|
21 | - * Whether certificate is a CA. |
|
22 | - * |
|
23 | - * @var bool |
|
24 | - */ |
|
25 | - protected $_ca; |
|
20 | + /** |
|
21 | + * Whether certificate is a CA. |
|
22 | + * |
|
23 | + * @var bool |
|
24 | + */ |
|
25 | + protected $_ca; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Maximum certification path length. |
|
29 | - * |
|
30 | - * @var null|int |
|
31 | - */ |
|
32 | - protected $_pathLen; |
|
27 | + /** |
|
28 | + * Maximum certification path length. |
|
29 | + * |
|
30 | + * @var null|int |
|
31 | + */ |
|
32 | + protected $_pathLen; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @param bool $critical |
|
38 | - * @param bool $ca |
|
39 | - * @param null|int $path_len |
|
40 | - */ |
|
41 | - public function __construct(bool $critical, bool $ca, ?int $path_len = null) |
|
42 | - { |
|
43 | - parent::__construct(self::OID_BASIC_CONSTRAINTS, $critical); |
|
44 | - $this->_ca = $ca; |
|
45 | - $this->_pathLen = $path_len; |
|
46 | - } |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @param bool $critical |
|
38 | + * @param bool $ca |
|
39 | + * @param null|int $path_len |
|
40 | + */ |
|
41 | + public function __construct(bool $critical, bool $ca, ?int $path_len = null) |
|
42 | + { |
|
43 | + parent::__construct(self::OID_BASIC_CONSTRAINTS, $critical); |
|
44 | + $this->_ca = $ca; |
|
45 | + $this->_pathLen = $path_len; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Whether certificate is a CA. |
|
50 | - * |
|
51 | - * @return bool |
|
52 | - */ |
|
53 | - public function isCA(): bool |
|
54 | - { |
|
55 | - return $this->_ca; |
|
56 | - } |
|
48 | + /** |
|
49 | + * Whether certificate is a CA. |
|
50 | + * |
|
51 | + * @return bool |
|
52 | + */ |
|
53 | + public function isCA(): bool |
|
54 | + { |
|
55 | + return $this->_ca; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Whether path length is present. |
|
60 | - * |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function hasPathLen(): bool |
|
64 | - { |
|
65 | - return isset($this->_pathLen); |
|
66 | - } |
|
58 | + /** |
|
59 | + * Whether path length is present. |
|
60 | + * |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function hasPathLen(): bool |
|
64 | + { |
|
65 | + return isset($this->_pathLen); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get path length. |
|
70 | - * |
|
71 | - * @throws \LogicException If not set |
|
72 | - * |
|
73 | - * @return int |
|
74 | - */ |
|
75 | - public function pathLen(): int |
|
76 | - { |
|
77 | - if (!$this->hasPathLen()) { |
|
78 | - throw new \LogicException('pathLenConstraint not set.'); |
|
79 | - } |
|
80 | - return $this->_pathLen; |
|
81 | - } |
|
68 | + /** |
|
69 | + * Get path length. |
|
70 | + * |
|
71 | + * @throws \LogicException If not set |
|
72 | + * |
|
73 | + * @return int |
|
74 | + */ |
|
75 | + public function pathLen(): int |
|
76 | + { |
|
77 | + if (!$this->hasPathLen()) { |
|
78 | + throw new \LogicException('pathLenConstraint not set.'); |
|
79 | + } |
|
80 | + return $this->_pathLen; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * {@inheritdoc} |
|
85 | - */ |
|
86 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
87 | - { |
|
88 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
89 | - $ca = false; |
|
90 | - $path_len = null; |
|
91 | - $idx = 0; |
|
92 | - if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
93 | - $ca = $seq->at($idx++)->asBoolean()->value(); |
|
94 | - } |
|
95 | - if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
96 | - $path_len = $seq->at($idx)->asInteger()->intNumber(); |
|
97 | - } |
|
98 | - return new self($critical, $ca, $path_len); |
|
99 | - } |
|
83 | + /** |
|
84 | + * {@inheritdoc} |
|
85 | + */ |
|
86 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
87 | + { |
|
88 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
89 | + $ca = false; |
|
90 | + $path_len = null; |
|
91 | + $idx = 0; |
|
92 | + if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
93 | + $ca = $seq->at($idx++)->asBoolean()->value(); |
|
94 | + } |
|
95 | + if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
96 | + $path_len = $seq->at($idx)->asInteger()->intNumber(); |
|
97 | + } |
|
98 | + return new self($critical, $ca, $path_len); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * {@inheritdoc} |
|
103 | - */ |
|
104 | - protected function _valueASN1(): Element |
|
105 | - { |
|
106 | - $elements = []; |
|
107 | - if ($this->_ca) { |
|
108 | - $elements[] = new Boolean(true); |
|
109 | - } |
|
110 | - if (isset($this->_pathLen)) { |
|
111 | - $elements[] = new Integer($this->_pathLen); |
|
112 | - } |
|
113 | - return new Sequence(...$elements); |
|
114 | - } |
|
101 | + /** |
|
102 | + * {@inheritdoc} |
|
103 | + */ |
|
104 | + protected function _valueASN1(): Element |
|
105 | + { |
|
106 | + $elements = []; |
|
107 | + if ($this->_ca) { |
|
108 | + $elements[] = new Boolean(true); |
|
109 | + } |
|
110 | + if (isset($this->_pathLen)) { |
|
111 | + $elements[] = new Integer($this->_pathLen); |
|
112 | + } |
|
113 | + return new Sequence(...$elements); |
|
114 | + } |
|
115 | 115 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension; |
6 | 6 |
@@ -12,215 +12,215 @@ |
||
12 | 12 | */ |
13 | 13 | class CertificateBundle implements \Countable, \IteratorAggregate |
14 | 14 | { |
15 | - /** |
|
16 | - * Certificates. |
|
17 | - * |
|
18 | - * @var Certificate[] |
|
19 | - */ |
|
20 | - protected $_certs; |
|
21 | - |
|
22 | - /** |
|
23 | - * Mapping from public key id to array of certificates. |
|
24 | - * |
|
25 | - * @var null|(Certificate[])[] |
|
26 | - */ |
|
27 | - private $_keyIdMap; |
|
28 | - |
|
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param Certificate ...$certs Certificate objects |
|
33 | - */ |
|
34 | - public function __construct(Certificate ...$certs) |
|
35 | - { |
|
36 | - $this->_certs = $certs; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Reset internal cached variables on clone. |
|
41 | - */ |
|
42 | - public function __clone() |
|
43 | - { |
|
44 | - $this->_keyIdMap = null; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Initialize from PEMs. |
|
49 | - * |
|
50 | - * @param PEM ...$pems PEM objects |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public static function fromPEMs(PEM ...$pems): self |
|
55 | - { |
|
56 | - $certs = array_map( |
|
57 | - function ($pem) { |
|
58 | - return Certificate::fromPEM($pem); |
|
59 | - }, $pems); |
|
60 | - return new self(...$certs); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Initialize from PEM bundle. |
|
65 | - * |
|
66 | - * @param PEMBundle $pem_bundle |
|
67 | - * |
|
68 | - * @return self |
|
69 | - */ |
|
70 | - public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
71 | - { |
|
72 | - return self::fromPEMs(...$pem_bundle->all()); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Get self with certificates added. |
|
77 | - * |
|
78 | - * @param Certificate ...$cert |
|
79 | - * |
|
80 | - * @return self |
|
81 | - */ |
|
82 | - public function withCertificates(Certificate ...$cert): self |
|
83 | - { |
|
84 | - $obj = clone $this; |
|
85 | - $obj->_certs = array_merge($obj->_certs, $cert); |
|
86 | - return $obj; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get self with certificates from PEMBundle added. |
|
91 | - * |
|
92 | - * @param PEMBundle $pem_bundle |
|
93 | - * |
|
94 | - * @return self |
|
95 | - */ |
|
96 | - public function withPEMBundle(PEMBundle $pem_bundle): self |
|
97 | - { |
|
98 | - $certs = $this->_certs; |
|
99 | - foreach ($pem_bundle as $pem) { |
|
100 | - $certs[] = Certificate::fromPEM($pem); |
|
101 | - } |
|
102 | - return new self(...$certs); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get self with single certificate from PEM added. |
|
107 | - * |
|
108 | - * @param PEM $pem |
|
109 | - * |
|
110 | - * @return self |
|
111 | - */ |
|
112 | - public function withPEM(PEM $pem): self |
|
113 | - { |
|
114 | - $certs = $this->_certs; |
|
115 | - $certs[] = Certificate::fromPEM($pem); |
|
116 | - return new self(...$certs); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Check whether bundle contains a given certificate. |
|
121 | - * |
|
122 | - * @param Certificate $cert |
|
123 | - * |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function contains(Certificate $cert): bool |
|
127 | - { |
|
128 | - $id = self::_getCertKeyId($cert); |
|
129 | - $map = $this->_getKeyIdMap(); |
|
130 | - if (!isset($map[$id])) { |
|
131 | - return false; |
|
132 | - } |
|
133 | - foreach ($map[$id] as $c) { |
|
134 | - /** @var Certificate $c */ |
|
135 | - if ($cert->equals($c)) { |
|
136 | - return true; |
|
137 | - } |
|
138 | - } |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Get all certificates that have given subject key identifier. |
|
144 | - * |
|
145 | - * @param string $id |
|
146 | - * |
|
147 | - * @return Certificate[] |
|
148 | - */ |
|
149 | - public function allBySubjectKeyIdentifier(string $id): array |
|
150 | - { |
|
151 | - $map = $this->_getKeyIdMap(); |
|
152 | - if (!isset($map[$id])) { |
|
153 | - return []; |
|
154 | - } |
|
155 | - return $map[$id]; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Get all certificates in a bundle. |
|
160 | - * |
|
161 | - * @return Certificate[] |
|
162 | - */ |
|
163 | - public function all(): array |
|
164 | - { |
|
165 | - return $this->_certs; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @see \Countable::count() |
|
170 | - * |
|
171 | - * @return int |
|
172 | - */ |
|
173 | - public function count(): int |
|
174 | - { |
|
175 | - return count($this->_certs); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Get iterator for certificates. |
|
180 | - * |
|
181 | - * @see \IteratorAggregate::getIterator() |
|
182 | - * |
|
183 | - * @return \ArrayIterator |
|
184 | - */ |
|
185 | - public function getIterator(): \ArrayIterator |
|
186 | - { |
|
187 | - return new \ArrayIterator($this->_certs); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get certificate mapping by public key id. |
|
192 | - * |
|
193 | - * @return (Certificate[])[] |
|
194 | - */ |
|
195 | - private function _getKeyIdMap(): array |
|
196 | - { |
|
197 | - // lazily build mapping |
|
198 | - if (!isset($this->_keyIdMap)) { |
|
199 | - $this->_keyIdMap = []; |
|
200 | - foreach ($this->_certs as $cert) { |
|
201 | - $id = self::_getCertKeyId($cert); |
|
202 | - if (!isset($this->_keyIdMap[$id])) { |
|
203 | - $this->_keyIdMap[$id] = []; |
|
204 | - } |
|
205 | - array_push($this->_keyIdMap[$id], $cert); |
|
206 | - } |
|
207 | - } |
|
208 | - return $this->_keyIdMap; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get public key id for the certificate. |
|
213 | - * |
|
214 | - * @param Certificate $cert |
|
215 | - * |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - private static function _getCertKeyId(Certificate $cert): string |
|
219 | - { |
|
220 | - $exts = $cert->tbsCertificate()->extensions(); |
|
221 | - if ($exts->hasSubjectKeyIdentifier()) { |
|
222 | - return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
223 | - } |
|
224 | - return $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
225 | - } |
|
15 | + /** |
|
16 | + * Certificates. |
|
17 | + * |
|
18 | + * @var Certificate[] |
|
19 | + */ |
|
20 | + protected $_certs; |
|
21 | + |
|
22 | + /** |
|
23 | + * Mapping from public key id to array of certificates. |
|
24 | + * |
|
25 | + * @var null|(Certificate[])[] |
|
26 | + */ |
|
27 | + private $_keyIdMap; |
|
28 | + |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param Certificate ...$certs Certificate objects |
|
33 | + */ |
|
34 | + public function __construct(Certificate ...$certs) |
|
35 | + { |
|
36 | + $this->_certs = $certs; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Reset internal cached variables on clone. |
|
41 | + */ |
|
42 | + public function __clone() |
|
43 | + { |
|
44 | + $this->_keyIdMap = null; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Initialize from PEMs. |
|
49 | + * |
|
50 | + * @param PEM ...$pems PEM objects |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public static function fromPEMs(PEM ...$pems): self |
|
55 | + { |
|
56 | + $certs = array_map( |
|
57 | + function ($pem) { |
|
58 | + return Certificate::fromPEM($pem); |
|
59 | + }, $pems); |
|
60 | + return new self(...$certs); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Initialize from PEM bundle. |
|
65 | + * |
|
66 | + * @param PEMBundle $pem_bundle |
|
67 | + * |
|
68 | + * @return self |
|
69 | + */ |
|
70 | + public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
71 | + { |
|
72 | + return self::fromPEMs(...$pem_bundle->all()); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Get self with certificates added. |
|
77 | + * |
|
78 | + * @param Certificate ...$cert |
|
79 | + * |
|
80 | + * @return self |
|
81 | + */ |
|
82 | + public function withCertificates(Certificate ...$cert): self |
|
83 | + { |
|
84 | + $obj = clone $this; |
|
85 | + $obj->_certs = array_merge($obj->_certs, $cert); |
|
86 | + return $obj; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get self with certificates from PEMBundle added. |
|
91 | + * |
|
92 | + * @param PEMBundle $pem_bundle |
|
93 | + * |
|
94 | + * @return self |
|
95 | + */ |
|
96 | + public function withPEMBundle(PEMBundle $pem_bundle): self |
|
97 | + { |
|
98 | + $certs = $this->_certs; |
|
99 | + foreach ($pem_bundle as $pem) { |
|
100 | + $certs[] = Certificate::fromPEM($pem); |
|
101 | + } |
|
102 | + return new self(...$certs); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get self with single certificate from PEM added. |
|
107 | + * |
|
108 | + * @param PEM $pem |
|
109 | + * |
|
110 | + * @return self |
|
111 | + */ |
|
112 | + public function withPEM(PEM $pem): self |
|
113 | + { |
|
114 | + $certs = $this->_certs; |
|
115 | + $certs[] = Certificate::fromPEM($pem); |
|
116 | + return new self(...$certs); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Check whether bundle contains a given certificate. |
|
121 | + * |
|
122 | + * @param Certificate $cert |
|
123 | + * |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function contains(Certificate $cert): bool |
|
127 | + { |
|
128 | + $id = self::_getCertKeyId($cert); |
|
129 | + $map = $this->_getKeyIdMap(); |
|
130 | + if (!isset($map[$id])) { |
|
131 | + return false; |
|
132 | + } |
|
133 | + foreach ($map[$id] as $c) { |
|
134 | + /** @var Certificate $c */ |
|
135 | + if ($cert->equals($c)) { |
|
136 | + return true; |
|
137 | + } |
|
138 | + } |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Get all certificates that have given subject key identifier. |
|
144 | + * |
|
145 | + * @param string $id |
|
146 | + * |
|
147 | + * @return Certificate[] |
|
148 | + */ |
|
149 | + public function allBySubjectKeyIdentifier(string $id): array |
|
150 | + { |
|
151 | + $map = $this->_getKeyIdMap(); |
|
152 | + if (!isset($map[$id])) { |
|
153 | + return []; |
|
154 | + } |
|
155 | + return $map[$id]; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Get all certificates in a bundle. |
|
160 | + * |
|
161 | + * @return Certificate[] |
|
162 | + */ |
|
163 | + public function all(): array |
|
164 | + { |
|
165 | + return $this->_certs; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @see \Countable::count() |
|
170 | + * |
|
171 | + * @return int |
|
172 | + */ |
|
173 | + public function count(): int |
|
174 | + { |
|
175 | + return count($this->_certs); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Get iterator for certificates. |
|
180 | + * |
|
181 | + * @see \IteratorAggregate::getIterator() |
|
182 | + * |
|
183 | + * @return \ArrayIterator |
|
184 | + */ |
|
185 | + public function getIterator(): \ArrayIterator |
|
186 | + { |
|
187 | + return new \ArrayIterator($this->_certs); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get certificate mapping by public key id. |
|
192 | + * |
|
193 | + * @return (Certificate[])[] |
|
194 | + */ |
|
195 | + private function _getKeyIdMap(): array |
|
196 | + { |
|
197 | + // lazily build mapping |
|
198 | + if (!isset($this->_keyIdMap)) { |
|
199 | + $this->_keyIdMap = []; |
|
200 | + foreach ($this->_certs as $cert) { |
|
201 | + $id = self::_getCertKeyId($cert); |
|
202 | + if (!isset($this->_keyIdMap[$id])) { |
|
203 | + $this->_keyIdMap[$id] = []; |
|
204 | + } |
|
205 | + array_push($this->_keyIdMap[$id], $cert); |
|
206 | + } |
|
207 | + } |
|
208 | + return $this->_keyIdMap; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get public key id for the certificate. |
|
213 | + * |
|
214 | + * @param Certificate $cert |
|
215 | + * |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + private static function _getCertKeyId(Certificate $cert): string |
|
219 | + { |
|
220 | + $exts = $cert->tbsCertificate()->extensions(); |
|
221 | + if ($exts->hasSubjectKeyIdentifier()) { |
|
222 | + return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
223 | + } |
|
224 | + return $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
225 | + } |
|
226 | 226 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | public static function fromPEMs(PEM ...$pems): self |
55 | 55 | { |
56 | 56 | $certs = array_map( |
57 | - function ($pem) { |
|
57 | + function($pem) { |
|
58 | 58 | return Certificate::fromPEM($pem); |
59 | 59 | }, $pems); |
60 | 60 | return new self(...$certs); |
@@ -13,136 +13,136 @@ |
||
13 | 13 | */ |
14 | 14 | class CertificateChain implements \Countable, \IteratorAggregate |
15 | 15 | { |
16 | - /** |
|
17 | - * List of certificates in a chain. |
|
18 | - * |
|
19 | - * @var Certificate[] |
|
20 | - */ |
|
21 | - protected $_certs; |
|
16 | + /** |
|
17 | + * List of certificates in a chain. |
|
18 | + * |
|
19 | + * @var Certificate[] |
|
20 | + */ |
|
21 | + protected $_certs; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - * |
|
26 | - * @param Certificate ...$certs List of certificates, end-entity first |
|
27 | - */ |
|
28 | - public function __construct(Certificate ...$certs) |
|
29 | - { |
|
30 | - $this->_certs = $certs; |
|
31 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + * |
|
26 | + * @param Certificate ...$certs List of certificates, end-entity first |
|
27 | + */ |
|
28 | + public function __construct(Certificate ...$certs) |
|
29 | + { |
|
30 | + $this->_certs = $certs; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Initialize from a list of PEMs. |
|
35 | - * |
|
36 | - * @param PEM ...$pems |
|
37 | - * |
|
38 | - * @return self |
|
39 | - */ |
|
40 | - public static function fromPEMs(PEM ...$pems): self |
|
41 | - { |
|
42 | - $certs = array_map( |
|
43 | - function (PEM $pem) { |
|
44 | - return Certificate::fromPEM($pem); |
|
45 | - }, $pems); |
|
46 | - return new self(...$certs); |
|
47 | - } |
|
33 | + /** |
|
34 | + * Initialize from a list of PEMs. |
|
35 | + * |
|
36 | + * @param PEM ...$pems |
|
37 | + * |
|
38 | + * @return self |
|
39 | + */ |
|
40 | + public static function fromPEMs(PEM ...$pems): self |
|
41 | + { |
|
42 | + $certs = array_map( |
|
43 | + function (PEM $pem) { |
|
44 | + return Certificate::fromPEM($pem); |
|
45 | + }, $pems); |
|
46 | + return new self(...$certs); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Initialize from a string containing multiple PEM blocks. |
|
51 | - * |
|
52 | - * @param string $str |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public static function fromPEMString(string $str): self |
|
57 | - { |
|
58 | - $pems = PEMBundle::fromString($str)->all(); |
|
59 | - return self::fromPEMs(...$pems); |
|
60 | - } |
|
49 | + /** |
|
50 | + * Initialize from a string containing multiple PEM blocks. |
|
51 | + * |
|
52 | + * @param string $str |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public static function fromPEMString(string $str): self |
|
57 | + { |
|
58 | + $pems = PEMBundle::fromString($str)->all(); |
|
59 | + return self::fromPEMs(...$pems); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get all certificates in a chain ordered from the end-entity certificate |
|
64 | - * to the trust anchor. |
|
65 | - * |
|
66 | - * @return Certificate[] |
|
67 | - */ |
|
68 | - public function certificates(): array |
|
69 | - { |
|
70 | - return $this->_certs; |
|
71 | - } |
|
62 | + /** |
|
63 | + * Get all certificates in a chain ordered from the end-entity certificate |
|
64 | + * to the trust anchor. |
|
65 | + * |
|
66 | + * @return Certificate[] |
|
67 | + */ |
|
68 | + public function certificates(): array |
|
69 | + { |
|
70 | + return $this->_certs; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get the end-entity certificate. |
|
75 | - * |
|
76 | - * @throws \LogicException |
|
77 | - * |
|
78 | - * @return Certificate |
|
79 | - */ |
|
80 | - public function endEntityCertificate(): Certificate |
|
81 | - { |
|
82 | - if (!count($this->_certs)) { |
|
83 | - throw new \LogicException('No certificates.'); |
|
84 | - } |
|
85 | - return $this->_certs[0]; |
|
86 | - } |
|
73 | + /** |
|
74 | + * Get the end-entity certificate. |
|
75 | + * |
|
76 | + * @throws \LogicException |
|
77 | + * |
|
78 | + * @return Certificate |
|
79 | + */ |
|
80 | + public function endEntityCertificate(): Certificate |
|
81 | + { |
|
82 | + if (!count($this->_certs)) { |
|
83 | + throw new \LogicException('No certificates.'); |
|
84 | + } |
|
85 | + return $this->_certs[0]; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the trust anchor certificate. |
|
90 | - * |
|
91 | - * @throws \LogicException |
|
92 | - * |
|
93 | - * @return Certificate |
|
94 | - */ |
|
95 | - public function trustAnchorCertificate(): Certificate |
|
96 | - { |
|
97 | - if (!count($this->_certs)) { |
|
98 | - throw new \LogicException('No certificates.'); |
|
99 | - } |
|
100 | - return $this->_certs[count($this->_certs) - 1]; |
|
101 | - } |
|
88 | + /** |
|
89 | + * Get the trust anchor certificate. |
|
90 | + * |
|
91 | + * @throws \LogicException |
|
92 | + * |
|
93 | + * @return Certificate |
|
94 | + */ |
|
95 | + public function trustAnchorCertificate(): Certificate |
|
96 | + { |
|
97 | + if (!count($this->_certs)) { |
|
98 | + throw new \LogicException('No certificates.'); |
|
99 | + } |
|
100 | + return $this->_certs[count($this->_certs) - 1]; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Convert certificate chain to certification path. |
|
105 | - * |
|
106 | - * @return CertificationPath |
|
107 | - */ |
|
108 | - public function certificationPath(): CertificationPath |
|
109 | - { |
|
110 | - return CertificationPath::fromCertificateChain($this); |
|
111 | - } |
|
103 | + /** |
|
104 | + * Convert certificate chain to certification path. |
|
105 | + * |
|
106 | + * @return CertificationPath |
|
107 | + */ |
|
108 | + public function certificationPath(): CertificationPath |
|
109 | + { |
|
110 | + return CertificationPath::fromCertificateChain($this); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Convert certificate chain to string of PEM blocks. |
|
115 | - * |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public function toPEMString(): string |
|
119 | - { |
|
120 | - return implode("\n", |
|
121 | - array_map( |
|
122 | - function (Certificate $cert) { |
|
123 | - return $cert->toPEM()->string(); |
|
124 | - }, $this->_certs)); |
|
125 | - } |
|
113 | + /** |
|
114 | + * Convert certificate chain to string of PEM blocks. |
|
115 | + * |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public function toPEMString(): string |
|
119 | + { |
|
120 | + return implode("\n", |
|
121 | + array_map( |
|
122 | + function (Certificate $cert) { |
|
123 | + return $cert->toPEM()->string(); |
|
124 | + }, $this->_certs)); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @see \Countable::count() |
|
129 | - * |
|
130 | - * @return int |
|
131 | - */ |
|
132 | - public function count(): int |
|
133 | - { |
|
134 | - return count($this->_certs); |
|
135 | - } |
|
127 | + /** |
|
128 | + * @see \Countable::count() |
|
129 | + * |
|
130 | + * @return int |
|
131 | + */ |
|
132 | + public function count(): int |
|
133 | + { |
|
134 | + return count($this->_certs); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Get iterator for certificates. |
|
139 | - * |
|
140 | - * @see \IteratorAggregate::getIterator() |
|
141 | - * |
|
142 | - * @return \ArrayIterator |
|
143 | - */ |
|
144 | - public function getIterator(): \ArrayIterator |
|
145 | - { |
|
146 | - return new \ArrayIterator($this->_certs); |
|
147 | - } |
|
137 | + /** |
|
138 | + * Get iterator for certificates. |
|
139 | + * |
|
140 | + * @see \IteratorAggregate::getIterator() |
|
141 | + * |
|
142 | + * @return \ArrayIterator |
|
143 | + */ |
|
144 | + public function getIterator(): \ArrayIterator |
|
145 | + { |
|
146 | + return new \ArrayIterator($this->_certs); |
|
147 | + } |
|
148 | 148 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 | |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | public static function fromPEMs(PEM ...$pems): self |
41 | 41 | { |
42 | 42 | $certs = array_map( |
43 | - function (PEM $pem) { |
|
43 | + function(PEM $pem) { |
|
44 | 44 | return Certificate::fromPEM($pem); |
45 | 45 | }, $pems); |
46 | 46 | return new self(...$certs); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | { |
120 | 120 | return implode("\n", |
121 | 121 | array_map( |
122 | - function (Certificate $cert) { |
|
122 | + function(Certificate $cert) { |
|
123 | 123 | return $cert->toPEM()->string(); |
124 | 124 | }, $this->_certs)); |
125 | 125 | } |
@@ -16,139 +16,139 @@ |
||
16 | 16 | */ |
17 | 17 | class CertificationPathBuilder |
18 | 18 | { |
19 | - /** |
|
20 | - * Trust anchors. |
|
21 | - * |
|
22 | - * @var CertificateBundle |
|
23 | - */ |
|
24 | - protected $_trustList; |
|
19 | + /** |
|
20 | + * Trust anchors. |
|
21 | + * |
|
22 | + * @var CertificateBundle |
|
23 | + */ |
|
24 | + protected $_trustList; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
30 | - */ |
|
31 | - public function __construct(CertificateBundle $trust_list) |
|
32 | - { |
|
33 | - $this->_trustList = $trust_list; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
30 | + */ |
|
31 | + public function __construct(CertificateBundle $trust_list) |
|
32 | + { |
|
33 | + $this->_trustList = $trust_list; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get all certification paths to given target certificate from |
|
38 | - * any trust anchor. |
|
39 | - * |
|
40 | - * @param Certificate $target Target certificate |
|
41 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
42 | - * |
|
43 | - * @return CertificationPath[] |
|
44 | - */ |
|
45 | - public function allPathsToTarget(Certificate $target, |
|
46 | - ?CertificateBundle $intermediate = null): array |
|
47 | - { |
|
48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | - // map paths to CertificationPath objects |
|
50 | - return array_map( |
|
51 | - function ($certs) { |
|
52 | - return new CertificationPath(...$certs); |
|
53 | - }, $paths); |
|
54 | - } |
|
36 | + /** |
|
37 | + * Get all certification paths to given target certificate from |
|
38 | + * any trust anchor. |
|
39 | + * |
|
40 | + * @param Certificate $target Target certificate |
|
41 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
42 | + * |
|
43 | + * @return CertificationPath[] |
|
44 | + */ |
|
45 | + public function allPathsToTarget(Certificate $target, |
|
46 | + ?CertificateBundle $intermediate = null): array |
|
47 | + { |
|
48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | + // map paths to CertificationPath objects |
|
50 | + return array_map( |
|
51 | + function ($certs) { |
|
52 | + return new CertificationPath(...$certs); |
|
53 | + }, $paths); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get shortest path to given target certificate from any trust anchor. |
|
58 | - * |
|
59 | - * @param Certificate $target Target certificate |
|
60 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
61 | - * |
|
62 | - * @throws PathBuildingException |
|
63 | - * |
|
64 | - * @return CertificationPath |
|
65 | - */ |
|
66 | - public function shortestPathToTarget(Certificate $target, |
|
67 | - ?CertificateBundle $intermediate = null): CertificationPath |
|
68 | - { |
|
69 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
70 | - if (!count($paths)) { |
|
71 | - throw new PathBuildingException('No certification paths.'); |
|
72 | - } |
|
73 | - usort($paths, |
|
74 | - function ($a, $b) { |
|
75 | - return count($a) < count($b) ? -1 : 1; |
|
76 | - }); |
|
77 | - return reset($paths); |
|
78 | - } |
|
56 | + /** |
|
57 | + * Get shortest path to given target certificate from any trust anchor. |
|
58 | + * |
|
59 | + * @param Certificate $target Target certificate |
|
60 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
61 | + * |
|
62 | + * @throws PathBuildingException |
|
63 | + * |
|
64 | + * @return CertificationPath |
|
65 | + */ |
|
66 | + public function shortestPathToTarget(Certificate $target, |
|
67 | + ?CertificateBundle $intermediate = null): CertificationPath |
|
68 | + { |
|
69 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
70 | + if (!count($paths)) { |
|
71 | + throw new PathBuildingException('No certification paths.'); |
|
72 | + } |
|
73 | + usort($paths, |
|
74 | + function ($a, $b) { |
|
75 | + return count($a) < count($b) ? -1 : 1; |
|
76 | + }); |
|
77 | + return reset($paths); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Find all issuers of the target certificate from a given bundle. |
|
82 | - * |
|
83 | - * @param Certificate $target Target certificate |
|
84 | - * @param CertificateBundle $bundle Certificates to search |
|
85 | - * |
|
86 | - * @return Certificate[] |
|
87 | - */ |
|
88 | - protected function _findIssuers(Certificate $target, |
|
89 | - CertificateBundle $bundle): array |
|
90 | - { |
|
91 | - $issuers = []; |
|
92 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
93 | - $extensions = $target->tbsCertificate()->extensions(); |
|
94 | - // find by authority key identifier |
|
95 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
96 | - $ext = $extensions->authorityKeyIdentifier(); |
|
97 | - if ($ext->hasKeyIdentifier()) { |
|
98 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
99 | - $ext->keyIdentifier()) as $issuer) { |
|
100 | - // check that issuer name matches |
|
101 | - if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
102 | - $issuers[] = $issuer; |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
107 | - return $issuers; |
|
108 | - } |
|
80 | + /** |
|
81 | + * Find all issuers of the target certificate from a given bundle. |
|
82 | + * |
|
83 | + * @param Certificate $target Target certificate |
|
84 | + * @param CertificateBundle $bundle Certificates to search |
|
85 | + * |
|
86 | + * @return Certificate[] |
|
87 | + */ |
|
88 | + protected function _findIssuers(Certificate $target, |
|
89 | + CertificateBundle $bundle): array |
|
90 | + { |
|
91 | + $issuers = []; |
|
92 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
93 | + $extensions = $target->tbsCertificate()->extensions(); |
|
94 | + // find by authority key identifier |
|
95 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
96 | + $ext = $extensions->authorityKeyIdentifier(); |
|
97 | + if ($ext->hasKeyIdentifier()) { |
|
98 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
99 | + $ext->keyIdentifier()) as $issuer) { |
|
100 | + // check that issuer name matches |
|
101 | + if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
102 | + $issuers[] = $issuer; |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | + return $issuers; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Resolve all possible certification paths from any trust anchor to |
|
112 | - * the target certificate, using optional intermediate certificates. |
|
113 | - * |
|
114 | - * Helper method for allPathsToTarget to be called recursively. |
|
115 | - * |
|
116 | - * @todo Implement loop detection |
|
117 | - * |
|
118 | - * @param Certificate $target |
|
119 | - * @param null|CertificateBundle $intermediate |
|
120 | - * |
|
121 | - * @return array[] Array of arrays containing path certificates |
|
122 | - */ |
|
123 | - private function _resolvePathsToTarget(Certificate $target, |
|
124 | - ?CertificateBundle $intermediate = null): array |
|
125 | - { |
|
126 | - // array of possible paths |
|
127 | - $paths = []; |
|
128 | - // signed by certificate in the trust list |
|
129 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
130 | - // if target is self-signed, path consists of only |
|
131 | - // the target certificate |
|
132 | - if ($target->equals($issuer)) { |
|
133 | - $paths[] = [$target]; |
|
134 | - } else { |
|
135 | - $paths[] = [$issuer, $target]; |
|
136 | - } |
|
137 | - } |
|
138 | - if (isset($intermediate)) { |
|
139 | - // signed by intermediate certificate |
|
140 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
141 | - // intermediate certificate must not be self-signed |
|
142 | - if ($issuer->isSelfIssued()) { |
|
143 | - continue; |
|
144 | - } |
|
145 | - // resolve paths to issuer |
|
146 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
147 | - foreach ($subpaths as $path) { |
|
148 | - $paths[] = array_merge($path, [$target]); |
|
149 | - } |
|
150 | - } |
|
151 | - } |
|
152 | - return $paths; |
|
153 | - } |
|
110 | + /** |
|
111 | + * Resolve all possible certification paths from any trust anchor to |
|
112 | + * the target certificate, using optional intermediate certificates. |
|
113 | + * |
|
114 | + * Helper method for allPathsToTarget to be called recursively. |
|
115 | + * |
|
116 | + * @todo Implement loop detection |
|
117 | + * |
|
118 | + * @param Certificate $target |
|
119 | + * @param null|CertificateBundle $intermediate |
|
120 | + * |
|
121 | + * @return array[] Array of arrays containing path certificates |
|
122 | + */ |
|
123 | + private function _resolvePathsToTarget(Certificate $target, |
|
124 | + ?CertificateBundle $intermediate = null): array |
|
125 | + { |
|
126 | + // array of possible paths |
|
127 | + $paths = []; |
|
128 | + // signed by certificate in the trust list |
|
129 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
130 | + // if target is self-signed, path consists of only |
|
131 | + // the target certificate |
|
132 | + if ($target->equals($issuer)) { |
|
133 | + $paths[] = [$target]; |
|
134 | + } else { |
|
135 | + $paths[] = [$issuer, $target]; |
|
136 | + } |
|
137 | + } |
|
138 | + if (isset($intermediate)) { |
|
139 | + // signed by intermediate certificate |
|
140 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
141 | + // intermediate certificate must not be self-signed |
|
142 | + if ($issuer->isSelfIssued()) { |
|
143 | + continue; |
|
144 | + } |
|
145 | + // resolve paths to issuer |
|
146 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
147 | + foreach ($subpaths as $path) { |
|
148 | + $paths[] = array_merge($path, [$target]); |
|
149 | + } |
|
150 | + } |
|
151 | + } |
|
152 | + return $paths; |
|
153 | + } |
|
154 | 154 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathBuilding; |
6 | 6 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $paths = $this->_resolvePathsToTarget($target, $intermediate); |
49 | 49 | // map paths to CertificationPath objects |
50 | 50 | return array_map( |
51 | - function ($certs) { |
|
51 | + function($certs) { |
|
52 | 52 | return new CertificationPath(...$certs); |
53 | 53 | }, $paths); |
54 | 54 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | throw new PathBuildingException('No certification paths.'); |
72 | 72 | } |
73 | 73 | usort($paths, |
74 | - function ($a, $b) { |
|
74 | + function($a, $b) { |
|
75 | 75 | return count($a) < count($b) ? -1 : 1; |
76 | 76 | }); |
77 | 77 | return reset($paths); |