GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 405cf3...79c9ba )
by Joni
04:48
created
lib/X509/Certificate/Extension/AuthorityKeyIdentifierExtension.php 2 patches
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -20,185 +20,185 @@
 block discarded – undo
20 20
  */
21 21
 class AuthorityKeyIdentifierExtension extends Extension
22 22
 {
23
-    /**
24
-     * Key identifier.
25
-     *
26
-     * @var null|string
27
-     */
28
-    protected $_keyIdentifier;
29
-
30
-    /**
31
-     * Issuer name.
32
-     *
33
-     * @var null|GeneralNames
34
-     */
35
-    protected $_authorityCertIssuer;
36
-
37
-    /**
38
-     * Issuer serial number as a base 10 integer.
39
-     *
40
-     * @var null|string
41
-     */
42
-    protected $_authorityCertSerialNumber;
43
-
44
-    /**
45
-     * Constructor.
46
-     *
47
-     * @param bool              $critical      Conforming CA's must mark as non-critical (false)
48
-     * @param null|string       $keyIdentifier Key identifier
49
-     * @param null|GeneralNames $issuer        Issuer name
50
-     * @param null|int|string   $serial        Issuer serial number as a base 10 integer
51
-     */
52
-    public function __construct(bool $critical, ?string $keyIdentifier,
53
-        ?GeneralNames $issuer = null, $serial = null)
54
-    {
55
-        parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical);
56
-        $this->_keyIdentifier = $keyIdentifier;
57
-        $this->_authorityCertIssuer = $issuer;
58
-        $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null;
59
-    }
60
-
61
-    /**
62
-     * Create from public key info.
63
-     *
64
-     * @param PublicKeyInfo $pki
65
-     *
66
-     * @return AuthorityKeyIdentifierExtension
67
-     */
68
-    public static function fromPublicKeyInfo(PublicKeyInfo $pki): self
69
-    {
70
-        return new self(false, $pki->keyIdentifier());
71
-    }
72
-
73
-    /**
74
-     * Whether key identifier is present.
75
-     *
76
-     * @return bool
77
-     */
78
-    public function hasKeyIdentifier(): bool
79
-    {
80
-        return isset($this->_keyIdentifier);
81
-    }
82
-
83
-    /**
84
-     * Get key identifier.
85
-     *
86
-     * @throws \LogicException If not set
87
-     *
88
-     * @return string
89
-     */
90
-    public function keyIdentifier(): string
91
-    {
92
-        if (!$this->hasKeyIdentifier()) {
93
-            throw new \LogicException('keyIdentifier not set.');
94
-        }
95
-        return $this->_keyIdentifier;
96
-    }
97
-
98
-    /**
99
-     * Whether issuer is present.
100
-     *
101
-     * @return bool
102
-     */
103
-    public function hasIssuer(): bool
104
-    {
105
-        return isset($this->_authorityCertIssuer);
106
-    }
107
-
108
-    /**
109
-     * Get issuer.
110
-     *
111
-     * @throws \LogicException If not set
112
-     *
113
-     * @return GeneralNames
114
-     */
115
-    public function issuer(): GeneralNames
116
-    {
117
-        if (!$this->hasIssuer()) {
118
-            throw new \LogicException('authorityCertIssuer not set.');
119
-        }
120
-        return $this->_authorityCertIssuer;
121
-    }
122
-
123
-    /**
124
-     * Whether serial is present.
125
-     *
126
-     * @return bool
127
-     */
128
-    public function hasSerial(): bool
129
-    {
130
-        return isset($this->_authorityCertSerialNumber);
131
-    }
132
-
133
-    /**
134
-     * Get serial number.
135
-     *
136
-     * @throws \LogicException If not set
137
-     *
138
-     * @return string Base 10 integer string
139
-     */
140
-    public function serial(): string
141
-    {
142
-        if (!$this->hasSerial()) {
143
-            throw new \LogicException('authorityCertSerialNumber not set.');
144
-        }
145
-        return $this->_authorityCertSerialNumber;
146
-    }
147
-
148
-    /**
149
-     * {@inheritdoc}
150
-     */
151
-    protected static function _fromDER(string $data, bool $critical): Extension
152
-    {
153
-        $seq = UnspecifiedType::fromDER($data)->asSequence();
154
-        $keyIdentifier = null;
155
-        $issuer = null;
156
-        $serial = null;
157
-        if ($seq->hasTagged(0)) {
158
-            $keyIdentifier = $seq->getTagged(0)
159
-                ->asImplicit(Element::TYPE_OCTET_STRING)
160
-                ->asOctetString()->string();
161
-        }
162
-        if ($seq->hasTagged(1) || $seq->hasTagged(2)) {
163
-            if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) {
164
-                throw new \UnexpectedValueException(
165
-                    'AuthorityKeyIdentifier must have both' .
166
-                        ' authorityCertIssuer and authorityCertSerialNumber' .
167
-                        ' present or both absent.');
168
-            }
169
-            $issuer = GeneralNames::fromASN1($seq->getTagged(1)
170
-                ->asImplicit(Element::TYPE_SEQUENCE)->asSequence());
171
-            $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER)
172
-                ->asInteger()->number();
173
-        }
174
-        return new self($critical, $keyIdentifier, $issuer, $serial);
175
-    }
176
-
177
-    /**
178
-     * {@inheritdoc}
179
-     */
180
-    protected function _valueASN1(): Element
181
-    {
182
-        $elements = [];
183
-        if (isset($this->_keyIdentifier)) {
184
-            $elements[] = new ImplicitlyTaggedType(0,
185
-                new OctetString($this->_keyIdentifier));
186
-        }
187
-        // if either issuer or serial is set, both must be set
188
-        if (isset($this->_authorityCertIssuer) ||
189
-             isset($this->_authorityCertSerialNumber)) {
190
-            if (!isset($this->_authorityCertIssuer,
191
-                $this->_authorityCertSerialNumber)) {
192
-                throw new \LogicException(
193
-                    'AuthorityKeyIdentifier must have both' .
194
-                        ' authorityCertIssuer and authorityCertSerialNumber' .
195
-                        ' present or both absent.');
196
-            }
197
-            $elements[] = new ImplicitlyTaggedType(1,
198
-                $this->_authorityCertIssuer->toASN1());
199
-            $elements[] = new ImplicitlyTaggedType(2,
200
-                new Integer($this->_authorityCertSerialNumber));
201
-        }
202
-        return new Sequence(...$elements);
203
-    }
23
+	/**
24
+	 * Key identifier.
25
+	 *
26
+	 * @var null|string
27
+	 */
28
+	protected $_keyIdentifier;
29
+
30
+	/**
31
+	 * Issuer name.
32
+	 *
33
+	 * @var null|GeneralNames
34
+	 */
35
+	protected $_authorityCertIssuer;
36
+
37
+	/**
38
+	 * Issuer serial number as a base 10 integer.
39
+	 *
40
+	 * @var null|string
41
+	 */
42
+	protected $_authorityCertSerialNumber;
43
+
44
+	/**
45
+	 * Constructor.
46
+	 *
47
+	 * @param bool              $critical      Conforming CA's must mark as non-critical (false)
48
+	 * @param null|string       $keyIdentifier Key identifier
49
+	 * @param null|GeneralNames $issuer        Issuer name
50
+	 * @param null|int|string   $serial        Issuer serial number as a base 10 integer
51
+	 */
52
+	public function __construct(bool $critical, ?string $keyIdentifier,
53
+		?GeneralNames $issuer = null, $serial = null)
54
+	{
55
+		parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical);
56
+		$this->_keyIdentifier = $keyIdentifier;
57
+		$this->_authorityCertIssuer = $issuer;
58
+		$this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null;
59
+	}
60
+
61
+	/**
62
+	 * Create from public key info.
63
+	 *
64
+	 * @param PublicKeyInfo $pki
65
+	 *
66
+	 * @return AuthorityKeyIdentifierExtension
67
+	 */
68
+	public static function fromPublicKeyInfo(PublicKeyInfo $pki): self
69
+	{
70
+		return new self(false, $pki->keyIdentifier());
71
+	}
72
+
73
+	/**
74
+	 * Whether key identifier is present.
75
+	 *
76
+	 * @return bool
77
+	 */
78
+	public function hasKeyIdentifier(): bool
79
+	{
80
+		return isset($this->_keyIdentifier);
81
+	}
82
+
83
+	/**
84
+	 * Get key identifier.
85
+	 *
86
+	 * @throws \LogicException If not set
87
+	 *
88
+	 * @return string
89
+	 */
90
+	public function keyIdentifier(): string
91
+	{
92
+		if (!$this->hasKeyIdentifier()) {
93
+			throw new \LogicException('keyIdentifier not set.');
94
+		}
95
+		return $this->_keyIdentifier;
96
+	}
97
+
98
+	/**
99
+	 * Whether issuer is present.
100
+	 *
101
+	 * @return bool
102
+	 */
103
+	public function hasIssuer(): bool
104
+	{
105
+		return isset($this->_authorityCertIssuer);
106
+	}
107
+
108
+	/**
109
+	 * Get issuer.
110
+	 *
111
+	 * @throws \LogicException If not set
112
+	 *
113
+	 * @return GeneralNames
114
+	 */
115
+	public function issuer(): GeneralNames
116
+	{
117
+		if (!$this->hasIssuer()) {
118
+			throw new \LogicException('authorityCertIssuer not set.');
119
+		}
120
+		return $this->_authorityCertIssuer;
121
+	}
122
+
123
+	/**
124
+	 * Whether serial is present.
125
+	 *
126
+	 * @return bool
127
+	 */
128
+	public function hasSerial(): bool
129
+	{
130
+		return isset($this->_authorityCertSerialNumber);
131
+	}
132
+
133
+	/**
134
+	 * Get serial number.
135
+	 *
136
+	 * @throws \LogicException If not set
137
+	 *
138
+	 * @return string Base 10 integer string
139
+	 */
140
+	public function serial(): string
141
+	{
142
+		if (!$this->hasSerial()) {
143
+			throw new \LogicException('authorityCertSerialNumber not set.');
144
+		}
145
+		return $this->_authorityCertSerialNumber;
146
+	}
147
+
148
+	/**
149
+	 * {@inheritdoc}
150
+	 */
151
+	protected static function _fromDER(string $data, bool $critical): Extension
152
+	{
153
+		$seq = UnspecifiedType::fromDER($data)->asSequence();
154
+		$keyIdentifier = null;
155
+		$issuer = null;
156
+		$serial = null;
157
+		if ($seq->hasTagged(0)) {
158
+			$keyIdentifier = $seq->getTagged(0)
159
+				->asImplicit(Element::TYPE_OCTET_STRING)
160
+				->asOctetString()->string();
161
+		}
162
+		if ($seq->hasTagged(1) || $seq->hasTagged(2)) {
163
+			if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) {
164
+				throw new \UnexpectedValueException(
165
+					'AuthorityKeyIdentifier must have both' .
166
+						' authorityCertIssuer and authorityCertSerialNumber' .
167
+						' present or both absent.');
168
+			}
169
+			$issuer = GeneralNames::fromASN1($seq->getTagged(1)
170
+				->asImplicit(Element::TYPE_SEQUENCE)->asSequence());
171
+			$serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER)
172
+				->asInteger()->number();
173
+		}
174
+		return new self($critical, $keyIdentifier, $issuer, $serial);
175
+	}
176
+
177
+	/**
178
+	 * {@inheritdoc}
179
+	 */
180
+	protected function _valueASN1(): Element
181
+	{
182
+		$elements = [];
183
+		if (isset($this->_keyIdentifier)) {
184
+			$elements[] = new ImplicitlyTaggedType(0,
185
+				new OctetString($this->_keyIdentifier));
186
+		}
187
+		// if either issuer or serial is set, both must be set
188
+		if (isset($this->_authorityCertIssuer) ||
189
+			 isset($this->_authorityCertSerialNumber)) {
190
+			if (!isset($this->_authorityCertIssuer,
191
+				$this->_authorityCertSerialNumber)) {
192
+				throw new \LogicException(
193
+					'AuthorityKeyIdentifier must have both' .
194
+						' authorityCertIssuer and authorityCertSerialNumber' .
195
+						' present or both absent.');
196
+			}
197
+			$elements[] = new ImplicitlyTaggedType(1,
198
+				$this->_authorityCertIssuer->toASN1());
199
+			$elements[] = new ImplicitlyTaggedType(2,
200
+				new Integer($this->_authorityCertSerialNumber));
201
+		}
202
+		return new Sequence(...$elements);
203
+	}
204 204
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
lib/X509/Certificate/Time.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -17,108 +17,108 @@
 block discarded – undo
