@@ -7,67 +7,67 @@ |
||
7 | 7 | */ |
8 | 8 | trait DateTimeHelper |
9 | 9 | { |
10 | - /** |
|
11 | - * Create DateTime object from time string and timezone. |
|
12 | - * |
|
13 | - * @param string|null $time Time string, default to 'now' |
|
14 | - * @param string|null $tz Timezone, default if omitted |
|
15 | - * @throws \RuntimeException |
|
16 | - * @return \DateTimeImmutable |
|
17 | - */ |
|
18 | - private static function _createDateTime($time = null, $tz = null): \DateTimeImmutable |
|
19 | - { |
|
20 | - try { |
|
21 | - if (!isset($tz)) { |
|
22 | - $tz = date_default_timezone_get(); |
|
23 | - } |
|
10 | + /** |
|
11 | + * Create DateTime object from time string and timezone. |
|
12 | + * |
|
13 | + * @param string|null $time Time string, default to 'now' |
|
14 | + * @param string|null $tz Timezone, default if omitted |
|
15 | + * @throws \RuntimeException |
|
16 | + * @return \DateTimeImmutable |
|
17 | + */ |
|
18 | + private static function _createDateTime($time = null, $tz = null): \DateTimeImmutable |
|
19 | + { |
|
20 | + try { |
|
21 | + if (!isset($tz)) { |
|
22 | + $tz = date_default_timezone_get(); |
|
23 | + } |
|
24 | 24 | |
25 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
26 | - return self::_roundDownFractionalSeconds($dt); |
|
27 | - } catch (\Exception $e) { |
|
28 | - throw new \RuntimeException( |
|
29 | - "Failed to create DateTime: " . |
|
30 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
31 | - } |
|
32 | - } |
|
25 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
26 | + return self::_roundDownFractionalSeconds($dt); |
|
27 | + } catch (\Exception $e) { |
|
28 | + throw new \RuntimeException( |
|
29 | + "Failed to create DateTime: " . |
|
30 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
31 | + } |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Rounds a \DateTimeImmutable value such that fractional |
|
36 | - * seconds are removed. |
|
37 | - * |
|
38 | - * @param \DateTimeImmutable $dt |
|
39 | - * @return \DateTimeImmutable |
|
40 | - */ |
|
41 | - private static function _roundDownFractionalSeconds(\DateTimeImmutable $dt): \DateTimeImmutable |
|
42 | - { |
|
43 | - return \DateTimeImmutable::createFromFormat("Y-m-d H:i:s", |
|
44 | - $dt->format("Y-m-d H:i:s"), $dt->getTimezone()); |
|
45 | - } |
|
34 | + /** |
|
35 | + * Rounds a \DateTimeImmutable value such that fractional |
|
36 | + * seconds are removed. |
|
37 | + * |
|
38 | + * @param \DateTimeImmutable $dt |
|
39 | + * @return \DateTimeImmutable |
|
40 | + */ |
|
41 | + private static function _roundDownFractionalSeconds(\DateTimeImmutable $dt): \DateTimeImmutable |
|
42 | + { |
|
43 | + return \DateTimeImmutable::createFromFormat("Y-m-d H:i:s", |
|
44 | + $dt->format("Y-m-d H:i:s"), $dt->getTimezone()); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Create DateTimeZone object from string. |
|
49 | - * |
|
50 | - * @param string $tz |
|
51 | - * @throws \UnexpectedValueException |
|
52 | - * @return \DateTimeZone |
|
53 | - */ |
|
54 | - private static function _createTimeZone($tz) |
|
55 | - { |
|
56 | - try { |
|
57 | - return new \DateTimeZone($tz); |
|
58 | - } catch (\Exception $e) { |
|
59 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
60 | - } |
|
61 | - } |
|
47 | + /** |
|
48 | + * Create DateTimeZone object from string. |
|
49 | + * |
|
50 | + * @param string $tz |
|
51 | + * @throws \UnexpectedValueException |
|
52 | + * @return \DateTimeZone |
|
53 | + */ |
|
54 | + private static function _createTimeZone($tz) |
|
55 | + { |
|
56 | + try { |
|
57 | + return new \DateTimeZone($tz); |
|
58 | + } catch (\Exception $e) { |
|
59 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Get last error caused by DateTimeImmutable. |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - private static function _getLastDateTimeImmutableErrorsStr() |
|
69 | - { |
|
70 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
71 | - return implode(", ", $errors); |
|
72 | - } |
|
63 | + /** |
|
64 | + * Get last error caused by DateTimeImmutable. |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + private static function _getLastDateTimeImmutableErrorsStr() |
|
69 | + { |
|
70 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
71 | + return implode(", ", $errors); |
|
72 | + } |
|
73 | 73 | } |
@@ -20,107 +20,107 @@ |
||
20 | 20 | */ |
21 | 21 | class Attributes implements \Countable, \IteratorAggregate |
22 | 22 | { |
23 | - use AttributeContainer; |
|
23 | + use AttributeContainer; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Mapping from OID to attribute value class name. |
|
27 | - * |
|
28 | - * @internal |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - const MAP_OID_TO_CLASS = array( |
|
33 | - /* @formatter:off */ |
|
34 | - ExtensionRequestValue::OID => ExtensionRequestValue::class |
|
35 | - /* @formatter:on */ |
|
36 | - ); |
|
25 | + /** |
|
26 | + * Mapping from OID to attribute value class name. |
|
27 | + * |
|
28 | + * @internal |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + const MAP_OID_TO_CLASS = array( |
|
33 | + /* @formatter:off */ |
|
34 | + ExtensionRequestValue::OID => ExtensionRequestValue::class |
|
35 | + /* @formatter:on */ |
|
36 | + ); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param Attribute ...$attribs Attribute objects |
|
42 | - */ |
|
43 | - public function __construct(Attribute ...$attribs) |
|
44 | - { |
|
45 | - $this->_attributes = $attribs; |
|
46 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param Attribute ...$attribs Attribute objects |
|
42 | + */ |
|
43 | + public function __construct(Attribute ...$attribs) |
|
44 | + { |
|
45 | + $this->_attributes = $attribs; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize from attribute values. |
|
50 | - * |
|
51 | - * @param AttributeValue ...$values |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public static function fromAttributeValues(AttributeValue ...$values) |
|
55 | - { |
|
56 | - $attribs = array_map( |
|
57 | - function (AttributeValue $value) { |
|
58 | - return $value->toAttribute(); |
|
59 | - }, $values); |
|
60 | - return new self(...$attribs); |
|
61 | - } |
|
48 | + /** |
|
49 | + * Initialize from attribute values. |
|
50 | + * |
|
51 | + * @param AttributeValue ...$values |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public static function fromAttributeValues(AttributeValue ...$values) |
|
55 | + { |
|
56 | + $attribs = array_map( |
|
57 | + function (AttributeValue $value) { |
|
58 | + return $value->toAttribute(); |
|
59 | + }, $values); |
|
60 | + return new self(...$attribs); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Initialize from ASN.1. |
|
65 | - * |
|
66 | - * @param Set $set |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromASN1(Set $set) |
|
70 | - { |
|
71 | - $attribs = array_map( |
|
72 | - function (UnspecifiedType $el) { |
|
73 | - return Attribute::fromASN1($el->asSequence()); |
|
74 | - }, $set->elements()); |
|
75 | - // cast attributes |
|
76 | - $attribs = array_map( |
|
77 | - function (Attribute $attr) { |
|
78 | - $oid = $attr->oid(); |
|
79 | - if (array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
|
80 | - $cls = self::MAP_OID_TO_CLASS[$oid]; |
|
81 | - return $attr->castValues($cls); |
|
82 | - } |
|
83 | - return $attr; |
|
84 | - }, $attribs); |
|
85 | - return new self(...$attribs); |
|
86 | - } |
|
63 | + /** |
|
64 | + * Initialize from ASN.1. |
|
65 | + * |
|
66 | + * @param Set $set |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromASN1(Set $set) |
|
70 | + { |
|
71 | + $attribs = array_map( |
|
72 | + function (UnspecifiedType $el) { |
|
73 | + return Attribute::fromASN1($el->asSequence()); |
|
74 | + }, $set->elements()); |
|
75 | + // cast attributes |
|
76 | + $attribs = array_map( |
|
77 | + function (Attribute $attr) { |
|
78 | + $oid = $attr->oid(); |
|
79 | + if (array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
|
80 | + $cls = self::MAP_OID_TO_CLASS[$oid]; |
|
81 | + return $attr->castValues($cls); |
|
82 | + } |
|
83 | + return $attr; |
|
84 | + }, $attribs); |
|
85 | + return new self(...$attribs); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Check whether extension request attribute is present. |
|
90 | - * |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - public function hasExtensionRequest(): bool |
|
94 | - { |
|
95 | - return $this->has(ExtensionRequestValue::OID); |
|
96 | - } |
|
88 | + /** |
|
89 | + * Check whether extension request attribute is present. |
|
90 | + * |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + public function hasExtensionRequest(): bool |
|
94 | + { |
|
95 | + return $this->has(ExtensionRequestValue::OID); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get extension request attribute value. |
|
100 | - * |
|
101 | - * @throws \LogicException |
|
102 | - * @return ExtensionRequestValue |
|
103 | - */ |
|
104 | - public function extensionRequest(): ExtensionRequestValue |
|
105 | - { |
|
106 | - if (!$this->hasExtensionRequest()) { |
|
107 | - throw new \LogicException("No extension request attribute."); |
|
108 | - } |
|
109 | - return $this->firstOf(ExtensionRequestValue::OID)->first(); |
|
110 | - } |
|
98 | + /** |
|
99 | + * Get extension request attribute value. |
|
100 | + * |
|
101 | + * @throws \LogicException |
|
102 | + * @return ExtensionRequestValue |
|
103 | + */ |
|
104 | + public function extensionRequest(): ExtensionRequestValue |
|
105 | + { |
|
106 | + if (!$this->hasExtensionRequest()) { |
|
107 | + throw new \LogicException("No extension request attribute."); |
|
108 | + } |
|
109 | + return $this->firstOf(ExtensionRequestValue::OID)->first(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Generate ASN.1 structure. |
|
114 | - * |
|
115 | - * @return Set |
|
116 | - */ |
|
117 | - public function toASN1(): Set |
|
118 | - { |
|
119 | - $elements = array_map( |
|
120 | - function (Attribute $attr) { |
|
121 | - return $attr->toASN1(); |
|
122 | - }, array_values($this->_attributes)); |
|
123 | - $set = new Set(...$elements); |
|
124 | - return $set->sortedSetOf(); |
|
125 | - } |
|
112 | + /** |
|
113 | + * Generate ASN.1 structure. |
|
114 | + * |
|
115 | + * @return Set |
|
116 | + */ |
|
117 | + public function toASN1(): Set |
|
118 | + { |
|
119 | + $elements = array_map( |
|
120 | + function (Attribute $attr) { |
|
121 | + return $attr->toASN1(); |
|
122 | + }, array_values($this->_attributes)); |
|
123 | + $set = new Set(...$elements); |
|
124 | + return $set->sortedSetOf(); |
|
125 | + } |
|
126 | 126 | } |
@@ -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 X509\CertificationRequest; |
6 | 6 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | public static function fromAttributeValues(AttributeValue ...$values) |
55 | 55 | { |
56 | 56 | $attribs = array_map( |
57 | - function (AttributeValue $value) { |
|
57 | + function(AttributeValue $value) { |
|
58 | 58 | return $value->toAttribute(); |
59 | 59 | }, $values); |
60 | 60 | return new self(...$attribs); |
@@ -69,12 +69,12 @@ discard block |
||
69 | 69 | public static function fromASN1(Set $set) |
70 | 70 | { |
71 | 71 | $attribs = array_map( |
72 | - function (UnspecifiedType $el) { |
|
72 | + function(UnspecifiedType $el) { |
|
73 | 73 | return Attribute::fromASN1($el->asSequence()); |
74 | 74 | }, $set->elements()); |
75 | 75 | // cast attributes |
76 | 76 | $attribs = array_map( |
77 | - function (Attribute $attr) { |
|
77 | + function(Attribute $attr) { |
|
78 | 78 | $oid = $attr->oid(); |
79 | 79 | if (array_key_exists($oid, self::MAP_OID_TO_CLASS)) { |
80 | 80 | $cls = self::MAP_OID_TO_CLASS[$oid]; |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | public function toASN1(): Set |
118 | 118 | { |
119 | 119 | $elements = array_map( |
120 | - function (Attribute $attr) { |
|
120 | + function(Attribute $attr) { |
|
121 | 121 | return $attr->toASN1(); |
122 | 122 | }, array_values($this->_attributes)); |
123 | 123 | $set = new Set(...$elements); |
@@ -16,94 +16,94 @@ |
||
16 | 16 | */ |
17 | 17 | class ExtensionRequestValue extends AttributeValue |
18 | 18 | { |
19 | - const OID = "1.2.840.113549.1.9.14"; |
|
19 | + const OID = "1.2.840.113549.1.9.14"; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Extensions. |
|
23 | - * |
|
24 | - * @var Extensions $_extensions |
|
25 | - */ |
|
26 | - protected $_extensions; |
|
21 | + /** |
|
22 | + * Extensions. |
|
23 | + * |
|
24 | + * @var Extensions $_extensions |
|
25 | + */ |
|
26 | + protected $_extensions; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param Extensions $extensions |
|
32 | - */ |
|
33 | - public function __construct(Extensions $extensions) |
|
34 | - { |
|
35 | - $this->_extensions = $extensions; |
|
36 | - $this->_oid = self::OID; |
|
37 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param Extensions $extensions |
|
32 | + */ |
|
33 | + public function __construct(Extensions $extensions) |
|
34 | + { |
|
35 | + $this->_extensions = $extensions; |
|
36 | + $this->_oid = self::OID; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * |
|
41 | - * @see \X501\ASN1\AttributeValue\AttributeValue::fromASN1() |
|
42 | - * @param UnspecifiedType $el |
|
43 | - * @return self |
|
44 | - */ |
|
45 | - public static function fromASN1(UnspecifiedType $el) |
|
46 | - { |
|
47 | - return new self(Extensions::fromASN1($el->asSequence())); |
|
48 | - } |
|
39 | + /** |
|
40 | + * |
|
41 | + * @see \X501\ASN1\AttributeValue\AttributeValue::fromASN1() |
|
42 | + * @param UnspecifiedType $el |
|
43 | + * @return self |
|
44 | + */ |
|
45 | + public static function fromASN1(UnspecifiedType $el) |
|
46 | + { |
|
47 | + return new self(Extensions::fromASN1($el->asSequence())); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Get requested extensions. |
|
52 | - * |
|
53 | - * @return Extensions |
|
54 | - */ |
|
55 | - public function extensions(): Extensions |
|
56 | - { |
|
57 | - return $this->_extensions; |
|
58 | - } |
|
50 | + /** |
|
51 | + * Get requested extensions. |
|
52 | + * |
|
53 | + * @return Extensions |
|
54 | + */ |
|
55 | + public function extensions(): Extensions |
|
56 | + { |
|
57 | + return $this->_extensions; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * |
|
62 | - * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1() |
|
63 | - * @return \ASN1\Type\Constructed\Sequence |
|
64 | - */ |
|
65 | - public function toASN1(): \ASN1\Type\Constructed\Sequence |
|
66 | - { |
|
67 | - return $this->_extensions->toASN1(); |
|
68 | - } |
|
60 | + /** |
|
61 | + * |
|
62 | + * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1() |
|
63 | + * @return \ASN1\Type\Constructed\Sequence |
|
64 | + */ |
|
65 | + public function toASN1(): \ASN1\Type\Constructed\Sequence |
|
66 | + { |
|
67 | + return $this->_extensions->toASN1(); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * |
|
72 | - * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue() |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function stringValue(): string |
|
76 | - { |
|
77 | - return "#" . bin2hex($this->toASN1()->toDER()); |
|
78 | - } |
|
70 | + /** |
|
71 | + * |
|
72 | + * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue() |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function stringValue(): string |
|
76 | + { |
|
77 | + return "#" . bin2hex($this->toASN1()->toDER()); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * |
|
82 | - * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule() |
|
83 | - * @return BinaryMatch |
|
84 | - */ |
|
85 | - public function equalityMatchingRule(): BinaryMatch |
|
86 | - { |
|
87 | - return new BinaryMatch(); |
|
88 | - } |
|
80 | + /** |
|
81 | + * |
|
82 | + * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule() |
|
83 | + * @return BinaryMatch |
|
84 | + */ |
|
85 | + public function equalityMatchingRule(): BinaryMatch |
|
86 | + { |
|
87 | + return new BinaryMatch(); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * |
|
92 | - * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String() |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function rfc2253String(): string |
|
96 | - { |
|
97 | - return $this->stringValue(); |
|
98 | - } |
|
90 | + /** |
|
91 | + * |
|
92 | + * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String() |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function rfc2253String(): string |
|
96 | + { |
|
97 | + return $this->stringValue(); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * |
|
102 | - * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString() |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - protected function _transcodedString(): string |
|
106 | - { |
|
107 | - return $this->stringValue(); |
|
108 | - } |
|
100 | + /** |
|
101 | + * |
|
102 | + * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString() |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + protected function _transcodedString(): string |
|
106 | + { |
|
107 | + return $this->stringValue(); |
|
108 | + } |
|
109 | 109 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationRequest\Attribute; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationRequest; |
6 | 6 |
@@ -22,204 +22,204 @@ |
||
22 | 22 | */ |
23 | 23 | class CertificationRequestInfo |
24 | 24 | { |
25 | - const VERSION_1 = 0; |
|
26 | - |
|
27 | - /** |
|
28 | - * Version. |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - protected $_version; |
|
33 | - |
|
34 | - /** |
|
35 | - * Subject. |
|
36 | - * |
|
37 | - * @var Name $_subject |
|
38 | - */ |
|
39 | - protected $_subject; |
|
40 | - |
|
41 | - /** |
|
42 | - * Public key info. |
|
43 | - * |
|
44 | - * @var PublicKeyInfo $_subjectPKInfo |
|
45 | - */ |
|
46 | - protected $_subjectPKInfo; |
|
47 | - |
|
48 | - /** |
|
49 | - * Attributes. |
|
50 | - * |
|
51 | - * @var Attributes|null $_attributes |
|
52 | - */ |
|
53 | - protected $_attributes; |
|
54 | - |
|
55 | - /** |
|
56 | - * Constructor. |
|
57 | - * |
|
58 | - * @param Name $subject Subject |
|
59 | - * @param PublicKeyInfo $pkinfo Public key info |
|
60 | - */ |
|
61 | - public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
62 | - { |
|
63 | - $this->_version = self::VERSION_1; |
|
64 | - $this->_subject = $subject; |
|
65 | - $this->_subjectPKInfo = $pkinfo; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Initialize from ASN.1. |
|
70 | - * |
|
71 | - * @param Sequence $seq |
|
72 | - * @throws \UnexpectedValueException |
|
73 | - * @return self |
|
74 | - */ |
|
75 | - public static function fromASN1(Sequence $seq) |
|
76 | - { |
|
77 | - $version = $seq->at(0) |
|
78 | - ->asInteger() |
|
79 | - ->number(); |
|
80 | - if ($version != self::VERSION_1) { |
|
81 | - throw new \UnexpectedValueException( |
|
82 | - "Version $version not supported."); |
|
83 | - } |
|
84 | - $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
85 | - $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
86 | - $obj = new self($subject, $pkinfo); |
|
87 | - if ($seq->hasTagged(0)) { |
|
88 | - $obj->_attributes = Attributes::fromASN1( |
|
89 | - $seq->getTagged(0) |
|
90 | - ->asImplicit(Element::TYPE_SET) |
|
91 | - ->asSet()); |
|
92 | - } |
|
93 | - return $obj; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get version. |
|
98 | - * |
|
99 | - * @return int |
|
100 | - */ |
|
101 | - public function version(): int |
|
102 | - { |
|
103 | - return $this->_version; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get self with subject. |
|
108 | - * |
|
109 | - * @param Name $subject |
|
110 | - * @return self |
|
111 | - */ |
|
112 | - public function withSubject(Name $subject) |
|
113 | - { |
|
114 | - $obj = clone $this; |
|
115 | - $obj->_subject = $subject; |
|
116 | - return $obj; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Get subject. |
|
121 | - * |
|
122 | - * @return Name |
|
123 | - */ |
|
124 | - public function subject(): Name |
|
125 | - { |
|
126 | - return $this->_subject; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get subject public key info. |
|
131 | - * |
|
132 | - * @return PublicKeyInfo |
|
133 | - */ |
|
134 | - public function subjectPKInfo(): PublicKeyInfo |
|
135 | - { |
|
136 | - return $this->_subjectPKInfo; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Whether certification request info has attributes. |
|
141 | - * |
|
142 | - * @return bool |
|
143 | - */ |
|
144 | - public function hasAttributes(): bool |
|
145 | - { |
|
146 | - return isset($this->_attributes); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get attributes. |
|
151 | - * |
|
152 | - * @throws \LogicException |
|
153 | - * @return Attributes |
|
154 | - */ |
|
155 | - public function attributes(): Attributes |
|
156 | - { |
|
157 | - if (!$this->hasAttributes()) { |
|
158 | - throw new \LogicException("No attributes."); |
|
159 | - } |
|
160 | - return $this->_attributes; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get instance of self with attributes. |
|
165 | - * |
|
166 | - * @param Attributes $attribs |
|
167 | - */ |
|
168 | - public function withAttributes(Attributes $attribs) |
|
169 | - { |
|
170 | - $obj = clone $this; |
|
171 | - $obj->_attributes = $attribs; |
|
172 | - return $obj; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Get self with extension request attribute. |
|
177 | - * |
|
178 | - * @param Extensions $extensions Extensions to request |
|
179 | - * @return self |
|
180 | - */ |
|
181 | - public function withExtensionRequest(Extensions $extensions) |
|
182 | - { |
|
183 | - $obj = clone $this; |
|
184 | - if (!isset($obj->_attributes)) { |
|
185 | - $obj->_attributes = new Attributes(); |
|
186 | - } |
|
187 | - $obj->_attributes = $obj->_attributes->withUnique( |
|
188 | - Attribute::fromAttributeValues( |
|
189 | - new ExtensionRequestValue($extensions))); |
|
190 | - return $obj; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Generate ASN.1 structure. |
|
195 | - * |
|
196 | - * @return Sequence |
|
197 | - */ |
|
198 | - public function toASN1(): Sequence |
|
199 | - { |
|
200 | - $elements = array(new Integer($this->_version), |
|
201 | - $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1()); |
|
202 | - if (isset($this->_attributes)) { |
|
203 | - $elements[] = new ImplicitlyTaggedType(0, |
|
204 | - $this->_attributes->toASN1()); |
|
205 | - } |
|
206 | - return new Sequence(...$elements); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Create signed CertificationRequest. |
|
211 | - * |
|
212 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
213 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
214 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
215 | - * @return CertificationRequest |
|
216 | - */ |
|
217 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
218 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null) |
|
219 | - { |
|
220 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
221 | - $data = $this->toASN1()->toDER(); |
|
222 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
223 | - return new CertificationRequest($this, $algo, $signature); |
|
224 | - } |
|
25 | + const VERSION_1 = 0; |
|
26 | + |
|
27 | + /** |
|
28 | + * Version. |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + protected $_version; |
|
33 | + |
|
34 | + /** |
|
35 | + * Subject. |
|
36 | + * |
|
37 | + * @var Name $_subject |
|
38 | + */ |
|
39 | + protected $_subject; |
|
40 | + |
|
41 | + /** |
|
42 | + * Public key info. |
|
43 | + * |
|
44 | + * @var PublicKeyInfo $_subjectPKInfo |
|
45 | + */ |
|
46 | + protected $_subjectPKInfo; |
|
47 | + |
|
48 | + /** |
|
49 | + * Attributes. |
|
50 | + * |
|
51 | + * @var Attributes|null $_attributes |
|
52 | + */ |
|
53 | + protected $_attributes; |
|
54 | + |
|
55 | + /** |
|
56 | + * Constructor. |
|
57 | + * |
|
58 | + * @param Name $subject Subject |
|
59 | + * @param PublicKeyInfo $pkinfo Public key info |
|
60 | + */ |
|
61 | + public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
62 | + { |
|
63 | + $this->_version = self::VERSION_1; |
|
64 | + $this->_subject = $subject; |
|
65 | + $this->_subjectPKInfo = $pkinfo; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Initialize from ASN.1. |
|
70 | + * |
|
71 | + * @param Sequence $seq |
|
72 | + * @throws \UnexpectedValueException |
|
73 | + * @return self |
|
74 | + */ |
|
75 | + public static function fromASN1(Sequence $seq) |
|
76 | + { |
|
77 | + $version = $seq->at(0) |
|
78 | + ->asInteger() |
|
79 | + ->number(); |
|
80 | + if ($version != self::VERSION_1) { |
|
81 | + throw new \UnexpectedValueException( |
|
82 | + "Version $version not supported."); |
|
83 | + } |
|
84 | + $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
85 | + $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
86 | + $obj = new self($subject, $pkinfo); |
|
87 | + if ($seq->hasTagged(0)) { |
|
88 | + $obj->_attributes = Attributes::fromASN1( |
|
89 | + $seq->getTagged(0) |
|
90 | + ->asImplicit(Element::TYPE_SET) |
|
91 | + ->asSet()); |
|
92 | + } |
|
93 | + return $obj; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get version. |
|
98 | + * |
|
99 | + * @return int |
|
100 | + */ |
|
101 | + public function version(): int |
|
102 | + { |
|
103 | + return $this->_version; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get self with subject. |
|
108 | + * |
|
109 | + * @param Name $subject |
|
110 | + * @return self |
|
111 | + */ |
|
112 | + public function withSubject(Name $subject) |
|
113 | + { |
|
114 | + $obj = clone $this; |
|
115 | + $obj->_subject = $subject; |
|
116 | + return $obj; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Get subject. |
|
121 | + * |
|
122 | + * @return Name |
|
123 | + */ |
|
124 | + public function subject(): Name |
|
125 | + { |
|
126 | + return $this->_subject; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get subject public key info. |
|
131 | + * |
|
132 | + * @return PublicKeyInfo |
|
133 | + */ |
|
134 | + public function subjectPKInfo(): PublicKeyInfo |
|
135 | + { |
|
136 | + return $this->_subjectPKInfo; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Whether certification request info has attributes. |
|
141 | + * |
|
142 | + * @return bool |
|
143 | + */ |
|
144 | + public function hasAttributes(): bool |
|
145 | + { |
|
146 | + return isset($this->_attributes); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get attributes. |
|
151 | + * |
|
152 | + * @throws \LogicException |
|
153 | + * @return Attributes |
|
154 | + */ |
|
155 | + public function attributes(): Attributes |
|
156 | + { |
|
157 | + if (!$this->hasAttributes()) { |
|
158 | + throw new \LogicException("No attributes."); |
|
159 | + } |
|
160 | + return $this->_attributes; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get instance of self with attributes. |
|
165 | + * |
|
166 | + * @param Attributes $attribs |
|
167 | + */ |
|
168 | + public function withAttributes(Attributes $attribs) |
|
169 | + { |
|
170 | + $obj = clone $this; |
|
171 | + $obj->_attributes = $attribs; |
|
172 | + return $obj; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Get self with extension request attribute. |
|
177 | + * |
|
178 | + * @param Extensions $extensions Extensions to request |
|
179 | + * @return self |
|
180 | + */ |
|
181 | + public function withExtensionRequest(Extensions $extensions) |
|
182 | + { |
|
183 | + $obj = clone $this; |
|
184 | + if (!isset($obj->_attributes)) { |
|
185 | + $obj->_attributes = new Attributes(); |
|
186 | + } |
|
187 | + $obj->_attributes = $obj->_attributes->withUnique( |
|
188 | + Attribute::fromAttributeValues( |
|
189 | + new ExtensionRequestValue($extensions))); |
|
190 | + return $obj; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Generate ASN.1 structure. |
|
195 | + * |
|
196 | + * @return Sequence |
|
197 | + */ |
|
198 | + public function toASN1(): Sequence |
|
199 | + { |
|
200 | + $elements = array(new Integer($this->_version), |
|
201 | + $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1()); |
|
202 | + if (isset($this->_attributes)) { |
|
203 | + $elements[] = new ImplicitlyTaggedType(0, |
|
204 | + $this->_attributes->toASN1()); |
|
205 | + } |
|
206 | + return new Sequence(...$elements); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Create signed CertificationRequest. |
|
211 | + * |
|
212 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
213 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
214 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
215 | + * @return CertificationRequest |
|
216 | + */ |
|
217 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
218 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null) |
|
219 | + { |
|
220 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
221 | + $data = $this->toASN1()->toDER(); |
|
222 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
223 | + return new CertificationRequest($this, $algo, $signature); |
|
224 | + } |
|
225 | 225 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\Exception; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\Exception; |
6 | 6 |
@@ -13,265 +13,265 @@ |
||
13 | 13 | */ |
14 | 14 | class PolicyNode implements \IteratorAggregate, \Countable |
15 | 15 | { |
16 | - /** |
|
17 | - * Policy OID. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $_validPolicy; |
|
16 | + /** |
|
17 | + * Policy OID. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $_validPolicy; |
|
22 | 22 | |
23 | - /** |
|
24 | - * List of qualifiers. |
|
25 | - * |
|
26 | - * @var \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] |
|
27 | - */ |
|
28 | - protected $_qualifiers; |
|
23 | + /** |
|
24 | + * List of qualifiers. |
|
25 | + * |
|
26 | + * @var \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] |
|
27 | + */ |
|
28 | + protected $_qualifiers; |
|
29 | 29 | |
30 | - /** |
|
31 | - * List of expected policy OIDs. |
|
32 | - * |
|
33 | - * @var string[] |
|
34 | - */ |
|
35 | - protected $_expectedPolicies; |
|
30 | + /** |
|
31 | + * List of expected policy OIDs. |
|
32 | + * |
|
33 | + * @var string[] |
|
34 | + */ |
|
35 | + protected $_expectedPolicies; |
|
36 | 36 | |
37 | - /** |
|
38 | - * List of child nodes. |
|
39 | - * |
|
40 | - * @var PolicyNode[] |
|
41 | - */ |
|
42 | - protected $_children; |
|
37 | + /** |
|
38 | + * List of child nodes. |
|
39 | + * |
|
40 | + * @var PolicyNode[] |
|
41 | + */ |
|
42 | + protected $_children; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Reference to the parent node. |
|
46 | - * |
|
47 | - * @var PolicyNode|null |
|
48 | - */ |
|
49 | - protected $_parent; |
|
44 | + /** |
|
45 | + * Reference to the parent node. |
|
46 | + * |
|
47 | + * @var PolicyNode|null |
|
48 | + */ |
|
49 | + protected $_parent; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Constructor. |
|
53 | - * |
|
54 | - * @param string $valid_policy Policy OID |
|
55 | - * @param \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] $qualifiers |
|
56 | - * @param string[] $expected_policies |
|
57 | - */ |
|
58 | - public function __construct(string $valid_policy, array $qualifiers, |
|
59 | - array $expected_policies) |
|
60 | - { |
|
61 | - $this->_validPolicy = $valid_policy; |
|
62 | - $this->_qualifiers = $qualifiers; |
|
63 | - $this->_expectedPolicies = $expected_policies; |
|
64 | - $this->_children = array(); |
|
65 | - } |
|
51 | + /** |
|
52 | + * Constructor. |
|
53 | + * |
|
54 | + * @param string $valid_policy Policy OID |
|
55 | + * @param \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] $qualifiers |
|
56 | + * @param string[] $expected_policies |
|
57 | + */ |
|
58 | + public function __construct(string $valid_policy, array $qualifiers, |
|
59 | + array $expected_policies) |
|
60 | + { |
|
61 | + $this->_validPolicy = $valid_policy; |
|
62 | + $this->_qualifiers = $qualifiers; |
|
63 | + $this->_expectedPolicies = $expected_policies; |
|
64 | + $this->_children = array(); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Create initial node for the policy tree. |
|
69 | - * |
|
70 | - * @return self |
|
71 | - */ |
|
72 | - public static function anyPolicyNode() |
|
73 | - { |
|
74 | - return new self(PolicyInformation::OID_ANY_POLICY, array(), |
|
75 | - array(PolicyInformation::OID_ANY_POLICY)); |
|
76 | - } |
|
67 | + /** |
|
68 | + * Create initial node for the policy tree. |
|
69 | + * |
|
70 | + * @return self |
|
71 | + */ |
|
72 | + public static function anyPolicyNode() |
|
73 | + { |
|
74 | + return new self(PolicyInformation::OID_ANY_POLICY, array(), |
|
75 | + array(PolicyInformation::OID_ANY_POLICY)); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get the valid policy OID. |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function validPolicy(): string |
|
84 | - { |
|
85 | - return $this->_validPolicy; |
|
86 | - } |
|
78 | + /** |
|
79 | + * Get the valid policy OID. |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function validPolicy(): string |
|
84 | + { |
|
85 | + return $this->_validPolicy; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Check whether node has anyPolicy as a valid policy. |
|
90 | - * |
|
91 | - * @return boolean |
|
92 | - */ |
|
93 | - public function isAnyPolicy(): bool |
|
94 | - { |
|
95 | - return PolicyInformation::OID_ANY_POLICY == $this->_validPolicy; |
|
96 | - } |
|
88 | + /** |
|
89 | + * Check whether node has anyPolicy as a valid policy. |
|
90 | + * |
|
91 | + * @return boolean |
|
92 | + */ |
|
93 | + public function isAnyPolicy(): bool |
|
94 | + { |
|
95 | + return PolicyInformation::OID_ANY_POLICY == $this->_validPolicy; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get the qualifier set. |
|
100 | - * |
|
101 | - * @return \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] |
|
102 | - */ |
|
103 | - public function qualifiers(): array |
|
104 | - { |
|
105 | - return $this->_qualifiers; |
|
106 | - } |
|
98 | + /** |
|
99 | + * Get the qualifier set. |
|
100 | + * |
|
101 | + * @return \X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo[] |
|
102 | + */ |
|
103 | + public function qualifiers(): array |
|
104 | + { |
|
105 | + return $this->_qualifiers; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Check whether node has OID as an expected policy. |
|
110 | - * |
|
111 | - * @param string $oid |
|
112 | - * @return boolean |
|
113 | - */ |
|
114 | - public function hasExpectedPolicy($oid): bool |
|
115 | - { |
|
116 | - return in_array($oid, $this->_expectedPolicies); |
|
117 | - } |
|
108 | + /** |
|
109 | + * Check whether node has OID as an expected policy. |
|
110 | + * |
|
111 | + * @param string $oid |
|
112 | + * @return boolean |
|
113 | + */ |
|
114 | + public function hasExpectedPolicy($oid): bool |
|
115 | + { |
|
116 | + return in_array($oid, $this->_expectedPolicies); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Get the expected policy set. |
|
121 | - * |
|
122 | - * @return string[] |
|
123 | - */ |
|
124 | - public function expectedPolicies(): array |
|
125 | - { |
|
126 | - return $this->_expectedPolicies; |
|
127 | - } |
|
119 | + /** |
|
120 | + * Get the expected policy set. |
|
121 | + * |
|
122 | + * @return string[] |
|
123 | + */ |
|
124 | + public function expectedPolicies(): array |
|
125 | + { |
|
126 | + return $this->_expectedPolicies; |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * Set expected policies. |
|
131 | - * |
|
132 | - * @param string ...$oids Policy OIDs |
|
133 | - */ |
|
134 | - public function setExpectedPolicies(...$oids) |
|
135 | - { |
|
136 | - $this->_expectedPolicies = $oids; |
|
137 | - } |
|
129 | + /** |
|
130 | + * Set expected policies. |
|
131 | + * |
|
132 | + * @param string ...$oids Policy OIDs |
|
133 | + */ |
|
134 | + public function setExpectedPolicies(...$oids) |
|
135 | + { |
|
136 | + $this->_expectedPolicies = $oids; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Check whether node has a child node with given valid policy OID. |
|
141 | - * |
|
142 | - * @param string $oid |
|
143 | - * @return boolean |
|
144 | - */ |
|
145 | - public function hasChildWithValidPolicy($oid): bool |
|
146 | - { |
|
147 | - foreach ($this->_children as $node) { |
|
148 | - if ($node->validPolicy() == $oid) { |
|
149 | - return true; |
|
150 | - } |
|
151 | - } |
|
152 | - return false; |
|
153 | - } |
|
139 | + /** |
|
140 | + * Check whether node has a child node with given valid policy OID. |
|
141 | + * |
|
142 | + * @param string $oid |
|
143 | + * @return boolean |
|
144 | + */ |
|
145 | + public function hasChildWithValidPolicy($oid): bool |
|
146 | + { |
|
147 | + foreach ($this->_children as $node) { |
|
148 | + if ($node->validPolicy() == $oid) { |
|
149 | + return true; |
|
150 | + } |
|
151 | + } |
|
152 | + return false; |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Add child node. |
|
157 | - * |
|
158 | - * @param PolicyNode $node |
|
159 | - * @return self |
|
160 | - */ |
|
161 | - public function addChild(PolicyNode $node) |
|
162 | - { |
|
163 | - $id = spl_object_hash($node); |
|
164 | - $node->_parent = $this; |
|
165 | - $this->_children[$id] = $node; |
|
166 | - return $this; |
|
167 | - } |
|
155 | + /** |
|
156 | + * Add child node. |
|
157 | + * |
|
158 | + * @param PolicyNode $node |
|
159 | + * @return self |
|
160 | + */ |
|
161 | + public function addChild(PolicyNode $node) |
|
162 | + { |
|
163 | + $id = spl_object_hash($node); |
|
164 | + $node->_parent = $this; |
|
165 | + $this->_children[$id] = $node; |
|
166 | + return $this; |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Get the child nodes. |
|
171 | - * |
|
172 | - * @return PolicyNode[] |
|
173 | - */ |
|
174 | - public function children(): array |
|
175 | - { |
|
176 | - return array_values($this->_children); |
|
177 | - } |
|
169 | + /** |
|
170 | + * Get the child nodes. |
|
171 | + * |
|
172 | + * @return PolicyNode[] |
|
173 | + */ |
|
174 | + public function children(): array |
|
175 | + { |
|
176 | + return array_values($this->_children); |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Remove this node from the tree. |
|
181 | - * |
|
182 | - * @return self The removed node |
|
183 | - */ |
|
184 | - public function remove() |
|
185 | - { |
|
186 | - if ($this->_parent) { |
|
187 | - $id = spl_object_hash($this); |
|
188 | - unset($this->_parent->_children[$id]); |
|
189 | - unset($this->_parent); |
|
190 | - } |
|
191 | - return $this; |
|
192 | - } |
|
179 | + /** |
|
180 | + * Remove this node from the tree. |
|
181 | + * |
|
182 | + * @return self The removed node |
|
183 | + */ |
|
184 | + public function remove() |
|
185 | + { |
|
186 | + if ($this->_parent) { |
|
187 | + $id = spl_object_hash($this); |
|
188 | + unset($this->_parent->_children[$id]); |
|
189 | + unset($this->_parent); |
|
190 | + } |
|
191 | + return $this; |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Check whether node has a parent. |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - public function hasParent(): bool |
|
200 | - { |
|
201 | - return isset($this->_parent); |
|
202 | - } |
|
194 | + /** |
|
195 | + * Check whether node has a parent. |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + public function hasParent(): bool |
|
200 | + { |
|
201 | + return isset($this->_parent); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Get the parent node. |
|
206 | - * |
|
207 | - * @return PolicyNode|null |
|
208 | - */ |
|
209 | - public function parent() |
|
210 | - { |
|
211 | - return $this->_parent; |
|
212 | - } |
|
204 | + /** |
|
205 | + * Get the parent node. |
|
206 | + * |
|
207 | + * @return PolicyNode|null |
|
208 | + */ |
|
209 | + public function parent() |
|
210 | + { |
|
211 | + return $this->_parent; |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * Get chain of parent nodes from this node's parent to the root node. |
|
216 | - * |
|
217 | - * @return PolicyNode[] |
|
218 | - */ |
|
219 | - public function parents(): array |
|
220 | - { |
|
221 | - if (!$this->_parent) { |
|
222 | - return array(); |
|
223 | - } |
|
224 | - $nodes = $this->_parent->parents(); |
|
225 | - $nodes[] = $this->_parent; |
|
226 | - return array_reverse($nodes); |
|
227 | - } |
|
214 | + /** |
|
215 | + * Get chain of parent nodes from this node's parent to the root node. |
|
216 | + * |
|
217 | + * @return PolicyNode[] |
|
218 | + */ |
|
219 | + public function parents(): array |
|
220 | + { |
|
221 | + if (!$this->_parent) { |
|
222 | + return array(); |
|
223 | + } |
|
224 | + $nodes = $this->_parent->parents(); |
|
225 | + $nodes[] = $this->_parent; |
|
226 | + return array_reverse($nodes); |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * Walk tree from this node, applying a callback for each node. |
|
231 | - * |
|
232 | - * Nodes are traversed depth-first and callback shall be applied post-order. |
|
233 | - * |
|
234 | - * @param callable $fn |
|
235 | - */ |
|
236 | - public function walkNodes(callable $fn) |
|
237 | - { |
|
238 | - foreach ($this->_children as $node) { |
|
239 | - $node->walkNodes($fn); |
|
240 | - } |
|
241 | - $fn($this); |
|
242 | - } |
|
229 | + /** |
|
230 | + * Walk tree from this node, applying a callback for each node. |
|
231 | + * |
|
232 | + * Nodes are traversed depth-first and callback shall be applied post-order. |
|
233 | + * |
|
234 | + * @param callable $fn |
|
235 | + */ |
|
236 | + public function walkNodes(callable $fn) |
|
237 | + { |
|
238 | + foreach ($this->_children as $node) { |
|
239 | + $node->walkNodes($fn); |
|
240 | + } |
|
241 | + $fn($this); |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Get the total number of nodes in a tree. |
|
246 | - * |
|
247 | - * @return int |
|
248 | - */ |
|
249 | - public function nodeCount(): int |
|
250 | - { |
|
251 | - $c = 1; |
|
252 | - foreach ($this->_children as $child) { |
|
253 | - $c += $child->nodeCount(); |
|
254 | - } |
|
255 | - return $c; |
|
256 | - } |
|
244 | + /** |
|
245 | + * Get the total number of nodes in a tree. |
|
246 | + * |
|
247 | + * @return int |
|
248 | + */ |
|
249 | + public function nodeCount(): int |
|
250 | + { |
|
251 | + $c = 1; |
|
252 | + foreach ($this->_children as $child) { |
|
253 | + $c += $child->nodeCount(); |
|
254 | + } |
|
255 | + return $c; |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * Get the number of child nodes. |
|
260 | - * |
|
261 | - * @see \Countable::count() |
|
262 | - */ |
|
263 | - public function count(): int |
|
264 | - { |
|
265 | - return count($this->_children); |
|
266 | - } |
|
258 | + /** |
|
259 | + * Get the number of child nodes. |
|
260 | + * |
|
261 | + * @see \Countable::count() |
|
262 | + */ |
|
263 | + public function count(): int |
|
264 | + { |
|
265 | + return count($this->_children); |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get iterator for the child nodes. |
|
270 | - * |
|
271 | - * @see \IteratorAggregate::getIterator() |
|
272 | - */ |
|
273 | - public function getIterator(): \ArrayIterator |
|
274 | - { |
|
275 | - return new \ArrayIterator($this->_children); |
|
276 | - } |
|
268 | + /** |
|
269 | + * Get iterator for the child nodes. |
|
270 | + * |
|
271 | + * @see \IteratorAggregate::getIterator() |
|
272 | + */ |
|
273 | + public function getIterator(): \ArrayIterator |
|
274 | + { |
|
275 | + return new \ArrayIterator($this->_children); |
|
276 | + } |
|
277 | 277 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\CertificationPath\Policy; |
6 | 6 |
@@ -10,411 +10,411 @@ |
||
10 | 10 | |
11 | 11 | class PolicyTree |
12 | 12 | { |
13 | - /** |
|
14 | - * Root node at depth zero. |
|
15 | - * |
|
16 | - * @var PolicyNode|null |
|
17 | - */ |
|
18 | - protected $_root; |
|
13 | + /** |
|
14 | + * Root node at depth zero. |
|
15 | + * |
|
16 | + * @var PolicyNode|null |
|
17 | + */ |
|
18 | + protected $_root; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param PolicyNode $root Initial root node |
|
24 | - */ |
|
25 | - public function __construct(PolicyNode $root) |
|
26 | - { |
|
27 | - $this->_root = $root; |
|
28 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param PolicyNode $root Initial root node |
|
24 | + */ |
|
25 | + public function __construct(PolicyNode $root) |
|
26 | + { |
|
27 | + $this->_root = $root; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Process policy information from the certificate. |
|
32 | - * |
|
33 | - * Certificate policies extension must be present. |
|
34 | - * |
|
35 | - * @param ValidatorState $state |
|
36 | - * @param Certificate $cert |
|
37 | - * @return ValidatorState |
|
38 | - */ |
|
39 | - public function processPolicies(ValidatorState $state, Certificate $cert): ValidatorState |
|
40 | - { |
|
41 | - $policies = $cert->tbsCertificate() |
|
42 | - ->extensions() |
|
43 | - ->certificatePolicies(); |
|
44 | - $tree = clone $this; |
|
45 | - // (d.1) for each policy P not equal to anyPolicy |
|
46 | - foreach ($policies as $policy) { |
|
47 | - if ($policy->isAnyPolicy()) { |
|
48 | - $tree->_processAnyPolicy($policy, $cert, $state); |
|
49 | - } else { |
|
50 | - $tree->_processPolicy($policy, $state); |
|
51 | - } |
|
52 | - } |
|
53 | - // if whole tree is pruned |
|
54 | - if (!$tree->_pruneTree($state->index() - 1)) { |
|
55 | - return $state->withoutValidPolicyTree(); |
|
56 | - } |
|
57 | - return $state->withValidPolicyTree($tree); |
|
58 | - } |
|
30 | + /** |
|
31 | + * Process policy information from the certificate. |
|
32 | + * |
|
33 | + * Certificate policies extension must be present. |
|
34 | + * |
|
35 | + * @param ValidatorState $state |
|
36 | + * @param Certificate $cert |
|
37 | + * @return ValidatorState |
|
38 | + */ |
|
39 | + public function processPolicies(ValidatorState $state, Certificate $cert): ValidatorState |
|
40 | + { |
|
41 | + $policies = $cert->tbsCertificate() |
|
42 | + ->extensions() |
|
43 | + ->certificatePolicies(); |
|
44 | + $tree = clone $this; |
|
45 | + // (d.1) for each policy P not equal to anyPolicy |
|
46 | + foreach ($policies as $policy) { |
|
47 | + if ($policy->isAnyPolicy()) { |
|
48 | + $tree->_processAnyPolicy($policy, $cert, $state); |
|
49 | + } else { |
|
50 | + $tree->_processPolicy($policy, $state); |
|
51 | + } |
|
52 | + } |
|
53 | + // if whole tree is pruned |
|
54 | + if (!$tree->_pruneTree($state->index() - 1)) { |
|
55 | + return $state->withoutValidPolicyTree(); |
|
56 | + } |
|
57 | + return $state->withValidPolicyTree($tree); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Process policy mappings from the certificate. |
|
62 | - * |
|
63 | - * @param ValidatorState $state |
|
64 | - * @param Certificate $cert |
|
65 | - * @return ValidatorState |
|
66 | - */ |
|
67 | - public function processMappings(ValidatorState $state, Certificate $cert): ValidatorState |
|
68 | - { |
|
69 | - $tree = clone $this; |
|
70 | - if ($state->policyMapping() > 0) { |
|
71 | - $tree->_applyMappings($cert, $state); |
|
72 | - } else if ($state->policyMapping() == 0) { |
|
73 | - $tree->_deleteMappings($cert, $state); |
|
74 | - } |
|
75 | - // if whole tree is pruned |
|
76 | - if (!$tree->_root) { |
|
77 | - return $state->withoutValidPolicyTree(); |
|
78 | - } |
|
79 | - return $state->withValidPolicyTree($tree); |
|
80 | - } |
|
60 | + /** |
|
61 | + * Process policy mappings from the certificate. |
|
62 | + * |
|
63 | + * @param ValidatorState $state |
|
64 | + * @param Certificate $cert |
|
65 | + * @return ValidatorState |
|
66 | + */ |
|
67 | + public function processMappings(ValidatorState $state, Certificate $cert): ValidatorState |
|
68 | + { |
|
69 | + $tree = clone $this; |
|
70 | + if ($state->policyMapping() > 0) { |
|
71 | + $tree->_applyMappings($cert, $state); |
|
72 | + } else if ($state->policyMapping() == 0) { |
|
73 | + $tree->_deleteMappings($cert, $state); |
|
74 | + } |
|
75 | + // if whole tree is pruned |
|
76 | + if (!$tree->_root) { |
|
77 | + return $state->withoutValidPolicyTree(); |
|
78 | + } |
|
79 | + return $state->withValidPolicyTree($tree); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g. |
|
84 | - * |
|
85 | - * @param ValidatorState $state |
|
86 | - * @param array $policies |
|
87 | - * @return ValidatorState |
|
88 | - */ |
|
89 | - public function calculateIntersection(ValidatorState $state, array $policies): ValidatorState |
|
90 | - { |
|
91 | - $tree = clone $this; |
|
92 | - $valid_policy_node_set = $tree->_validPolicyNodeSet(); |
|
93 | - // 2. If the valid_policy of any node in the valid_policy_node_set |
|
94 | - // is not in the user-initial-policy-set and is not anyPolicy, |
|
95 | - // delete this node and all its children. |
|
96 | - $valid_policy_node_set = array_filter($valid_policy_node_set, |
|
97 | - function (PolicyNode $node) use ($policies) { |
|
98 | - if ($node->isAnyPolicy()) { |
|
99 | - return true; |
|
100 | - } |
|
101 | - if (in_array($node->validPolicy(), $policies)) { |
|
102 | - return true; |
|
103 | - } |
|
104 | - $node->remove(); |
|
105 | - return false; |
|
106 | - }); |
|
107 | - // array of valid policy OIDs |
|
108 | - $valid_policy_set = array_map( |
|
109 | - function (PolicyNode $node) { |
|
110 | - return $node->validPolicy(); |
|
111 | - }, $valid_policy_node_set); |
|
112 | - // 3. If the valid_policy_tree includes a node of depth n with |
|
113 | - // the valid_policy anyPolicy and the user-initial-policy-set |
|
114 | - // is not any-policy |
|
115 | - foreach ($tree->_nodesAtDepth($state->index()) as $node) { |
|
116 | - if ($node->hasParent() && $node->isAnyPolicy()) { |
|
117 | - // a. Set P-Q to the qualifier_set in the node of depth n |
|
118 | - // with valid_policy anyPolicy. |
|
119 | - $pq = $node->qualifiers(); |
|
120 | - // b. For each P-OID in the user-initial-policy-set that is not |
|
121 | - // the valid_policy of a node in the valid_policy_node_set, |
|
122 | - // create a child node whose parent is the node of depth n-1 |
|
123 | - // with the valid_policy anyPolicy. |
|
124 | - $poids = array_diff($policies, $valid_policy_set); |
|
125 | - foreach ($tree->_nodesAtDepth($state->index() - 1) as $parent) { |
|
126 | - if ($parent->isAnyPolicy()) { |
|
127 | - // Set the values in the child node as follows: |
|
128 | - // set the valid_policy to P-OID, set the qualifier_set |
|
129 | - // to P-Q, and set the expected_policy_set to {P-OID}. |
|
130 | - foreach ($poids as $poid) { |
|
131 | - $parent->addChild( |
|
132 | - new PolicyNode($poid, $pq, array($poid))); |
|
133 | - } |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - // c. Delete the node of depth n with the |
|
138 | - // valid_policy anyPolicy. |
|
139 | - $node->remove(); |
|
140 | - } |
|
141 | - } |
|
142 | - // 4. If there is a node in the valid_policy_tree of depth n-1 or less |
|
143 | - // without any child nodes, delete that node. Repeat this step until |
|
144 | - // there are no nodes of depth n-1 or less without children. |
|
145 | - if (!$tree->_pruneTree($state->index() - 1)) { |
|
146 | - return $state->withoutValidPolicyTree(); |
|
147 | - } |
|
148 | - return $state->withValidPolicyTree($tree); |
|
149 | - } |
|
82 | + /** |
|
83 | + * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g. |
|
84 | + * |
|
85 | + * @param ValidatorState $state |
|
86 | + * @param array $policies |
|
87 | + * @return ValidatorState |
|
88 | + */ |
|
89 | + public function calculateIntersection(ValidatorState $state, array $policies): ValidatorState |
|
90 | + { |
|
91 | + $tree = clone $this; |
|
92 | + $valid_policy_node_set = $tree->_validPolicyNodeSet(); |
|
93 | + // 2. If the valid_policy of any node in the valid_policy_node_set |
|
94 | + // is not in the user-initial-policy-set and is not anyPolicy, |
|
95 | + // delete this node and all its children. |
|
96 | + $valid_policy_node_set = array_filter($valid_policy_node_set, |
|
97 | + function (PolicyNode $node) use ($policies) { |
|
98 | + if ($node->isAnyPolicy()) { |
|
99 | + return true; |
|
100 | + } |
|
101 | + if (in_array($node->validPolicy(), $policies)) { |
|
102 | + return true; |
|
103 | + } |
|
104 | + $node->remove(); |
|
105 | + return false; |
|
106 | + }); |
|
107 | + // array of valid policy OIDs |
|
108 | + $valid_policy_set = array_map( |
|
109 | + function (PolicyNode $node) { |
|
110 | + return $node->validPolicy(); |
|
111 | + }, $valid_policy_node_set); |
|
112 | + // 3. If the valid_policy_tree includes a node of depth n with |
|
113 | + // the valid_policy anyPolicy and the user-initial-policy-set |
|
114 | + // is not any-policy |
|
115 | + foreach ($tree->_nodesAtDepth($state->index()) as $node) { |
|
116 | + if ($node->hasParent() && $node->isAnyPolicy()) { |
|
117 | + // a. Set P-Q to the qualifier_set in the node of depth n |
|
118 | + // with valid_policy anyPolicy. |
|
119 | + $pq = $node->qualifiers(); |
|
120 | + // b. For each P-OID in the user-initial-policy-set that is not |
|
121 | + // the valid_policy of a node in the valid_policy_node_set, |
|
122 | + // create a child node whose parent is the node of depth n-1 |
|
123 | + // with the valid_policy anyPolicy. |
|
124 | + $poids = array_diff($policies, $valid_policy_set); |
|
125 | + foreach ($tree->_nodesAtDepth($state->index() - 1) as $parent) { |
|
126 | + if ($parent->isAnyPolicy()) { |
|
127 | + // Set the values in the child node as follows: |
|
128 | + // set the valid_policy to P-OID, set the qualifier_set |
|
129 | + // to P-Q, and set the expected_policy_set to {P-OID}. |
|
130 | + foreach ($poids as $poid) { |
|
131 | + $parent->addChild( |
|
132 | + new PolicyNode($poid, $pq, array($poid))); |
|
133 | + } |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + // c. Delete the node of depth n with the |
|
138 | + // valid_policy anyPolicy. |
|
139 | + $node->remove(); |
|
140 | + } |
|
141 | + } |
|
142 | + // 4. If there is a node in the valid_policy_tree of depth n-1 or less |
|
143 | + // without any child nodes, delete that node. Repeat this step until |
|
144 | + // there are no nodes of depth n-1 or less without children. |
|
145 | + if (!$tree->_pruneTree($state->index() - 1)) { |
|
146 | + return $state->withoutValidPolicyTree(); |
|
147 | + } |
|
148 | + return $state->withValidPolicyTree($tree); |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get policies at given policy tree depth. |
|
153 | - * |
|
154 | - * @param int $i Depth in range 1..n |
|
155 | - * @return PolicyInformation[] |
|
156 | - */ |
|
157 | - public function policiesAtDepth($i): array |
|
158 | - { |
|
159 | - $policies = array(); |
|
160 | - foreach ($this->_nodesAtDepth($i) as $node) { |
|
161 | - $policies[] = new PolicyInformation($node->validPolicy(), |
|
162 | - ...$node->qualifiers()); |
|
163 | - } |
|
164 | - return $policies; |
|
165 | - } |
|
151 | + /** |
|
152 | + * Get policies at given policy tree depth. |
|
153 | + * |
|
154 | + * @param int $i Depth in range 1..n |
|
155 | + * @return PolicyInformation[] |
|
156 | + */ |
|
157 | + public function policiesAtDepth($i): array |
|
158 | + { |
|
159 | + $policies = array(); |
|
160 | + foreach ($this->_nodesAtDepth($i) as $node) { |
|
161 | + $policies[] = new PolicyInformation($node->validPolicy(), |
|
162 | + ...$node->qualifiers()); |
|
163 | + } |
|
164 | + return $policies; |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Process single policy information. |
|
169 | - * |
|
170 | - * @param PolicyInformation $policy |
|
171 | - * @param ValidatorState $state |
|
172 | - */ |
|
173 | - protected function _processPolicy(PolicyInformation $policy, |
|
174 | - ValidatorState $state) |
|
175 | - { |
|
176 | - $p_oid = $policy->oid(); |
|
177 | - $i = $state->index(); |
|
178 | - $match_count = 0; |
|
179 | - // (d.1.i) for each node of depth i-1 in the valid_policy_tree... |
|
180 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
181 | - // ...where P-OID is in the expected_policy_set |
|
182 | - if ($node->hasExpectedPolicy($p_oid)) { |
|
183 | - $node->addChild( |
|
184 | - new PolicyNode($p_oid, $policy->qualifiers(), array($p_oid))); |
|
185 | - ++$match_count; |
|
186 | - } |
|
187 | - } |
|
188 | - // (d.1.ii) if there was no match in step (i)... |
|
189 | - if (!$match_count) { |
|
190 | - // ...and the valid_policy_tree includes a node of depth i-1 with |
|
191 | - // the valid_policy anyPolicy |
|
192 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
193 | - if ($node->isAnyPolicy()) { |
|
194 | - $node->addChild( |
|
195 | - new PolicyNode($p_oid, $policy->qualifiers(), |
|
196 | - array($p_oid))); |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
167 | + /** |
|
168 | + * Process single policy information. |
|
169 | + * |
|
170 | + * @param PolicyInformation $policy |
|
171 | + * @param ValidatorState $state |
|
172 | + */ |
|
173 | + protected function _processPolicy(PolicyInformation $policy, |
|
174 | + ValidatorState $state) |
|
175 | + { |
|
176 | + $p_oid = $policy->oid(); |
|
177 | + $i = $state->index(); |
|
178 | + $match_count = 0; |
|
179 | + // (d.1.i) for each node of depth i-1 in the valid_policy_tree... |
|
180 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
181 | + // ...where P-OID is in the expected_policy_set |
|
182 | + if ($node->hasExpectedPolicy($p_oid)) { |
|
183 | + $node->addChild( |
|
184 | + new PolicyNode($p_oid, $policy->qualifiers(), array($p_oid))); |
|
185 | + ++$match_count; |
|
186 | + } |
|
187 | + } |
|
188 | + // (d.1.ii) if there was no match in step (i)... |
|
189 | + if (!$match_count) { |
|
190 | + // ...and the valid_policy_tree includes a node of depth i-1 with |
|
191 | + // the valid_policy anyPolicy |
|
192 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
193 | + if ($node->isAnyPolicy()) { |
|
194 | + $node->addChild( |
|
195 | + new PolicyNode($p_oid, $policy->qualifiers(), |
|
196 | + array($p_oid))); |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * Process anyPolicy policy information. |
|
204 | - * |
|
205 | - * @param PolicyInformation $policy |
|
206 | - * @param Certificate $cert |
|
207 | - * @param ValidatorState $state |
|
208 | - */ |
|
209 | - protected function _processAnyPolicy(PolicyInformation $policy, |
|
210 | - Certificate $cert, ValidatorState $state) |
|
211 | - { |
|
212 | - $i = $state->index(); |
|
213 | - // if (a) inhibit_anyPolicy is greater than 0 or |
|
214 | - // (b) i<n and the certificate is self-issued |
|
215 | - if (!($state->inhibitAnyPolicy() > 0 || |
|
216 | - ($i < $state->pathLength() && $cert->isSelfIssued()))) { |
|
217 | - return; |
|
218 | - } |
|
219 | - // for each node in the valid_policy_tree of depth i-1 |
|
220 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
221 | - // for each value in the expected_policy_set |
|
222 | - foreach ($node->expectedPolicies() as $p_oid) { |
|
223 | - // that does not appear in a child node |
|
224 | - if (!$node->hasChildWithValidPolicy($p_oid)) { |
|
225 | - $node->addChild( |
|
226 | - new PolicyNode($p_oid, $policy->qualifiers(), |
|
227 | - array($p_oid))); |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - } |
|
202 | + /** |
|
203 | + * Process anyPolicy policy information. |
|
204 | + * |
|
205 | + * @param PolicyInformation $policy |
|
206 | + * @param Certificate $cert |
|
207 | + * @param ValidatorState $state |
|
208 | + */ |
|
209 | + protected function _processAnyPolicy(PolicyInformation $policy, |
|
210 | + Certificate $cert, ValidatorState $state) |
|
211 | + { |
|
212 | + $i = $state->index(); |
|
213 | + // if (a) inhibit_anyPolicy is greater than 0 or |
|
214 | + // (b) i<n and the certificate is self-issued |
|
215 | + if (!($state->inhibitAnyPolicy() > 0 || |
|
216 | + ($i < $state->pathLength() && $cert->isSelfIssued()))) { |
|
217 | + return; |
|
218 | + } |
|
219 | + // for each node in the valid_policy_tree of depth i-1 |
|
220 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
221 | + // for each value in the expected_policy_set |
|
222 | + foreach ($node->expectedPolicies() as $p_oid) { |
|
223 | + // that does not appear in a child node |
|
224 | + if (!$node->hasChildWithValidPolicy($p_oid)) { |
|
225 | + $node->addChild( |
|
226 | + new PolicyNode($p_oid, $policy->qualifiers(), |
|
227 | + array($p_oid))); |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * Apply policy mappings to the policy tree. |
|
235 | - * |
|
236 | - * @param Certificate $cert |
|
237 | - * @param ValidatorState $state |
|
238 | - */ |
|
239 | - protected function _applyMappings(Certificate $cert, ValidatorState $state) |
|
240 | - { |
|
241 | - $policy_mappings = $cert->tbsCertificate() |
|
242 | - ->extensions() |
|
243 | - ->policyMappings(); |
|
244 | - // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i... |
|
245 | - foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
|
246 | - $match_count = 0; |
|
247 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
248 | - // ...where ID-P is the valid_policy |
|
249 | - if ($node->validPolicy() == $idp) { |
|
250 | - // set expected_policy_set to the set of subjectDomainPolicy |
|
251 | - // values that are specified as equivalent to ID-P by |
|
252 | - // the policy mappings extension |
|
253 | - $node->setExpectedPolicies(...$sdps); |
|
254 | - ++$match_count; |
|
255 | - } |
|
256 | - } |
|
257 | - // if no node of depth i in the valid_policy_tree has |
|
258 | - // a valid_policy of ID-P... |
|
259 | - if (!$match_count) { |
|
260 | - $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
|
261 | - } |
|
262 | - } |
|
263 | - } |
|
233 | + /** |
|
234 | + * Apply policy mappings to the policy tree. |
|
235 | + * |
|
236 | + * @param Certificate $cert |
|
237 | + * @param ValidatorState $state |
|
238 | + */ |
|
239 | + protected function _applyMappings(Certificate $cert, ValidatorState $state) |
|
240 | + { |
|
241 | + $policy_mappings = $cert->tbsCertificate() |
|
242 | + ->extensions() |
|
243 | + ->policyMappings(); |
|
244 | + // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i... |
|
245 | + foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
|
246 | + $match_count = 0; |
|
247 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
248 | + // ...where ID-P is the valid_policy |
|
249 | + if ($node->validPolicy() == $idp) { |
|
250 | + // set expected_policy_set to the set of subjectDomainPolicy |
|
251 | + // values that are specified as equivalent to ID-P by |
|
252 | + // the policy mappings extension |
|
253 | + $node->setExpectedPolicies(...$sdps); |
|
254 | + ++$match_count; |
|
255 | + } |
|
256 | + } |
|
257 | + // if no node of depth i in the valid_policy_tree has |
|
258 | + // a valid_policy of ID-P... |
|
259 | + if (!$match_count) { |
|
260 | + $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
|
261 | + } |
|
262 | + } |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1). |
|
267 | - * |
|
268 | - * @param Certificate $cert |
|
269 | - * @param ValidatorState $state |
|
270 | - * @param string $idp OID of the issuer domain policy |
|
271 | - * @param array $sdps Array of subject domain policy OIDs |
|
272 | - */ |
|
273 | - protected function _applyAnyPolicyMapping(Certificate $cert, |
|
274 | - ValidatorState $state, $idp, array $sdps) |
|
275 | - { |
|
276 | - // (6.1.4. b.1.) ...but there is a node of depth i with |
|
277 | - // a valid_policy of anyPolicy |
|
278 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
279 | - if ($node->isAnyPolicy()) { |
|
280 | - // then generate a child node of the node of depth i-1 |
|
281 | - // that has a valid_policy of anyPolicy as follows... |
|
282 | - foreach ($this->_nodesAtDepth($state->index() - 1) as $node) { |
|
283 | - if ($node->isAnyPolicy()) { |
|
284 | - // try to fetch qualifiers of anyPolicy certificate policy |
|
285 | - $qualifiers = array(); |
|
286 | - try { |
|
287 | - $qualifiers = $cert->tbsCertificate() |
|
288 | - ->extensions() |
|
289 | - ->certificatePolicies() |
|
290 | - ->anyPolicy() |
|
291 | - ->qualifiers(); |
|
292 | - } catch (\LogicException $e) { |
|
293 | - // if there's no policies or no qualifiers |
|
294 | - } |
|
295 | - $node->addChild( |
|
296 | - new PolicyNode($idp, $qualifiers, $sdps)); |
|
297 | - // bail after first anyPolicy has been processed |
|
298 | - break; |
|
299 | - } |
|
300 | - } |
|
301 | - // bail after first anyPolicy has been processed |
|
302 | - break; |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
265 | + /** |
|
266 | + * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1). |
|
267 | + * |
|
268 | + * @param Certificate $cert |
|
269 | + * @param ValidatorState $state |
|
270 | + * @param string $idp OID of the issuer domain policy |
|
271 | + * @param array $sdps Array of subject domain policy OIDs |
|
272 | + */ |
|
273 | + protected function _applyAnyPolicyMapping(Certificate $cert, |
|
274 | + ValidatorState $state, $idp, array $sdps) |
|
275 | + { |
|
276 | + // (6.1.4. b.1.) ...but there is a node of depth i with |
|
277 | + // a valid_policy of anyPolicy |
|
278 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
279 | + if ($node->isAnyPolicy()) { |
|
280 | + // then generate a child node of the node of depth i-1 |
|
281 | + // that has a valid_policy of anyPolicy as follows... |
|
282 | + foreach ($this->_nodesAtDepth($state->index() - 1) as $node) { |
|
283 | + if ($node->isAnyPolicy()) { |
|
284 | + // try to fetch qualifiers of anyPolicy certificate policy |
|
285 | + $qualifiers = array(); |
|
286 | + try { |
|
287 | + $qualifiers = $cert->tbsCertificate() |
|
288 | + ->extensions() |
|
289 | + ->certificatePolicies() |
|
290 | + ->anyPolicy() |
|
291 | + ->qualifiers(); |
|
292 | + } catch (\LogicException $e) { |
|
293 | + // if there's no policies or no qualifiers |
|
294 | + } |
|
295 | + $node->addChild( |
|
296 | + new PolicyNode($idp, $qualifiers, $sdps)); |
|
297 | + // bail after first anyPolicy has been processed |
|
298 | + break; |
|
299 | + } |
|
300 | + } |
|
301 | + // bail after first anyPolicy has been processed |
|
302 | + break; |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * Delete nodes as specified in 6.1.4 (b)(2). |
|
309 | - * |
|
310 | - * @param Certificate $cert |
|
311 | - * @param ValidatorState $state |
|
312 | - */ |
|
313 | - protected function _deleteMappings(Certificate $cert, ValidatorState $state) |
|
314 | - { |
|
315 | - $idps = $cert->tbsCertificate() |
|
316 | - ->extensions() |
|
317 | - ->policyMappings() |
|
318 | - ->issuerDomainPolicies(); |
|
319 | - // delete each node of depth i in the valid_policy_tree |
|
320 | - // where ID-P is the valid_policy |
|
321 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
322 | - if (in_array($node->validPolicy(), $idps)) { |
|
323 | - $node->remove(); |
|
324 | - } |
|
325 | - } |
|
326 | - $this->_pruneTree($state->index() - 1); |
|
327 | - } |
|
307 | + /** |
|
308 | + * Delete nodes as specified in 6.1.4 (b)(2). |
|
309 | + * |
|
310 | + * @param Certificate $cert |
|
311 | + * @param ValidatorState $state |
|
312 | + */ |
|
313 | + protected function _deleteMappings(Certificate $cert, ValidatorState $state) |
|
314 | + { |
|
315 | + $idps = $cert->tbsCertificate() |
|
316 | + ->extensions() |
|
317 | + ->policyMappings() |
|
318 | + ->issuerDomainPolicies(); |
|
319 | + // delete each node of depth i in the valid_policy_tree |
|
320 | + // where ID-P is the valid_policy |
|
321 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
322 | + if (in_array($node->validPolicy(), $idps)) { |
|
323 | + $node->remove(); |
|
324 | + } |
|
325 | + } |
|
326 | + $this->_pruneTree($state->index() - 1); |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * Prune tree starting from given depth. |
|
331 | - * |
|
332 | - * @param int $depth |
|
333 | - * @return int The number of nodes left in a tree |
|
334 | - */ |
|
335 | - protected function _pruneTree($depth) |
|
336 | - { |
|
337 | - for ($i = $depth; $i > 0; --$i) { |
|
338 | - foreach ($this->_nodesAtDepth($i) as $node) { |
|
339 | - if (!count($node)) { |
|
340 | - $node->remove(); |
|
341 | - } |
|
342 | - } |
|
343 | - } |
|
344 | - // if root has no children left |
|
345 | - if (!count($this->_root)) { |
|
346 | - $this->_root = null; |
|
347 | - return 0; |
|
348 | - } |
|
349 | - return $this->_root->nodeCount(); |
|
350 | - } |
|
329 | + /** |
|
330 | + * Prune tree starting from given depth. |
|
331 | + * |
|
332 | + * @param int $depth |
|
333 | + * @return int The number of nodes left in a tree |
|
334 | + */ |
|
335 | + protected function _pruneTree($depth) |
|
336 | + { |
|
337 | + for ($i = $depth; $i > 0; --$i) { |
|
338 | + foreach ($this->_nodesAtDepth($i) as $node) { |
|
339 | + if (!count($node)) { |
|
340 | + $node->remove(); |
|
341 | + } |
|
342 | + } |
|
343 | + } |
|
344 | + // if root has no children left |
|
345 | + if (!count($this->_root)) { |
|
346 | + $this->_root = null; |
|
347 | + return 0; |
|
348 | + } |
|
349 | + return $this->_root->nodeCount(); |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * Get all nodes at given depth. |
|
354 | - * |
|
355 | - * @param int $i |
|
356 | - * @return PolicyNode[] |
|
357 | - */ |
|
358 | - protected function _nodesAtDepth($i): array |
|
359 | - { |
|
360 | - if (!$this->_root) { |
|
361 | - return array(); |
|
362 | - } |
|
363 | - $depth = 0; |
|
364 | - $nodes = array($this->_root); |
|
365 | - while ($depth < $i) { |
|
366 | - $nodes = self::_gatherChildren(...$nodes); |
|
367 | - if (!count($nodes)) { |
|
368 | - break; |
|
369 | - } |
|
370 | - ++$depth; |
|
371 | - } |
|
372 | - return $nodes; |
|
373 | - } |
|
352 | + /** |
|
353 | + * Get all nodes at given depth. |
|
354 | + * |
|
355 | + * @param int $i |
|
356 | + * @return PolicyNode[] |
|
357 | + */ |
|
358 | + protected function _nodesAtDepth($i): array |
|
359 | + { |
|
360 | + if (!$this->_root) { |
|
361 | + return array(); |
|
362 | + } |
|
363 | + $depth = 0; |
|
364 | + $nodes = array($this->_root); |
|
365 | + while ($depth < $i) { |
|
366 | + $nodes = self::_gatherChildren(...$nodes); |
|
367 | + if (!count($nodes)) { |
|
368 | + break; |
|
369 | + } |
|
370 | + ++$depth; |
|
371 | + } |
|
372 | + return $nodes; |
|
373 | + } |
|
374 | 374 | |
375 | - /** |
|
376 | - * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1. |
|
377 | - * |
|
378 | - * @return PolicyNode[] |
|
379 | - */ |
|
380 | - protected function _validPolicyNodeSet(): array |
|
381 | - { |
|
382 | - // 1. Determine the set of policy nodes whose parent nodes have |
|
383 | - // a valid_policy of anyPolicy. This is the valid_policy_node_set. |
|
384 | - $set = array(); |
|
385 | - if (!$this->_root) { |
|
386 | - return $set; |
|
387 | - } |
|
388 | - // for each node in a tree |
|
389 | - $this->_root->walkNodes( |
|
390 | - function (PolicyNode $node) use (&$set) { |
|
391 | - $parents = $node->parents(); |
|
392 | - // node has parents |
|
393 | - if (count($parents)) { |
|
394 | - // check that each ancestor is an anyPolicy node |
|
395 | - foreach ($parents as $ancestor) { |
|
396 | - if (!$ancestor->isAnyPolicy()) { |
|
397 | - return; |
|
398 | - } |
|
399 | - } |
|
400 | - $set[] = $node; |
|
401 | - } |
|
402 | - }); |
|
403 | - return $set; |
|
404 | - } |
|
375 | + /** |
|
376 | + * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1. |
|
377 | + * |
|
378 | + * @return PolicyNode[] |
|
379 | + */ |
|
380 | + protected function _validPolicyNodeSet(): array |
|
381 | + { |
|
382 | + // 1. Determine the set of policy nodes whose parent nodes have |
|
383 | + // a valid_policy of anyPolicy. This is the valid_policy_node_set. |
|
384 | + $set = array(); |
|
385 | + if (!$this->_root) { |
|
386 | + return $set; |
|
387 | + } |
|
388 | + // for each node in a tree |
|
389 | + $this->_root->walkNodes( |
|
390 | + function (PolicyNode $node) use (&$set) { |
|
391 | + $parents = $node->parents(); |
|
392 | + // node has parents |
|
393 | + if (count($parents)) { |
|
394 | + // check that each ancestor is an anyPolicy node |
|
395 | + foreach ($parents as $ancestor) { |
|
396 | + if (!$ancestor->isAnyPolicy()) { |
|
397 | + return; |
|
398 | + } |
|
399 | + } |
|
400 | + $set[] = $node; |
|
401 | + } |
|
402 | + }); |
|
403 | + return $set; |
|
404 | + } |
|
405 | 405 | |
406 | - /** |
|
407 | - * Gather all children of given nodes to a flattened array. |
|
408 | - * |
|
409 | - * @param PolicyNode ...$nodes |
|
410 | - * @return PolicyNode[] |
|
411 | - */ |
|
412 | - private static function _gatherChildren(PolicyNode ...$nodes): array |
|
413 | - { |
|
414 | - $children = array(); |
|
415 | - foreach ($nodes as $node) { |
|
416 | - $children = array_merge($children, $node->children()); |
|
417 | - } |
|
418 | - return $children; |
|
419 | - } |
|
406 | + /** |
|
407 | + * Gather all children of given nodes to a flattened array. |
|
408 | + * |
|
409 | + * @param PolicyNode ...$nodes |
|
410 | + * @return PolicyNode[] |
|
411 | + */ |
|
412 | + private static function _gatherChildren(PolicyNode ...$nodes): array |
|
413 | + { |
|
414 | + $children = array(); |
|
415 | + foreach ($nodes as $node) { |
|
416 | + $children = array_merge($children, $node->children()); |
|
417 | + } |
|
418 | + return $children; |
|
419 | + } |
|
420 | 420 | } |
@@ -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 X509\CertificationPath\Policy; |
6 | 6 | |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | // is not in the user-initial-policy-set and is not anyPolicy, |
95 | 95 | // delete this node and all its children. |
96 | 96 | $valid_policy_node_set = array_filter($valid_policy_node_set, |
97 | - function (PolicyNode $node) use ($policies) { |
|
97 | + function(PolicyNode $node) use ($policies) { |
|
98 | 98 | if ($node->isAnyPolicy()) { |
99 | 99 | return true; |
100 | 100 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | }); |
107 | 107 | // array of valid policy OIDs |
108 | 108 | $valid_policy_set = array_map( |
109 | - function (PolicyNode $node) { |
|
109 | + function(PolicyNode $node) { |
|
110 | 110 | return $node->validPolicy(); |
111 | 111 | }, $valid_policy_node_set); |
112 | 112 | // 3. If the valid_policy_tree includes a node of depth n with |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | } |
388 | 388 | // for each node in a tree |
389 | 389 | $this->_root->walkNodes( |
390 | - function (PolicyNode $node) use (&$set) { |
|
390 | + function(PolicyNode $node) use (&$set) { |
|
391 | 391 | $parents = $node->parents(); |
392 | 392 | // node has parents |
393 | 393 | if (count($parents)) { |