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.
Completed
Push — master ( 96d802...e5ec4d )
by Joni
04:39
created

AttributeCertificateInfo::withValidity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
nc 1
cc 1
eloc 4
nop 1
crap 1
1
<?php
2
3
namespace X509\AttributeCertificate;
4
5
use ASN1\Element;
6
use ASN1\Type\Constructed\Sequence;
7
use ASN1\Type\Primitive\Integer;
8
use CryptoUtil\ASN1\AlgorithmIdentifier;
9
use CryptoUtil\ASN1\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
10
use CryptoUtil\ASN1\PrivateKeyInfo;
11
use CryptoUtil\Crypto\Crypto;
12
use X509\Certificate\Extensions;
13
use X509\Certificate\UniqueIdentifier;
14
15
16
/**
17
 * Implements <i>AttributeCertificateInfo</i> ASN.1 type.
18
 *
19
 * @link https://tools.ietf.org/html/rfc5755#section-4.1
20
 */
21
class AttributeCertificateInfo
22
{
23
	const VERSION_2 = 1;
24
	
25
	/**
26
	 * AC version.
27
	 *
28
	 * @var int $_version
29
	 */
30
	protected $_version;
31
	
32
	/**
33
	 * AC holder.
34
	 *
35
	 * @var Holder $_holder
36
	 */
37
	protected $_holder;
38
	
39
	/**
40
	 * AC issuer.
41
	 *
42
	 * @var AttCertIssuer $_issuer
43
	 */
44
	protected $_issuer;
45
	
46
	/**
47
	 * Signature algorithm identifier.
48
	 *
49
	 * @var SignatureAlgorithmIdentifier $_signature
50
	 */
51
	protected $_signature;
52
	
53
	/**
54
	 * AC serial number.
55
	 *
56
	 * @var int|string $_serialNumber
57
	 */
58
	protected $_serialNumber;
59
	
60
	/**
61
	 * Validity period.
62
	 *
63
	 * @var AttCertValidityPeriod $_attrCertValidityPeriod
64
	 */
65
	protected $_attrCertValidityPeriod;
66
	
67
	/**
68
	 * Attributes.
69
	 *
70
	 * @var Attributes $_attributes
71
	 */
72
	protected $_attributes;
73
	
74
	/**
75
	 * Issuer unique identifier.
76
	 *
77
	 * @var UniqueIdentifier|null $_issuerUniqueID
78
	 */
79
	protected $_issuerUniqueID;
80
	
81
	/**
82
	 * Extensions.
83
	 *
84
	 * @var Extensions $_extensions
85
	 */
86
	protected $_extensions;
87
	
88
	/**
89
	 * Constructor
90
	 *
91
	 * @param Holder $holder AC holder
92
	 * @param AttCertIssuer $issuer AC issuer
93
	 * @param AttCertValidityPeriod $validity Validity
94
	 * @param Attributes $attribs Attributes
95
	 */
96 8
	public function __construct(Holder $holder, AttCertIssuer $issuer, 
97
			AttCertValidityPeriod $validity, Attributes $attribs) {
98 8
		$this->_version = self::VERSION_2;
99 8
		$this->_holder = $holder;
100 8
		$this->_issuer = $issuer;
101 8
		$this->_attrCertValidityPeriod = $validity;
102 8
		$this->_attributes = $attribs;
103 8
		$this->_extensions = new Extensions();
104 8
	}
105
	
106
	/**
107
	 * Initialize from ASN.1.
108
	 *
109
	 * @param Sequence $seq
110
	 * @throws \UnexpectedValueException
111
	 * @return self
112
	 */
113 7
	public static function fromASN1(Sequence $seq) {
114 7
		$version = $seq->at(0)
115 7
			->asInteger()
116 7
			->number();
117 7
		if ($version != self::VERSION_2) {
118 1
			throw new \UnexpectedValueException("Version must be 2.");
119
		}
120 6
		$holder = Holder::fromASN1($seq->at(1)->asSequence());
121 6
		$issuer = AttCertIssuer::fromASN1($seq->at(2));
122 6
		$signature = AlgorithmIdentifier::fromASN1($seq->at(3)->asSequence());
123 6
		if (!$signature instanceof SignatureAlgorithmIdentifier) {
124 1
			throw new \UnexpectedValueException(
125 1
				"Unsupported signature algorithm " . $signature->oid() . ".");
126
		}
127 5
		$serial = $seq->at(4)
128 5
			->asInteger()
129 5
			->number();
130 5
		$validity = AttCertValidityPeriod::fromASN1($seq->at(5)->asSequence());
131 5
		$attribs = Attributes::fromASN1($seq->at(6)->asSequence());
132 5
		$obj = new self($holder, $issuer, $validity, $attribs);
133 5
		$obj->_signature = $signature;
134 5
		$obj->_serialNumber = $serial;
135 5
		$idx = 7;
136 5
		if ($seq->has($idx, Element::TYPE_BIT_STRING)) {
137 1
			$obj->_issuerUniqueID = UniqueIdentifier::fromASN1(
138 1
				$seq->at($idx++)->asBitString());
139 1
		}
140 5
		if ($seq->has($idx, Element::TYPE_SEQUENCE)) {
141 3
			$obj->_extensions = Extensions::fromASN1(
142 3
				$seq->at($idx++)->asSequence());
143 3
		}
144 5
		return $obj;
145
	}
146
	
147
	/**
148
	 * Get self with holder.
149
	 *
150
	 * @param Holder $holder
151
	 * @return self
152
	 */
153 1
	public function withHolder(Holder $holder) {
154 1
		$obj = clone $this;
155 1
		$obj->_holder = $holder;
156 1
		return $obj;
157
	}
158
	
159
	/**
160
	 * Get self with issuer.
161
	 *
162
	 * @param AttCertIssuer $issuer
163
	 * @return self
164
	 */
165 1
	public function withIssuer(AttCertIssuer $issuer) {
166 1
		$obj = clone $this;
167 1
		$obj->_issuer = $issuer;
168 1
		return $obj;
169
	}
170
	
171
	/**
172
	 * Get self with signature algorithm identifier.
173
	 *
174
	 * @param SignatureAlgorithmIdentifier $algo
175
	 * @return self
176
	 */
177 3
	public function withSignature(SignatureAlgorithmIdentifier $algo) {
178 3
		$obj = clone $this;
179 3
		$obj->_signature = $algo;
180 3
		return $obj;
181
	}
182
	
183
	/**
184
	 * Get self with serial number.
185
	 *
186
	 * @param int|string $serial
187
	 * @return self
188
	 */
189 4
	public function withSerialNumber($serial) {
190 4
		$obj = clone $this;
191 4
		$obj->_serialNumber = $serial;
192 4
		return $obj;
193
	}
194
	
195
	/**
196
	 * Get self with random positive serial number.
197
	 *
198
	 * @param int $size Number of random bytes
199
	 * @return self
200
	 */
201 1 View Code Duplication
	public function withRandomSerialNumber($size = 16) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
		// ensure that first byte is always non-zero and having first bit unset
203 1
		$num = gmp_init(mt_rand(1, 0x7f), 10);
204 1
		for ($i = 1; $i < $size; ++$i) {
205 1
			$num <<= 8;
206 1
			$num += mt_rand(0, 0xff);
207 1
		}
208 1
		return $this->withSerialNumber(gmp_strval($num, 10));
209
	}