17 17
  */
18 18
 class Time
19 19
 {
20
-    use DateTimeHelper;
20
+	use DateTimeHelper;
21 21
 
22
-    /**
23
-     * Datetime.
24
-     *
25
-     * @var \DateTimeImmutable
26
-     */
27
-    protected $_dt;
22
+	/**
23
+	 * Datetime.
24
+	 *
25
+	 * @var \DateTimeImmutable
26
+	 */
27
+	protected $_dt;
28 28
 
29
-    /**
30
-     * Time ASN.1 type tag.
31
-     *
32
-     * @var int
33
-     */
34
-    protected $_type;
29
+	/**
30
+	 * Time ASN.1 type tag.
31
+	 *
32
+	 * @var int
33
+	 */
34
+	protected $_type;
35 35
 
36
-    /**
37
-     * Constructor.
38
-     *
39
-     * @param \DateTimeImmutable $dt
40
-     */
41
-    public function __construct(\DateTimeImmutable $dt)
42
-    {
43
-        $this->_dt = $dt;
44
-        $this->_type = self::_determineType($dt);
45
-    }
36
+	/**
37
+	 * Constructor.
38
+	 *
39
+	 * @param \DateTimeImmutable $dt
40
+	 */
41
+	public function __construct(\DateTimeImmutable $dt)
42
+	{
43
+		$this->_dt = $dt;
44
+		$this->_type = self::_determineType($dt);
45
+	}
46 46
 
47
-    /**
48
-     * Initialize from ASN.1.
49
-     *
50
-     * @param TimeType $el
51
-     *
52
-     * @return self
53
-     */
54
-    public static function fromASN1(TimeType $el): self
55
-    {
56
-        $obj = new self($el->dateTime());
57
-        $obj->_type = $el->tag();
58
-        return $obj;
59
-    }
47
+	/**
48
+	 * Initialize from ASN.1.
49
+	 *
50
+	 * @param TimeType $el
51
+	 *
52
+	 * @return self
53
+	 */
54
+	public static function fromASN1(TimeType $el): self
55
+	{
56
+		$obj = new self($el->dateTime());
57
+		$obj->_type = $el->tag();
58
+		return $obj;
59
+	}
60 60
 
61
-    /**
62
-     * Initialize from date string.
63
-     *
64
-     * @param null|string $time
65
-     * @param null|string $tz
66
-     *
67
-     * @return self
68
-     */
69
-    public static function fromString(?string $time, ?string $tz = null): self
70
-    {
71
-        return new self(self::_createDateTime($time, $tz));
72
-    }
61
+	/**
62
+	 * Initialize from date string.
63
+	 *
64
+	 * @param null|string $time
65
+	 * @param null|string $tz
66
+	 *
67
+	 * @return self
68
+	 */
69
+	public static function fromString(?string $time, ?string $tz = null): self
70
+	{
71
+		return new self(self::_createDateTime($time, $tz));
72
+	}
73 73
 
74
-    /**
75
-     * Get datetime.
76
-     *
77
-     * @return \DateTimeImmutable
78
-     */
79
-    public function dateTime(): \DateTimeImmutable
80
-    {
81
-        return $this->_dt;
82
-    }
74
+	/**
75
+	 * Get datetime.
76
+	 *
77
+	 * @return \DateTimeImmutable
78
+	 */
79
+	public function dateTime(): \DateTimeImmutable
80
+	{
81
+		return $this->_dt;
82
+	}
83 83
 
84
-    /**
85
-     * Generate ASN.1.
86
-     *
87
-     * @throws \UnexpectedValueException
88
-     *
89
-     * @return TimeType
90
-     */
91
-    public function toASN1(): TimeType
92
-    {
93
-        $dt = $this->_dt;
94
-        switch ($this->_type) {
95
-            case Element::TYPE_UTC_TIME:
96
-                return new UTCTime($dt);
97
-            case Element::TYPE_GENERALIZED_TIME:
98
-                // GeneralizedTime must not contain fractional seconds
99
-                // (rfc5280 4.1.2.5.2)
100
-                if (0 !== intval($dt->format('u'))) {
101
-                    // remove fractional seconds (round down)
102
-                    $dt = self::_roundDownFractionalSeconds($dt);
103
-                }
104
-                return new GeneralizedTime($dt);
105
-        }
106
-        throw new \UnexpectedValueException(
107
-            'Time type ' . Element::tagToName($this->_type) . ' not supported.');
108
-    }
84
+	/**
85
+	 * Generate ASN.1.
86
+	 *
87
+	 * @throws \UnexpectedValueException
88
+	 *
89
+	 * @return TimeType
90
+	 */
91
+	public function toASN1(): TimeType
92
+	{
93
+		$dt = $this->_dt;
94
+		switch ($this->_type) {
95
+			case Element::TYPE_UTC_TIME:
96
+				return new UTCTime($dt);
97
+			case Element::TYPE_GENERALIZED_TIME:
98
+				// GeneralizedTime must not contain fractional seconds
99
+				// (rfc5280 4.1.2.5.2)
100
+				if (0 !== intval($dt->format('u'))) {
101
+					// remove fractional seconds (round down)
102
+					$dt = self::_roundDownFractionalSeconds($dt);
103
+				}
104
+				return new GeneralizedTime($dt);
105
+		}
106
+		throw new \UnexpectedValueException(
107
+			'Time type ' . Element::tagToName($this->_type) . ' not supported.');
108
+	}
109 109
 
110
-    /**
111
-     * Determine whether to use UTCTime or GeneralizedTime ASN.1 type.
112
-     *
113
-     * @param \DateTimeImmutable $dt
114
-     *
115
-     * @return int Type tag
116
-     */
117
-    protected static function _determineType(\DateTimeImmutable $dt): int
118
-    {
119
-        if ($dt->format('Y') >= 2050) {
120
-            return Element::TYPE_GENERALIZED_TIME;
121
-        }
122
-        return Element::TYPE_UTC_TIME;
123
-    }
110
+	/**
111
+	 * Determine whether to use UTCTime or GeneralizedTime ASN.1 type.
112
+	 *
113
+	 * @param \DateTimeImmutable $dt
114
+	 *
115
+	 * @return int Type tag
116
+	 */
117
+	protected static function _determineType(\DateTimeImmutable $dt): int
118
+	{
119
+		if ($dt->format('Y') >= 2050) {
120
+			return Element::TYPE_GENERALIZED_TIME;
121
+		}
122
+		return Element::TYPE_UTC_TIME;
123
+	}
124 124
 }
Please login to merge, or discard this patch.
Switch Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -92,16 +92,16 @@
 block discarded – undo
92 92
     {
93 93
         $dt = $this->_dt;
94 94
         switch ($this->_type) {
95
-            case Element::TYPE_UTC_TIME:
96
-                return new UTCTime($dt);
97
-            case Element::TYPE_GENERALIZED_TIME:
98
-                // GeneralizedTime must not contain fractional seconds
99
-                // (rfc5280 4.1.2.5.2)
100
-                if (0 !== intval($dt->format('u'))) {
101
-                    // remove fractional seconds (round down)
102
-                    $dt = self::_roundDownFractionalSeconds($dt);
103
-                }
104
-                return new GeneralizedTime($dt);
95
+        case Element::TYPE_UTC_TIME:
96
+            return new UTCTime($dt);
97
+        case Element::TYPE_GENERALIZED_TIME:
98
+            // GeneralizedTime must not contain fractional seconds
99
+            // (rfc5280 4.1.2.5.2)
100
+            if (0 !== intval($dt->format('u'))) {
101
+                // remove fractional seconds (round down)
102
+                $dt = self::_roundDownFractionalSeconds($dt);
103
+            }
104
+            return new GeneralizedTime($dt);
105 105
         }
106 106
         throw new \UnexpectedValueException(
107 107
             'Time type ' . Element::tagToName($this->_type) . ' not supported.');
Please login to merge, or discard this patch.
lib/X509/Certificate/TBSCertificate.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +618 added lines, -618 removed lines patch added patch discarded remove patch
@@ -27,622 +27,622 @@
 block discarded – undo
27 27
  */
28 28
 class TBSCertificate
29 29
 {
30
-    // Certificate version enumerations
31
-    const VERSION_1 = 0;
32
-    const VERSION_2 = 1;
33
-    const VERSION_3 = 2;
34
-
35
-    /**
36
-     * Certificate version.
37
-     *
38
-     * @var null|int
39
-     */
40
-    protected $_version;
41
-
42
-    /**
43
-     * Serial number.
44
-     *
45
-     * @var null|string
46
-     */
47
-    protected $_serialNumber;
48
-
49
-    /**
50
-     * Signature algorithm.
51
-     *
52
-     * @var null|SignatureAlgorithmIdentifier
53
-     */
54
-    protected $_signature;
55
-
56
-    /**
57
-     * Certificate issuer.
58
-     *
59
-     * @var Name
60
-     */
61
-    protected $_issuer;
62
-
63
-    /**
64
-     * Certificate validity period.
65
-     *
66
-     * @var Validity
67
-     */
68
-    protected $_validity;
69
-
70
-    /**
71
-     * Certificate subject.
72
-     *
73
-     * @var Name
74
-     */
75
-    protected $_subject;
76
-
77
-    /**
78
-     * Subject public key.
79
-     *
80
-     * @var PublicKeyInfo
81
-     */
82
-    protected $_subjectPublicKeyInfo;
83
-
84
-    /**
85
-     * Issuer unique identifier.
86
-     *
87
-     * @var null|UniqueIdentifier
88
-     */
89
-    protected $_issuerUniqueID;
90
-
91
-    /**
92
-     * Subject unique identifier.
93
-     *
94
-     * @var null|UniqueIdentifier
95
-     */
96
-    protected $_subjectUniqueID;
97
-
98
-    /**
99
-     * Extensions.
100
-     *
101
-     * @var Extensions
102
-     */
103
-    protected $_extensions;
104
-
105
-    /**
106
-     * Constructor.
107
-     *
108
-     * @param Name          $subject  Certificate subject
109
-     * @param PublicKeyInfo $pki      Subject public key
110
-     * @param Name          $issuer   Certificate issuer
111
-     * @param Validity      $validity Validity period
112
-     */
113
-    public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer,
114
-        Validity $validity)
115
-    {
116
-        $this->_subject = $subject;
117
-        $this->_subjectPublicKeyInfo = $pki;
118
-        $this->_issuer = $issuer;
119
-        $this->_validity = $validity;
120
-        $this->_extensions = new Extensions();
121
-    }
122
-
123
-    /**
124
-     * Initialize from ASN.1.
125
-     *
126
-     * @param Sequence $seq
127
-     *
128
-     * @return self
129
-     */
130
-    public static function fromASN1(Sequence $seq): self
131
-    {
132
-        $idx = 0;
133
-        if ($seq->hasTagged(0)) {
134
-            ++$idx;
135
-            $version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber();
136
-        } else {
137
-            $version = self::VERSION_1;
138
-        }
139
-        $serial = $seq->at($idx++)->asInteger()->number();
140
-        $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence());
141
-        if (!$algo instanceof SignatureAlgorithmIdentifier) {
142
-            throw new \UnexpectedValueException(
143
-                'Unsupported signature algorithm ' . $algo->name() . '.');
144
-        }
145
-        $issuer = Name::fromASN1($seq->at($idx++)->asSequence());
146
-        $validity = Validity::fromASN1($seq->at($idx++)->asSequence());
147
-        $subject = Name::fromASN1($seq->at($idx++)->asSequence());
148
-        $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence());
149
-        $tbs_cert = new self($subject, $pki, $issuer, $validity);
150
-        $tbs_cert->_version = $version;
151
-        $tbs_cert->_serialNumber = $serial;
152
-        $tbs_cert->_signature = $algo;
153
-        if ($seq->hasTagged(1)) {
154
-            $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1(
155
-                $seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING)
156
-                    ->asBitString());
157
-        }
158
-        if ($seq->hasTagged(2)) {
159
-            $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1(
160
-                $seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING)
161
-                    ->asBitString());
162
-        }
163
-        if ($seq->hasTagged(3)) {
164
-            $tbs_cert->_extensions = Extensions::fromASN1(
165
-                $seq->getTagged(3)->asExplicit()->asSequence());
166
-        }
167
-        return $tbs_cert;
168
-    }
169
-
170
-    /**
171
-     * Initialize from certification request.
172
-     *
173
-     * Note that signature is not verified and must be done by the caller.
174
-     *
175
-     * @param CertificationRequest $cr
176
-     *
177
-     * @return self
178
-     */
179
-    public static function fromCSR(CertificationRequest $cr): self
180
-    {
181
-        $cri = $cr->certificationRequestInfo();
182
-        $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(),
183
-            Validity::fromStrings(null, null));
184
-        // if CSR has Extension Request attribute
185
-        if ($cri->hasAttributes()) {
186
-            $attribs = $cri->attributes();
187
-            if ($attribs->hasExtensionRequest()) {
188
-                $tbs_cert = $tbs_cert->withExtensions(
189
-                    $attribs->extensionRequest()->extensions());
190
-            }
191
-        }
192
-        // add Subject Key Identifier extension
193
-        return $tbs_cert->withAdditionalExtensions(
194
-            new SubjectKeyIdentifierExtension(false,
195
-                $cri->subjectPKInfo()->keyIdentifier()));
196
-    }
197
-
198
-    /**
199
-     * Get self with fields set from the issuer's certificate.
200
-     *
201
-     * Issuer shall be set to issuing certificate's subject.
202
-     * Authority key identifier extensions shall be added with a key identifier
203
-     * set to issuing certificate's public key identifier.
204
-     *
205
-     * @param Certificate $cert Issuing party's certificate
206
-     *
207
-     * @return self
208
-     */
209
-    public function withIssuerCertificate(Certificate $cert): self
210
-    {
211
-        $obj = clone $this;
212
-        // set issuer DN from cert's subject
213
-        $obj->_issuer = $cert->tbsCertificate()->subject();
214
-        // add authority key identifier extension
215
-        $key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier();
216
-        $obj->_extensions = $obj->_extensions->withExtensions(
217
-            new AuthorityKeyIdentifierExtension(false, $key_id));
218
-        return $obj;
219
-    }
220
-
221
-    /**
222
-     * Get self with given version.
223
-     *
224
-     * If version is not set, appropriate version is automatically
225
-     * determined during signing.
226
-     *
227
-     * @param int $version
228
-     *
229
-     * @return self
230
-     */
231
-    public function withVersion(int $version): self
232
-    {
233
-        $obj = clone $this;
234
-        $obj->_version = $version;
235
-        return $obj;
236
-    }
237
-
238
-    /**
239
-     * Get self with given serial number.
240
-     *
241
-     * @param int|string $serial Base 10 number
242
-     *
243
-     * @return self
244
-     */
245
-    public function withSerialNumber($serial): self
246
-    {
247
-        $obj = clone $this;
248
-        $obj->_serialNumber = strval($serial);
249
-        return $obj;
250
-    }
251
-
252
-    /**
253
-     * Get self with random positive serial number.
254
-     *
255
-     * @param int $size Number of random bytes
256
-     *
257
-     * @return self
258
-     */
259
-    public function withRandomSerialNumber(int $size = 16): self
260
-    {
261
-        // ensure that first byte is always non-zero and having first bit unset
262
-        $num = gmp_init(mt_rand(1, 0x7f), 10);
263
-        for ($i = 1; $i < $size; ++$i) {
264
-            $num <<= 8;
265
-            $num += mt_rand(0, 0xff);
266
-        }
267
-        return $this->withSerialNumber(gmp_strval($num, 10));
268
-    }
269
-
270
-    /**
271
-     * Get self with given signature algorithm.
272
-     *
273
-     * @param SignatureAlgorithmIdentifier $algo
274
-     *
275
-     * @return self
276
-     */
277
-    public function withSignature(SignatureAlgorithmIdentifier $algo): self
278
-    {
279
-        $obj = clone $this;
280
-        $obj->_signature = $algo;
281
-        return $obj;
282
-    }
283
-
284
-    /**
285
-     * Get self with given issuer.
286
-     *
287
-     * @param Name $issuer
288
-     *
289
-     * @return self
290
-     */
291
-    public function withIssuer(Name $issuer): self
292
-    {
293
-        $obj = clone $this;
294
-        $obj->_issuer = $issuer;
295
-        return $obj;
296
-    }
297
-
298
-    /**
299
-     * Get self with given validity.
300
-     *
301
-     * @param Validity $validity
302
-     *
303
-     * @return self
304
-     */
305
-    public function withValidity(Validity $validity): self
306
-    {
307
-        $obj = clone $this;
308
-        $obj->_validity = $validity;
309
-        return $obj;
310
-    }
311
-
312
-    /**
313
-     * Get self with given subject.
314
-     *
315
-     * @param Name $subject
316
-     *
317
-     * @return self
318
-     */
319
-    public function withSubject(Name $subject): self
320
-    {
321
-        $obj = clone $this;
322
-        $obj->_subject = $subject;
323
-        return $obj;
324
-    }
325
-
326
-    /**
327
-     * Get self with given subject public key info.
328
-     *
329
-     * @param PublicKeyInfo $pub_key_info
330
-     *
331
-     * @return self
332
-     */
333
-    public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self
334
-    {
335
-        $obj = clone $this;
336
-        $obj->_subjectPublicKeyInfo = $pub_key_info;
337
-        return $obj;
338
-    }
339
-
340
-    /**
341
-     * Get self with issuer unique ID.
342
-     *
343
-     * @param UniqueIdentifier $id
344
-     *
345
-     * @return self
346
-     */
347
-    public function withIssuerUniqueID(UniqueIdentifier $id): self
348
-    {
349
-        $obj = clone $this;
350
-        $obj->_issuerUniqueID = $id;
351
-        return $obj;
352
-    }
353
-
354
-    /**
355
-     * Get self with subject unique ID.
356
-     *
357
-     * @param UniqueIdentifier $id
358
-     *
359
-     * @return self
360
-     */
361
-    public function withSubjectUniqueID(UniqueIdentifier $id): self
362
-    {
363
-        $obj = clone $this;
364
-        $obj->_subjectUniqueID = $id;
365
-        return $obj;
366
-    }
367
-
368
-    /**
369
-     * Get self with given extensions.
370
-     *
371
-     * @param Extensions $extensions
372
-     *
373
-     * @return self
374
-     */
375
-    public function withExtensions(Extensions $extensions): self
376
-    {
377
-        $obj = clone $this;
378
-        $obj->_extensions = $extensions;
379
-        return $obj;
380
-    }
381
-
382
-    /**
383
-     * Get self with extensions added.
384
-     *
385
-     * @param Extension ...$exts One or more Extension objects
386
-     *
387
-     * @return self
388
-     */
389
-    public function withAdditionalExtensions(Extension ...$exts): self
390
-    {
391
-        $obj = clone $this;
392
-        $obj->_extensions = $obj->_extensions->withExtensions(...$exts);
393
-        return $obj;
394
-    }
395
-
396
-    /**
397
-     * Check whether version is set.
398
-     *
399
-     * @return bool
400
-     */
401
-    public function hasVersion(): bool
402
-    {
403
-        return isset($this->_version);
404
-    }
405
-
406
-    /**
407
-     * Get certificate version.
408
-     *
409
-     * @throws \LogicException If not set
410
-     *
411
-     * @return int
412
-     */
413
-    public function version(): int
414
-    {
415
-        if (!$this->hasVersion()) {
416
-            throw new \LogicException('version not set.');
417
-        }
418
-        return $this->_version;
419
-    }
420
-
421
-    /**
422
-     * Check whether serial number is set.
423
-     *
424
-     * @return bool
425
-     */
426
-    public function hasSerialNumber(): bool
427
-    {
428
-        return isset($this->_serialNumber);
429
-    }
430
-
431
-    /**
432
-     * Get serial number.
433
-     *
434
-     * @throws \LogicException If not set
435
-     *
436
-     * @return string Base 10 integer
437
-     */
438
-    public function serialNumber(): string
439
-    {
440
-        if (!$this->hasSerialNumber()) {
441
-            throw new \LogicException('serialNumber not set.');
442
-        }
443
-        return $this->_serialNumber;
444
-    }
445
-
446
-    /**
447
-     * Check whether signature algorithm is set.
448
-     *
449
-     * @return bool
450
-     */
451
-    public function hasSignature(): bool
452
-    {
453
-        return isset($this->_signature);
454
-    }
455
-
456
-    /**
457
-     * Get signature algorithm.
458
-     *
459
-     * @throws \LogicException If not set
460
-     *
461
-     * @return SignatureAlgorithmIdentifier
462
-     */
463
-    public function signature(): SignatureAlgorithmIdentifier
464
-    {
465
-        if (!$this->hasSignature()) {
466
-            throw new \LogicException('signature not set.');
467
-        }
468
-        return $this->_signature;
469
-    }
470
-
471
-    /**
472
-     * Get issuer.
473
-     *
474
-     * @return Name
475
-     */
476
-    public function issuer(): Name
477
-    {
478
-        return $this->_issuer;
479
-    }
480
-
481
-    /**
482
-     * Get validity period.
483
-     *
484
-     * @return Validity
485
-     */
486
-    public function validity(): Validity
487
-    {
488
-        return $this->_validity;
489
-    }
490
-
491
-    /**
492
-     * Get subject.
493
-     *
494
-     * @return Name
495
-     */
496
-    public function subject(): Name
497
-    {
498
-        return $this->_subject;
499
-    }
500
-
501
-    /**
502
-     * Get subject public key.
503
-     *
504
-     * @return PublicKeyInfo
505
-     */
506
-    public function subjectPublicKeyInfo(): PublicKeyInfo
507
-    {
508
-        return $this->_subjectPublicKeyInfo;
509
-    }
510
-
511
-    /**
512
-     * Whether issuer unique identifier is present.
513
-     *
514
-     * @return bool
515
-     */
516
-    public function hasIssuerUniqueID(): bool
517
-    {
518
-        return isset($this->_issuerUniqueID);
519
-    }
520
-
521
-    /**
522
-     * Get issuerUniqueID.
523
-     *
524
-     * @throws \LogicException If not set
525
-     *
526
-     * @return UniqueIdentifier
527
-     */
528
-    public function issuerUniqueID(): UniqueIdentifier
529
-    {
530
-        if (!$this->hasIssuerUniqueID()) {
531
-            throw new \LogicException('issuerUniqueID not set.');
532
-        }
533
-        return $this->_issuerUniqueID;
534
-    }
535
-
536
-    /**
537
-     * Whether subject unique identifier is present.
538
-     *
539
-     * @return bool
540
-     */
541
-    public function hasSubjectUniqueID(): bool
542
-    {
543
-        return isset($this->_subjectUniqueID);
544
-    }
545
-
546
-    /**
547
-     * Get subjectUniqueID.
548
-     *
549
-     * @throws \LogicException If not set
550
-     *
551
-     * @return UniqueIdentifier
552
-     */
553
-    public function subjectUniqueID(): UniqueIdentifier
554
-    {
555
-        if (!$this->hasSubjectUniqueID()) {
556
-            throw new \LogicException('subjectUniqueID not set.');
557
-        }
558
-        return $this->_subjectUniqueID;
559
-    }
560
-
561
-    /**
562
-     * Get extensions.
563
-     *
564
-     * @return Extensions
565
-     */
566
-    public function extensions(): Extensions
567
-    {
568
-        return $this->_extensions;
569
-    }
570
-
571
-    /**
572
-     * Generate ASN.1 structure.
573
-     *
574
-     * @return Sequence
575
-     */
576
-    public function toASN1(): Sequence
577
-    {
578
-        $elements = [];
579
-        $version = $this->version();
580
-        // if version is not default
581
-        if (self::VERSION_1 !== $version) {
582
-            $elements[] = new ExplicitlyTaggedType(0, new Integer($version));
583
-        }
584
-        $serial = $this->serialNumber();
585
-        $signature = $this->signature();
586
-        // add required elements
587
-        array_push($elements, new Integer($serial), $signature->toASN1(),
588
-            $this->_issuer->toASN1(), $this->_validity->toASN1(),
589
-            $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1());
590
-        if (isset($this->_issuerUniqueID)) {
591
-            $elements[] = new ImplicitlyTaggedType(1,
592
-                $this->_issuerUniqueID->toASN1());
593
-        }
594
-        if (isset($this->_subjectUniqueID)) {
595
-            $elements[] = new ImplicitlyTaggedType(2,
596
-                $this->_subjectUniqueID->toASN1());
597
-        }
598
-        if (count($this->_extensions)) {
599
-            $elements[] = new ExplicitlyTaggedType(3,
600
-                $this->_extensions->toASN1());
601
-        }
602
-        return new Sequence(...$elements);
603
-    }
604
-
605
-    /**
606
-     * Create signed certificate.
607
-     *
608
-     * @param SignatureAlgorithmIdentifier $algo         Algorithm used for signing
609
-     * @param PrivateKeyInfo               $privkey_info Private key used for signing
610
-     * @param null|Crypto                  $crypto       Crypto engine, use default if not set
611
-     *
612
-     * @return Certificate
613
-     */
614
-    public function sign(SignatureAlgorithmIdentifier $algo,
615
-        PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate
616
-    {
617
-        $crypto = $crypto ?? Crypto::getDefault();
618
-        $tbs_cert = clone $this;
619
-        if (!isset($tbs_cert->_version)) {
620
-            $tbs_cert->_version = $tbs_cert->_determineVersion();
621
-        }
622
-        if (!isset($tbs_cert->_serialNumber)) {
623
-            $tbs_cert->_serialNumber = strval(0);
624
-        }
625
-        $tbs_cert->_signature = $algo;
626
-        $data = $tbs_cert->toASN1()->toDER();
627
-        $signature = $crypto->sign($data, $privkey_info, $algo);
628
-        return new Certificate($tbs_cert, $algo, $signature);
629
-    }
630
-
631
-    /**
632
-     * Determine minimum version for the certificate.
633
-     *
634
-     * @return int
635
-     */
636
-    protected function _determineVersion(): int
637
-    {
638
-        // if extensions are present
639
-        if (count($this->_extensions)) {
640
-            return self::VERSION_3;
641
-        }
642
-        // if UniqueIdentifier is present
643
-        if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) {
644
-            return self::VERSION_2;
645
-        }
646
-        return self::VERSION_1;
647
-    }
30
+	// Certificate version enumerations
31
+	const VERSION_1 = 0;
32
+	const VERSION_2 = 1;
33
+	const VERSION_3 = 2;
34
+
35
+	/**
36
+	 * Certificate version.
37
+	 *
38
+	 * @var null|int
39
+	 */
40
+	protected $_version;
41
+
42
+	/**
43
+	 * Serial number.
44
+	 *
45
+	 * @var null|string
46
+	 */
47
+	protected $_serialNumber;
48
+
49
+	/**
50
+	 * Signature algorithm.
51
+	 *
52
+	 * @var null|SignatureAlgorithmIdentifier
53
+	 */
54
+	protected $_signature;
55
+
56
+	/**
57
+	 * Certificate issuer.
58
+	 *
59
+	 * @var Name
60
+	 */
61
+	protected $_issuer;
62
+
63
+	/**
64
+	 * Certificate validity period.
65
+	 *
66
+	 * @var Validity
67
+	 */
68
+	protected $_validity;
69
+
70
+	/**
71
+	 * Certificate subject.
72
+	 *
73
+	 * @var Name
74
+	 */
75
+	protected $_subject;
76
+
77
+	/**
78
+	 * Subject public key.
79
+	 *
80
+	 * @var PublicKeyInfo
81
+	 */
82
+	protected $_subjectPublicKeyInfo;
83
+
84
+	/**
85
+	 * Issuer unique identifier.
86
+	 *
87
+	 * @var null|UniqueIdentifier
88
+	 */
89
+	protected $_issuerUniqueID;
90
+
91
+	/**
92
+	 * Subject unique identifier.
93
+	 *
94
+	 * @var null|UniqueIdentifier
95
+	 */
96
+	protected $_subjectUniqueID;
97
+
98
+	/**
99
+	 * Extensions.
100
+	 *
101
+	 * @var Extensions
102
+	 */
103
+	protected $_extensions;
104
+
105
+	/**
106
+	 * Constructor.
107
+	 *
108
+	 * @param Name          $subject  Certificate subject
109
+	 * @param PublicKeyInfo $pki      Subject public key
110
+	 * @param Name          $issuer   Certificate issuer
111
+	 * @param Validity      $validity Validity period
112
+	 */
113
+	public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer,
114
+		Validity $validity)
115
+	{
116
+		$this->_subject = $subject;
117
+		$this->_subjectPublicKeyInfo = $pki;
118
+		$this->_issuer = $issuer;
119
+		$this->_validity = $validity;
120
+		$this->_extensions = new Extensions();
121
+	}
122
+
123
+	/**
124
+	 * Initialize from ASN.1.
125
+	 *
126
+	 * @param Sequence $seq
127
+	 *
128
+	 * @return self
129
+	 */
130
+	public static function fromASN1(Sequence $seq): self
131
+	{
132
+		$idx = 0;
133
+		if ($seq->hasTagged(0)) {
134
+			++$idx;
135
+			$version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber();
136
+		} else {
137
+			$version = self::VERSION_1;
138
+		}
139
+		$serial = $seq->at($idx++)->asInteger()->number();
140
+		$algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence());
141
+		if (!$algo instanceof SignatureAlgorithmIdentifier) {
142
+			throw new \UnexpectedValueException(
143
+				'Unsupported signature algorithm ' . $algo->name() . '.');
144
+		}
145
+		$issuer = Name::fromASN1($seq->at($idx++)->asSequence());
146
+		$validity = Validity::fromASN1($seq->at($idx++)->asSequence());
147
+		$subject = Name::fromASN1($seq->at($idx++)->asSequence());
148
+		$pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence());
149
+		$tbs_cert = new self($subject, $pki, $issuer, $validity);
150
+		$tbs_cert->_version = $version;
151
+		$tbs_cert->_serialNumber = $serial;
152
+		$tbs_cert->_signature = $algo;
153
+		if ($seq->hasTagged(1)) {
154
+			$tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1(
155
+				$seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING)
156
+					->asBitString());
157
+		}
158
+		if ($seq->hasTagged(2)) {
159
+			$tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1(
160
+				$seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING)
161
+					->asBitString());
162
+		}
163
+		if ($seq->hasTagged(3)) {
164
+			$tbs_cert->_extensions = Extensions::fromASN1(
165
+				$seq->getTagged(3)->asExplicit()->asSequence());
166
+		}
167
+		return $tbs_cert;
168
+	}
169
+
170
+	/**
171
+	 * Initialize from certification request.
172
+	 *
173
+	 * Note that signature is not verified and must be done by the caller.
174
+	 *
175
+	 * @param CertificationRequest $cr
176
+	 *
177
+	 * @return self
178
+	 */
179
+	public static function fromCSR(CertificationRequest $cr): self
180
+	{
181
+		$cri = $cr->certificationRequestInfo();
182
+		$tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(),
183
+			Validity::fromStrings(null, null));
184
+		// if CSR has Extension Request attribute
185
+		if ($cri->hasAttributes()) {
186
+			$attribs = $cri->attributes();
187
+			if ($attribs->hasExtensionRequest()) {
188
+				$tbs_cert = $tbs_cert->withExtensions(
189
+					$attribs->extensionRequest()->extensions());
190
+			}
191
+		}
192
+		// add Subject Key Identifier extension
193
+		return $tbs_cert->withAdditionalExtensions(
194
+			new SubjectKeyIdentifierExtension(false,
195
+				$cri->subjectPKInfo()->keyIdentifier()));
196
+	}
197
+
198
+	/**
199
+	 * Get self with fields set from the issuer's certificate.
200
+	 *
201
+	 * Issuer shall be set to issuing certificate's subject.
202
+	 * Authority key identifier extensions shall be added with a key identifier
203
+	 * set to issuing certificate's public key identifier.
204
+	 *
205
+	 * @param Certificate $cert Issuing party's certificate
206
+	 *
207
+	 * @return self
208
+	 */
209
+	public function withIssuerCertificate(Certificate $cert): self
210
+	{
211
+		$obj = clone $this;
212
+		// set issuer DN from cert's subject
213
+		$obj->_issuer = $cert->tbsCertificate()->subject();
214
+		// add authority key identifier extension
215
+		$key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier();
216
+		$obj->_extensions = $obj->_extensions->withExtensions(
217
+			new AuthorityKeyIdentifierExtension(false, $key_id));
218
+		return $obj;
219
+	}
220
+
221
+	/**
222
+	 * Get self with given version.
223
+	 *
224
+	 * If version is not set, appropriate version is automatically
225
+	 * determined during signing.
226
+	 *
227
+	 * @param int $version
228
+	 *
229
+	 * @return self
230
+	 */
231
+	public function withVersion(int $version): self
232
+	{
233
+		$obj = clone $this;
234
+		$obj->_version = $version;
235
+		return $obj;
236
+	}
237
+
238
+	/**
239
+	 * Get self with given serial number.
240
+	 *
241
+	 * @param int|string $serial Base 10 number
242
+	 *
243
+	 * @return self
244
+	 */
245
+	public function withSerialNumber($serial): self
246
+	{
247
+		$obj = clone $this;
248
+		$obj->_serialNumber = strval($serial);
249
+		return $obj;
250
+	}
251
+
252
+	/**
253
+	 * Get self with random positive serial number.
254
+	 *
255
+	 * @param int $size Number of random bytes
256
+	 *
257
+	 * @return self
258
+	 */
259
+	public function withRandomSerialNumber(int $size = 16): self
260
+	{
261
+		// ensure that first byte is always non-zero and having first bit unset
262
+		$num = gmp_init(mt_rand(1, 0x7f), 10);
263
+		for ($i = 1; $i < $size; ++$i) {
264
+			$num <<= 8;
265
+			$num += mt_rand(0, 0xff);
266
+		}
267
+		return $this->withSerialNumber(gmp_strval($num, 10));
268
+	}
269
+
270
+	/**
271
+	 * Get self with given signature algorithm.
272
+	 *
273
+	 * @param SignatureAlgorithmIdentifier $algo
274
+	 *
275
+	 * @return self
276
+	 */
277
+	public function withSignature(SignatureAlgorithmIdentifier $algo): self
278
+	{
279
+		$obj = clone $this;
280
+		$obj->_signature = $algo;
281
+		return $obj;
282
+	}
283
+
284
+	/**
285
+	 * Get self with given issuer.
286
+	 *
287
+	 * @param Name $issuer
288
+	 *
289
+	 * @return self
290
+	 */
291
+	public function withIssuer(Name $issuer): self
292
+	{
293
+		$obj = clone $this;
294
+		$obj->_issuer = $issuer;
295
+		return $obj;
296
+	}
297
+
298
+	/**
299
+	 * Get self with given validity.
300
+	 *
301
+	 * @param Validity $validity
302
+	 *
303
+	 * @return self
304
+	 */
305
+	public function withValidity(Validity $validity): self
306
+	{
307
+		$obj = clone $this;
308
+		$obj->_validity = $validity;
309
+		return $obj;
310
+	}
311
+
312
+	/**
313
+	 * Get self with given subject.
314
+	 *
315
+	 * @param Name $subject
316
+	 *
317
+	 * @return self
318
+	 */
319
+	public function withSubject(Name $subject): self
320
+	{
321
+		$obj = clone $this;
322
+		$obj->_subject = $subject;
323
+		return $obj;
324
+	}
325
+
326
+	/**
327
+	 * Get self with given subject public key info.
328
+	 *
329
+	 * @param PublicKeyInfo $pub_key_info
330
+	 *
331
+	 * @return self
332
+	 */
333
+	public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self
334
+	{
335
+		$obj = clone $this;
336
+		$obj->_subjectPublicKeyInfo = $pub_key_info;
337
+		return $obj;
338
+	}
339
+
340
+	/**
341
+	 * Get self with issuer unique ID.
342
+	 *
343
+	 * @param UniqueIdentifier $id
344
+	 *
345
+	 * @return self
346
+	 */
347
+	public function withIssuerUniqueID(UniqueIdentifier $id): self
348
+	{
349
+		$obj = clone $this;
350
+		$obj->_issuerUniqueID = $id;
351
+		return $obj;
352
+	}
353
+
354
+	/**
355
+	 * Get self with subject unique ID.
356
+	 *
357
+	 * @param UniqueIdentifier $id
358
+	 *
359
+	 * @return self
360
+	 */
361
+	public function withSubjectUniqueID(UniqueIdentifier $id): self
362
+	{
363
+		$obj = clone $this;
364
+		$obj->_subjectUniqueID = $id;
365
+		return $obj;
366
+	}
367
+
368
+	/**
369
+	 * Get self with given extensions.
370
+	 *
371
+	 * @param Extensions $extensions
372
+	 *
373
+	 * @return self
374
+	 */
375
+	public function withExtensions(Extensions $extensions): self
376
+	{
377
+		$obj = clone $this;
378
+		$obj->_extensions = $extensions;
379
+		return $obj;
380
+	}
381
+
382
+	/**
383
+	 * Get self with extensions added.
384
+	 *
385
+	 * @param Extension ...$exts One or more Extension objects
386
+	 *
387
+	 * @return self
388
+	 */
389
+	public function withAdditionalExtensions(Extension ...$exts): self
390
+	{
391
+		$obj = clone $this;
392
+		$obj->_extensions = $obj->_extensions->withExtensions(...$exts);
393
+		return $obj;
394
+	}
395
+
396
+	/**
397
+	 * Check whether version is set.
398
+	 *
399
+	 * @return bool
400
+	 */
401
+	public function hasVersion(): bool
402
+	{
403
+		return isset($this->_version);
404
+	}
405
+
406
+	/**
407
+	 * Get certificate version.
408
+	 *
409
+	 * @throws \LogicException If not set
410
+	 *
411
+	 * @return int
412
+	 */
413
+	public function version(): int
414
+	{
415
+		if (!$this->hasVersion()) {
416
+			throw new \LogicException('version not set.');
417
+		}
418
+		return $this->_version;
419
+	}
420
+
421
+	/**
422
+	 * Check whether serial number is set.
423
+	 *
424
+	 * @return bool
425
+	 */
426
+	public function hasSerialNumber(): bool
427
+	{
428
+		return isset($this->_serialNumber);
429
+	}
430
+
431
+	/**
432
+	 * Get serial number.
433
+	 *
434
+	 * @throws \LogicException If not set
435
+	 *
436
+	 * @return string Base 10 integer
437
+	 */
438
+	public function serialNumber(): string
439
+	{
440
+		if (!$this->hasSerialNumber()) {
441
+			throw new \LogicException('serialNumber not set.');
442
+		}
443
+		return $this->_serialNumber;
444
+	}
445
+
446
+	/**
447
+	 * Check whether signature algorithm is set.
448
+	 *
449
+	 * @return bool
450
+	 */
451
+	public function hasSignature(): bool
452
+	{
453
+		return isset($this->_signature);
454
+	}
455
+
456
+	/**
457
+	 * Get signature algorithm.
458
+	 *
459
+	 * @throws \LogicException If not set
460
+	 *
461
+	 * @return SignatureAlgorithmIdentifier
462
+	 */
463
+	public function signature(): SignatureAlgorithmIdentifier
464
+	{
465
+		if (!$this->hasSignature()) {
466
+			throw new \LogicException('signature not set.');
467
+		}
468
+		return $this->_signature;
469
+	}
470
+
471
+	/**
472
+	 * Get issuer.
473
+	 *
474
+	 * @return Name
475
+	 */
476
+	public function issuer(): Name
477
+	{
478
+		return $this->_issuer;
479
+	}
480
+
481
+	/**
482
+	 * Get validity period.
483
+	 *
484
+	 * @return Validity
485
+	 */
486
+	public function validity(): Validity
487
+	{
488
+		return $this->_validity;
489
+	}
490
+
491
+	/**
492
+	 * Get subject.
493
+	 *
494
+	 * @return Name
495
+	 */
496
+	public function subject(): Name
497
+	{
498
+		return $this->_subject;
499
+	}
500
+
501
+	/**
502
+	 * Get subject public key.
503
+	 *
504
+	 * @return PublicKeyInfo
505
+	 */
506
+	public function subjectPublicKeyInfo(): PublicKeyInfo
507
+	{
508
+		return $this->_subjectPublicKeyInfo;
509
+	}
510
+
511
+	/**
512
+	 * Whether issuer unique identifier is present.
513
+	 *
514
+	 * @return bool
515
+	 */
516
+	public function hasIssuerUniqueID(): bool
517
+	{
518
+		return isset($this->_issuerUniqueID);
519
+	}
520
+
521
+	/**
522
+	 * Get issuerUniqueID.
523
+	 *
524
+	 * @throws \LogicException If not set
525
+	 *
526
+	 * @return UniqueIdentifier
527
+	 */
528
+	public function issuerUniqueID(): UniqueIdentifier
529
+	{
530
+		if (!$this->hasIssuerUniqueID()) {
531
+			throw new \LogicException('issuerUniqueID not set.');
532
+		}
533
+		return $this->_issuerUniqueID;
534
+	}
535
+
536
+	/**
537
+	 * Whether subject unique identifier is present.
538
+	 *
539
+	 * @return bool
540
+	 */
541
+	public function hasSubjectUniqueID(): bool
542
+	{
543
+		return isset($this->_subjectUniqueID);
544
+	}
545
+
546
+	/**
547
+	 * Get subjectUniqueID.
548
+	 *
549
+	 * @throws \LogicException If not set
550
+	 *
551
+	 * @return UniqueIdentifier
552
+	 */
553
+	public function subjectUniqueID(): UniqueIdentifier
554
+	{
555
+		if (!$this->hasSubjectUniqueID()) {
556
+			throw new \LogicException('subjectUniqueID not set.');
557
+		}
558
+		return $this->_subjectUniqueID;
559
+	}
560
+
561
+	/**
562
+	 * Get extensions.
563
+	 *
564
+	 * @return Extensions
565
+	 */
566
+	public function extensions(): Extensions
567
+	{
568
+		return $this->_extensions;
569
+	}
570
+
571
+	/**
572
+	 * Generate ASN.1 structure.
573
+	 *
574
+	 * @return Sequence
575
+	 */
576
+	public function toASN1(): Sequence
577
+	{
578
+		$elements = [];
579
+		$version = $this->version();
580
+		// if version is not default
581
+		if (self::VERSION_1 !== $version) {
582
+			$elements[] = new ExplicitlyTaggedType(0, new Integer($version));
583
+		}
584
+		$serial = $this->serialNumber();
585
+		$signature = $this->signature();
586
+		// add required elements
587
+		array_push($elements, new Integer($serial), $signature->toASN1(),
588
+			$this->_issuer->toASN1(), $this->_validity->toASN1(),
589
+			$this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1());
590
+		if (isset($this->_issuerUniqueID)) {
591
+			$elements[] = new ImplicitlyTaggedType(1,
592
+				$this->_issuerUniqueID->toASN1());
593
+		}
594
+		if (isset($this->_subjectUniqueID)) {
595
+			$elements[] = new ImplicitlyTaggedType(2,
596
+				$this->_subjectUniqueID->toASN1());
597
+		}
598
+		if (count($this->_extensions)) {
599
+			$elements[] = new ExplicitlyTaggedType(3,
600
+				$this->_extensions->toASN1());
601
+		}
602
+		return new Sequence(...$elements);
603
+	}
604
+
605
+	/**
606
+	 * Create signed certificate.
607
+	 *
608
+	 * @param SignatureAlgorithmIdentifier $algo         Algorithm used for signing
609
+	 * @param PrivateKeyInfo               $privkey_info Private key used for signing
610
+	 * @param null|Crypto                  $crypto       Crypto engine, use default if not set
611
+	 *
612
+	 * @return Certificate
613
+	 */
614
+	public function sign(SignatureAlgorithmIdentifier $algo,
615
+		PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate
616
+	{
617
+		$crypto = $crypto ?? Crypto::getDefault();
618
+		$tbs_cert = clone $this;
619
+		if (!isset($tbs_cert->_version)) {
620
+			$tbs_cert->_version = $tbs_cert->_determineVersion();
621
+		}
622
+		if (!isset($tbs_cert->_serialNumber)) {
623
+			$tbs_cert->_serialNumber = strval(0);
624
+		}
625
+		$tbs_cert->_signature = $algo;
626
+		$data = $tbs_cert->toASN1()->toDER();
627
+		$signature = $crypto->sign($data, $privkey_info, $algo);
628
+		return new Certificate($tbs_cert, $algo, $signature);
629
+	}
630
+
631
+	/**
632
+	 * Determine minimum version for the certificate.
633
+	 *
634
+	 * @return int
635
+	 */
636
+	protected function _determineVersion(): int
637
+	{
638
+		// if extensions are present
639
+		if (count($this->_extensions)) {
640
+			return self::VERSION_3;
641
+		}
642
+		// if UniqueIdentifier is present
643
+		if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) {
644
+			return self::VERSION_2;
645
+		}
646
+		return self::VERSION_1;
647
+	}
648 648
 }