210
	
211
	/**
212
	 * Get self with validity period.
213
	 *
214
	 * @param AttCertValidityPeriod $validity
215
	 * @return self
216
	 */
217 1
	public function withValidity(AttCertValidityPeriod $validity) {
218 1
		$obj = clone $this;
219 1
		$obj->_attrCertValidityPeriod = $validity;
220 1
		return $obj;
221
	}
222
	
223
	/**
224
	 * Get self with attributes.
225
	 *
226
	 * @param Attributes $attribs
227
	 * @return self
228
	 */
229 1
	public function withAttributes(Attributes $attribs) {
230 1
		$obj = clone $this;
231 1
		$obj->_attributes = $attribs;
232 1
		return $obj;
233
	}
234
	
235
	/**
236
	 * Get self with issuer unique identifier.
237
	 *
238
	 * @param UniqueIdentifier $uid
239
	 * @return self
240
	 */
241 2
	public function withIssuerUniqueID(UniqueIdentifier $uid) {
242 2
		$obj = clone $this;
243 2
		$obj->_issuerUniqueID = $uid;
244 2
		return $obj;
245
	}
246
	
247
	/**
248
	 * Get self with extensions.
249
	 *
250
	 * @param Extensions $extensions
251
	 * @return self
252
	 */
253 2
	public function withExtensions(Extensions $extensions) {
254 2
		$obj = clone $this;
255 2
		$obj->_extensions = $extensions;
256 2
		return $obj;
257
	}
258
	
259
	/**
260
	 * Get version.
261
	 *
262
	 * @return int
263
	 */
264 1
	public function version() {
265 1
		return $this->_version;
266
	}
267
	