Please login to merge, or discard this patch.
lib/X509/Certificate/Certificate.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -20,242 +20,242 @@
 block discarded – undo
20 20
  */
21 21
 class Certificate
22 22
 {
23
-    /**
24
-     * "To be signed" certificate information.
25
-     *
26
-     * @var TBSCertificate
27
-     */
28
-    protected $_tbsCertificate;
23
+	/**
24
+	 * "To be signed" certificate information.
25
+	 *
26
+	 * @var TBSCertificate
27
+	 */
28
+	protected $_tbsCertificate;
29 29
 
30
-    /**
31
-     * Signature algorithm.
32
-     *
33
-     * @var SignatureAlgorithmIdentifier
34
-     */
35
-    protected $_signatureAlgorithm;
30
+	/**
31
+	 * Signature algorithm.
32
+	 *
33
+	 * @var SignatureAlgorithmIdentifier
34
+	 */
35
+	protected $_signatureAlgorithm;
36 36
 
37
-    /**
38
-     * Signature value.
39
-     *
40
-     * @var Signature
41
-     */
42
-    protected $_signatureValue;
37
+	/**
38
+	 * Signature value.
39
+	 *
40
+	 * @var Signature
41
+	 */
42
+	protected $_signatureValue;
43 43
 
44
-    /**
45
-     * Constructor.
46
-     *
47
-     * @param TBSCertificate               $tbsCert
48
-     * @param SignatureAlgorithmIdentifier $algo
49
-     * @param Signature                    $signature
50
-     */
51
-    public function __construct(TBSCertificate $tbsCert,
52
-        SignatureAlgorithmIdentifier $algo, Signature $signature)
53
-    {
54
-        $this->_tbsCertificate = $tbsCert;
55
-        $this->_signatureAlgorithm = $algo;
56
-        $this->_signatureValue = $signature;
57
-    }
44
+	/**
45
+	 * Constructor.
46
+	 *
47
+	 * @param TBSCertificate               $tbsCert
48
+	 * @param SignatureAlgorithmIdentifier $algo
49
+	 * @param Signature                    $signature
50
+	 */
51
+	public function __construct(TBSCertificate $tbsCert,
52
+		SignatureAlgorithmIdentifier $algo, Signature $signature)
53
+	{
54
+		$this->_tbsCertificate = $tbsCert;
55
+		$this->_signatureAlgorithm = $algo;
56
+		$this->_signatureValue = $signature;
57
+	}
58 58
 
59
-    /**
60
-     * Get certificate as a PEM formatted string.
61
-     *
62
-     * @return string
63
-     */
64
-    public function __toString(): string
65
-    {
66
-        return $this->toPEM()->string();
67
-    }
59
+	/**
60
+	 * Get certificate as a PEM formatted string.
61
+	 *
62
+	 * @return string
63
+	 */
64
+	public function __toString(): string
65
+	{
66
+		return $this->toPEM()->string();
67
+	}
68 68
 
69
-    /**
70
-     * Initialize from ASN.1.
71
-     *
72
-     * @param Sequence $seq
73
-     *
74
-     * @return self
75
-     */
76
-    public static function fromASN1(Sequence $seq): self
77
-    {
78
-        $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence());
79
-        $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
80
-        if (!$algo instanceof SignatureAlgorithmIdentifier) {
81
-            throw new \UnexpectedValueException(
82
-                'Unsupported signature algorithm ' . $algo->oid() . '.');
83
-        }
84
-        $signature = Signature::fromSignatureData(
85
-            $seq->at(2)->asBitString()->string(), $algo);
86
-        return new self($tbsCert, $algo, $signature);
87
-    }
69
+	/**
70
+	 * Initialize from ASN.1.
71
+	 *
72
+	 * @param Sequence $seq
73
+	 *
74
+	 * @return self
75
+	 */
76
+	public static function fromASN1(Sequence $seq): self
77
+	{
78
+		$tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence());
79
+		$algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
80
+		if (!$algo instanceof SignatureAlgorithmIdentifier) {
81
+			throw new \UnexpectedValueException(
82
+				'Unsupported signature algorithm ' . $algo->oid() . '.');
83
+		}
84
+		$signature = Signature::fromSignatureData(
85
+			$seq->at(2)->asBitString()->string(), $algo);
86
+		return new self($tbsCert, $algo, $signature);
87
+	}
88 88
 
89
-    /**
90
-     * Initialize from DER.
91
-     *
92
-     * @param string $data
93
-     *
94
-     * @return self
95
-     */
96
-    public static function fromDER(string $data): self
97
-    {
98
-        return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
99
-    }
89
+	/**
90
+	 * Initialize from DER.
91
+	 *
92
+	 * @param string $data
93
+	 *
94
+	 * @return self
95
+	 */
96
+	public static function fromDER(string $data): self
97
+	{
98
+		return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
99
+	}
100 100
 
101
-    /**
102
-     * Initialize from PEM.
103
-     *
104
-     * @param PEM $pem
105
-     *
106
-     * @throws \UnexpectedValueException
107
-     *
108
-     * @return self
109
-     */
110
-    public static function fromPEM(PEM $pem): self
111
-    {
112
-        if (PEM::TYPE_CERTIFICATE !== $pem->type()) {
113
-            throw new \UnexpectedValueException('Invalid PEM type.');
114
-        }
115
-        return self::fromDER($pem->data());
116
-    }
101
+	/**
102
+	 * Initialize from PEM.
103
+	 *
104
+	 * @param PEM $pem
105
+	 *
106
+	 * @throws \UnexpectedValueException
107
+	 *
108
+	 * @return self
109
+	 */
110
+	public static function fromPEM(PEM $pem): self
111
+	{
112
+		if (PEM::TYPE_CERTIFICATE !== $pem->type()) {
113
+			throw new \UnexpectedValueException('Invalid PEM type.');
114
+		}
115
+		return self::fromDER($pem->data());
116
+	}
117 117
 