268
	/**
269
	 * Get AC holder.
270
	 *
271
	 * @return Holder
272
	 */
273 1
	public function holder() {
274 1
		return $this->_holder;
275
	}
276
	
277
	/**
278
	 * Get AC issuer.
279
	 *
280
	 * @return AttCertIssuer
281
	 */
282 1
	public function issuer() {
283 1
		return $this->_issuer;
284
	}
285
	
286
	/**
287
	 * Check whether signature is set.
288
	 *
289
	 * @return bool
290
	 */
291 13
	public function hasSignature() {
292 13
		return isset($this->_signature);
293
	}
294
	
295
	/**
296
	 * Get signature algorithm identifier.
297
	 *
298
	 * @return SignatureAlgorithmIdentifier
299
	 */
300 13
	public function signature() {
301 13
		if (!$this->hasSignature()) {
302 1
			throw new \LogicException("signature not set.");
303
		}
304 12
		return $this->_signature;
305
	}
306
	
307
	/**
308
	 * Check whether serial number is present.
309
	 *
310
	 * @return bool
311
	 */
312 14
	public function hasSerialNumber() {
313 14
		return isset($this->_serialNumber);
314
	}
315
	
316
	/**
317
	 * Get AC serial number.
318
	 *
319
	 * @return int|string
320
	 */
321 14
	public function serialNumber() {
322 14
		if (!$this->hasSerialNumber()) {
323 1
			throw new \LogicException("serialNumber not set.");
324
		}
325 13
		return $this->_serialNumber;
326
	}
327
	
328
	/**
329
	 * Get validity period.
330
	 *
331
	 * @return AttCertValidityPeriod
332
	 */
333 1
	public function validityPeriod() {
334 1
		return $this->_attrCertValidityPeriod;
335
	}
336
	
337
	/**
338
	 * Get attributes.
339
	 *
340
	 * @return Attributes
341
	 */
342 1
	public function attributes() {
343 1
		return $this->_attributes;
344
	}
345
	
346
	/**
347
	 * Check whether issuer unique identifier is present.
348
	 *
349
	 * @return bool
350
	 */
351 2
	public function hasIssuerUniqueID() {
352 2
		return isset($this->_issuerUniqueID);
353
	}
354
	
355
	/**
356
	 * Get issuer unique identifier.
357
	 *
358
	 * @return UniqueIdentifier
359
	 */
360 2
	public function issuerUniqueID() {
361 2
		if (!$this->hasIssuerUniqueID()) {
362 1
			throw new \LogicException("issuerUniqueID not set.");
363
		}
364 1
		return $this->_issuerUniqueID;
365
	}
366
	
367
	/**
368
	 * Get extensions.
369
	 *
370
	 * @return Extensions
371
	 */
372 1
	public function extensions() {
373 1
		return $this->_extensions;
374
	}
375
	
376
	/**
377
	 * Get ASN.1 structure.
378
	 *
379
	 * @return Sequence
380
	 */
381 11
	public function toASN1() {
382 11
		$elements = array(new Integer($this->_version), 
383 11
			$this->_holder->toASN1(), $this->_issuer->toASN1(), 
384 11
			$this->signature()->toASN1(), new Integer($this->serialNumber()), 
385 11
			$this->_attrCertValidityPeriod->toASN1(), 
386 11
			$this->_attributes->toASN1());
387 11
		if (isset($this->_issuerUniqueID)) {
388 3
			$elements[] = $this->_issuerUniqueID->toASN1();
389 3
		}
390 11
		if (count($this->_extensions)) {
391 6
			$elements[] = $this->_extensions->toASN1();
392 6
		}
393 11
		return new Sequence(...$elements);
394
	}
395
	
396
	/**
397
	 * Create signed attribute certificate.
398
	 *
399
	 * @param Crypto $crypto
400
	 * @param SignatureAlgorithmIdentifier $algo Signature algorithm
401
	 * @param PrivateKeyInfo $privkey_info Private key
402
	 * @return AttributeCertificate
403
	 */
404 1
	public function sign(Crypto $crypto, SignatureAlgorithmIdentifier $algo, 
405
			PrivateKeyInfo $privkey_info) {
406 1
		$aci = clone $this;
407 1
		if (!isset($aci->_serialNumber)) {
408 1
			$aci->_serialNumber = 0;
409 1
		}
410 1
		$aci->_signature = $algo;
411 1
		$data = $aci->toASN1()->toDER();
412 1
		$signature = $crypto->sign($data, $privkey_info, $algo);
413 1
		return new AttributeCertificate($aci, $algo, $signature);
414
	}
415
}
416