118
-    /**
119
-     * Get certificate information.
120
-     *
121
-     * @return TBSCertificate
122
-     */
123
-    public function tbsCertificate(): TBSCertificate
124
-    {
125
-        return $this->_tbsCertificate;
126
-    }
118
+	/**
119
+	 * Get certificate information.
120
+	 *
121
+	 * @return TBSCertificate
122
+	 */
123
+	public function tbsCertificate(): TBSCertificate
124
+	{
125
+		return $this->_tbsCertificate;
126
+	}
127 127
 
128
-    /**
129
-     * Get signature algorithm.
130
-     *
131
-     * @return SignatureAlgorithmIdentifier
132
-     */
133
-    public function signatureAlgorithm(): SignatureAlgorithmIdentifier
134
-    {
135
-        return $this->_signatureAlgorithm;
136
-    }
128
+	/**
129
+	 * Get signature algorithm.
130
+	 *
131
+	 * @return SignatureAlgorithmIdentifier
132
+	 */
133
+	public function signatureAlgorithm(): SignatureAlgorithmIdentifier
134
+	{
135
+		return $this->_signatureAlgorithm;
136
+	}
137 137
 
138
-    /**
139
-     * Get signature value.
140
-     *
141
-     * @return Signature
142
-     */
143
-    public function signatureValue(): Signature
144
-    {
145
-        return $this->_signatureValue;
146
-    }
138
+	/**
139
+	 * Get signature value.
140
+	 *
141
+	 * @return Signature
142
+	 */
143
+	public function signatureValue(): Signature
144
+	{
145
+		return $this->_signatureValue;
146
+	}
147 147
 
148
-    /**
149
-     * Check whether certificate is self-issued.
150
-     *
151
-     * @return bool
152
-     */
153
-    public function isSelfIssued(): bool
154
-    {
155
-        return $this->_tbsCertificate->subject()->equals(
156
-            $this->_tbsCertificate->issuer());
157
-    }
148
+	/**
149
+	 * Check whether certificate is self-issued.
150
+	 *
151
+	 * @return bool
152
+	 */
153
+	public function isSelfIssued(): bool
154
+	{
155
+		return $this->_tbsCertificate->subject()->equals(
156
+			$this->_tbsCertificate->issuer());
157
+	}
158 158
 
159
-    /**
160
-     * Check whether certificate is semantically equal to another.
161
-     *
162
-     * @param Certificate $cert Certificate to compare to
163
-     *
164
-     * @return bool
165
-     */
166
-    public function equals(Certificate $cert): bool
167
-    {
168
-        return $this->_hasEqualSerialNumber($cert) &&
169
-             $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert);
170
-    }
159
+	/**
160
+	 * Check whether certificate is semantically equal to another.
161
+	 *
162
+	 * @param Certificate $cert Certificate to compare to
163
+	 *
164
+	 * @return bool
165
+	 */
166
+	public function equals(Certificate $cert): bool
167
+	{
168
+		return $this->_hasEqualSerialNumber($cert) &&
169
+			 $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert);
170
+	}
171 171
 
172
-    /**
173
-     * Generate ASN.1 structure.
174
-     *
175
-     * @return Sequence
176
-     */
177
-    public function toASN1(): Sequence
178
-    {
179
-        return new Sequence($this->_tbsCertificate->toASN1(),
180
-            $this->_signatureAlgorithm->toASN1(),
181
-            $this->_signatureValue->bitString());
182
-    }
172
+	/**
173
+	 * Generate ASN.1 structure.
174
+	 *
175
+	 * @return Sequence
176
+	 */
177
+	public function toASN1(): Sequence
178
+	{
179
+		return new Sequence($this->_tbsCertificate->toASN1(),
180
+			$this->_signatureAlgorithm->toASN1(),
181
+			$this->_signatureValue->bitString());
182
+	}
183 183
 
184
-    /**
185
-     * Get certificate as a DER.
186
-     *
187
-     * @return string
188
-     */
189
-    public function toDER(): string
190
-    {
191
-        return $this->toASN1()->toDER();
192
-    }
184
+	/**
185
+	 * Get certificate as a DER.
186
+	 *
187
+	 * @return string
188
+	 */
189
+	public function toDER(): string
190
+	{
191
+		return $this->toASN1()->toDER();
192
+	}
193 193
 
194
-    /**
195
-     * Get certificate as a PEM.
196
-     *
197
-     * @return PEM
198
-     */
199
-    public function toPEM(): PEM
200
-    {
201
-        return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER());
202
-    }
194
+	/**
195
+	 * Get certificate as a PEM.
196
+	 *
197
+	 * @return PEM
198
+	 */
199
+	public function toPEM(): PEM
200
+	{
201
+		return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER());
202
+	}
203 203
 
204
-    /**
205
-     * Verify certificate signature.
206
-     *
207
-     * @param PublicKeyInfo $pubkey_info Issuer's public key
208
-     * @param null|Crypto   $crypto      Crypto engine, use default if not set
209
-     *
210
-     * @return bool True if certificate signature is valid
211
-     */
212
-    public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool
213
-    {
214
-        $crypto = $crypto ?? Crypto::getDefault();
215
-        $data = $this->_tbsCertificate->toASN1()->toDER();
216
-        return $crypto->verify($data, $this->_signatureValue, $pubkey_info,
217
-            $this->_signatureAlgorithm);
218
-    }
204
+	/**
205
+	 * Verify certificate signature.
206
+	 *
207
+	 * @param PublicKeyInfo $pubkey_info Issuer's public key
208
+	 * @param null|Crypto   $crypto      Crypto engine, use default if not set
209
+	 *
210
+	 * @return bool True if certificate signature is valid
211
+	 */
212
+	public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool
213
+	{
214
+		$crypto = $crypto ?? Crypto::getDefault();
215
+		$data = $this->_tbsCertificate->toASN1()->toDER();
216
+		return $crypto->verify($data, $this->_signatureValue, $pubkey_info,
217
+			$this->_signatureAlgorithm);
218
+	}
219 219
 
220
-    /**
221
-     * Check whether certificate has serial number equal to another.
222
-     *
223
-     * @param Certificate $cert
224
-     *
225
-     * @return bool
226
-     */
227
-    private function _hasEqualSerialNumber(Certificate $cert): bool
228
-    {
229
-        $sn1 = $this->_tbsCertificate->serialNumber();
230
-        $sn2 = $cert->_tbsCertificate->serialNumber();
231
-        return $sn1 === $sn2;
232
-    }
220
+	/**
221
+	 * Check whether certificate has serial number equal to another.
222
+	 *
223
+	 * @param Certificate $cert
224
+	 *
225
+	 * @return bool
226
+	 */
227
+	private function _hasEqualSerialNumber(Certificate $cert): bool
228
+	{
229
+		$sn1 = $this->_tbsCertificate->serialNumber();
230
+		$sn2 = $cert->_tbsCertificate->serialNumber();
231
+		return $sn1 === $sn2;
232
+	}
233 233
 
234
-    /**
235
-     * Check whether certificate has public key equal to another.
236
-     *
237
-     * @param Certificate $cert
238
-     *
239
-     * @return bool
240
-     */
241
-    private function _hasEqualPublicKey(Certificate $cert): bool
242
-    {
243
-        $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
244
-        $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
245
-        return $kid1 === $kid2;
246
-    }
234
+	/**
235
+	 * Check whether certificate has public key equal to another.
236
+	 *
237
+	 * @param Certificate $cert
238
+	 *
239
+	 * @return bool
240
+	 */
241
+	private function _hasEqualPublicKey(Certificate $cert): bool
242
+	{
243
+		$kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
244
+		$kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
245
+		return $kid1 === $kid2;
246
+	}
247 247
 
248
-    /**
249
-     * Check whether certificate has subject equal to another.
250
-     *
251
-     * @param Certificate $cert
252
-     *
253
-     * @return bool
254
-     */
255
-    private function _hasEqualSubject(Certificate $cert): bool
256
-    {
257
-        $dn1 = $this->_tbsCertificate->subject();
258
-        $dn2 = $cert->_tbsCertificate->subject();
259
-        return $dn1->equals($dn2);
260
-    }
248
+	/**
249
+	 * Check whether certificate has subject equal to another.
250
+	 *
251
+	 * @param Certificate $cert
252
+	 *
253
+	 * @return bool
254
+	 */
255
+	private function _hasEqualSubject(Certificate $cert): bool
256
+	{
257
+		$dn1 = $this->_tbsCertificate->subject();
258
+		$dn2 = $cert->_tbsCertificate->subject();
259
+		return $dn1->equals($dn2);
260
+	}
261 261
 }
Please login to merge, or discard this patch.
lib/X509/CertificationPath/Exception/PathValidationException.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace Sop\X509\CertificationPath\Exception;
6 6
 
Please login to merge, or discard this patch.
lib/X509/Certificate/Extension/Target/Targets.php 2 patches
Indentation   +121 added lines, -121 removed lines patch added patch discarded remove patch
@@ -14,135 +14,135 @@
 block discarded – undo
14 14
  */
15 15
 class Targets implements \Countable, \IteratorAggregate
16 16
 {
17
-    /**
18
-     * Target elements.
19
-     *
20
-     * @var Target[]
21
-     */
22
-    protected $_targets;
17
+	/**
18
+	 * Target elements.
19
+	 *
20
+	 * @var Target[]
21
+	 */
22
+	protected $_targets;
23 23
 
24
-    /**
25
-     * Constructor.
26
-     *
27
-     * @param Target ...$targets
28
-     */
29
-    public function __construct(Target ...$targets)
30
-    {
31
-        $this->_targets = $targets;
32
-    }
24
+	/**
25
+	 * Constructor.
26
+	 *
27
+	 * @param Target ...$targets
28
+	 */
29
+	public function __construct(Target ...$targets)
30
+	{
31
+		$this->_targets = $targets;
32
+	}
33 33
 
34
-    /**
35
-     * Initialize from ASN.1.
36
-     *
37
-     * @param Sequence $seq
38
-     *
39
-     * @return self
40
-     */
41
-    public static function fromASN1(Sequence $seq): self
42
-    {
43
-        $targets = array_map(
44
-            function (UnspecifiedType $el) {
45
-                return Target::fromASN1($el->asTagged());
46
-            }, $seq->elements());
47
-        return new self(...$targets);
48
-    }
34
+	/**
35
+	 * Initialize from ASN.1.
36
+	 *
37
+	 * @param Sequence $seq
38
+	 *
39
+	 * @return self
40
+	 */
41
+	public static function fromASN1(Sequence $seq): self
42
+	{
43
+		$targets = array_map(
44
+			function (UnspecifiedType $el) {
45
+				return Target::fromASN1($el->asTagged());
46
+			}, $seq->elements());
47
+		return new self(...$targets);
48
+	}
49 49
 
50
-    /**
51
-     * Get all targets.
52
-     *
53
-     * @return Target[]
54
-     */
55
-    public function all(): array
56
-    {
57
-        return $this->_targets;
58
-    }
50
+	/**
51
+	 * Get all targets.
52
+	 *
53
+	 * @return Target[]
54
+	 */
55
+	public function all(): array
56
+	{
57
+		return $this->_targets;
58
+	}
59 59
 
60
-    /**
61
-     * Get all name targets.
62
-     *
63
-     * @return Target[]
64
-     */
65
-    public function nameTargets(): array
66
-    {
67
-        return $this->_allOfType(Target::TYPE_NAME);
68
-    }
60
+	/**
61
+	 * Get all name targets.
62
+	 *
63
+	 * @return Target[]
64
+	 */
65
+	public function nameTargets(): array
66
+	{
67
+		return $this->_allOfType(Target::TYPE_NAME);
68
+	}
69 69
 
70
-    /**
71
-     * Get all group targets.
72
-     *
73
-     * @return Target[]
74
-     */
75
-    public function groupTargets(): array
76
-    {
77
-        return $this->_allOfType(Target::TYPE_GROUP);
78
-    }
70
+	/**
71
+	 * Get all group targets.
72
+	 *
73
+	 * @return Target[]
74
+	 */
75
+	public function groupTargets(): array
76
+	{
77
+		return $this->_allOfType(Target::TYPE_GROUP);
78
+	}
79 79
 
80
-    /**
81
-     * Check whether given target is present.
82
-     *
83
-     * @param Target $target
84
-     *
85
-     * @return bool
86
-     */
87
-    public function hasTarget(Target $target): bool
88
-    {
89
-        foreach ($this->_allOfType($target->type()) as $t) {
90
-            if ($target->equals($t)) {
91
-                return true;
92
-            }
93
-        }
94
-        return false;
95
-    }
80
+	/**
81
+	 * Check whether given target is present.
82
+	 *
83
+	 * @param Target $target
84
+	 *
85
+	 * @return bool
86
+	 */
87
+	public function hasTarget(Target $target): bool
88
+	{
89
+		foreach ($this->_allOfType($target->type()) as $t) {
90
+			if ($target->equals($t)) {
91
+				return true;
92
+			}
93
+		}
94
+		return false;
95
+	}
96 96
 
97
-    /**
98
-     * Generate ASN.1 structure.
99
-     *
100
-     * @return Sequence
101
-     */
102
-    public function toASN1(): Sequence
103
-    {
104
-        $elements = array_map(
105
-            function (Target $target) {
106
-                return $target->toASN1();
107
-            }, $this->_targets);
108
-        return new Sequence(...$elements);
109
-    }
97
+	/**
98
+	 * Generate ASN.1 structure.
99
+	 *
100
+	 * @return Sequence
101
+	 */
102
+	public function toASN1(): Sequence
103
+	{
104
+		$elements = array_map(
105
+			function (Target $target) {
106
+				return $target->toASN1();
107
+			}, $this->_targets);
108
+		return new Sequence(...$elements);
109
+	}
110 110
 
111
-    /**
112
-     * @see \Countable::count()
113
-     *
114
-     * @return int
115
-     */
116
-    public function count(): int
117
-    {
118
-        return count($this->_targets);
119
-    }
111
+	/**
112
+	 * @see \Countable::count()
113
+	 *
114
+	 * @return int
115
+	 */
116
+	public function count(): int
117
+	{
118
+		return count($this->_targets);
119
+	}
120 120
 
121
-    /**
122
-     * Get iterator for targets.
123
-     *
124
-     * @see \IteratorAggregate::getIterator()
125
-     *
126
-     * @return \ArrayIterator
127
-     */
128
-    public function getIterator(): \ArrayIterator
129
-    {
130
-        return new \ArrayIterator($this->_targets);
131
-    }
121
+	/**
122
+	 * Get iterator for targets.
123
+	 *
124
+	 * @see \IteratorAggregate::getIterator()
125
+	 *
126
+	 * @return \ArrayIterator
127
+	 */
128
+	public function getIterator(): \ArrayIterator
129
+	{
130
+		return new \ArrayIterator($this->_targets);
131
+	}
132 132
 
133
-    /**
134
-     * Get all targets of given type.
135
-     *
136
-     * @param int $type
137
-     *
138
-     * @return Target[]
139
-     */
140
-    protected function _allOfType(int $type): array
141
-    {
142
-        return array_values(
143
-            array_filter($this->_targets,
144
-                function (Target $target) use ($type) {
145
-                    return $target->type() === $type;
146
-                }));
147
-    }
133
+	/**
134
+	 * Get all targets of given type.
135
+	 *
136
+	 * @param int $type
137
+	 *
138
+	 * @return Target[]
139
+	 */
140
+	protected function _allOfType(int $type): array
141
+	{
142
+		return array_values(
143
+			array_filter($this->_targets,
144
+				function (Target $target) use ($type) {
145
+					return $target->type() === $type;
146
+				}));
147
+	}
148 148
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
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
 
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
     public static function fromASN1(Sequence $seq): self
42 42
     {
43 43
         $targets = array_map(
44
-            function (UnspecifiedType $el) {
44
+            function(UnspecifiedType $el) {
45 45
                 return Target::fromASN1($el->asTagged());
46 46
             }, $seq->elements());
47 47
         return new self(...$targets);
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
     public function toASN1(): Sequence
103 103
     {
104 104
         $elements = array_map(
105
-            function (Target $target) {
105
+            function(Target $target) {
106 106
                 return $target->toASN1();
107 107
             }, $this->_targets);
108 108
         return new Sequence(...$elements);
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
     {
142 142
         return array_values(
143 143
             array_filter($this->_targets,
144
-                function (Target $target) use ($type) {
144
+                function(Target $target) use ($type) {
145 145
                     return $target->type() === $type;
146 146
                 }));
147 147
     }
Please login to merge, or discard this patch.