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 ( cafc8c...f882cd )
by Joni
04:19
created

AttributeCertificate::fromASN1()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 10
loc 10
ccs 6
cts 8
cp 0.75
rs 9.4285
c 3
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace X509\AttributeCertificate;
4
5
use ASN1\Type\Constructed\Sequence;
6
use CryptoUtil\ASN1\AlgorithmIdentifier;
7
use CryptoUtil\ASN1\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
8
use CryptoUtil\ASN1\PublicKeyInfo;
9
use CryptoUtil\Crypto\Crypto;
10
use CryptoUtil\Crypto\Signature;
11
use CryptoUtil\PEM\PEM;
12
13
14
/**
15
 * Implements <i>AttributeCertificate</i> ASN.1 type.
16
 *
17
 * @link https://tools.ietf.org/html/rfc5755#section-4.1
18
 */
19
class AttributeCertificate
20
{
21
	/**
22
	 * Attribute certificate info.
23
	 *
24
	 * @var AttributeCertificateInfo $_acinfo
25
	 */
26
	protected $_acinfo;
27
	
28
	/**
29
	 * Signature algorithm identifier.
30
	 *
31
	 * @var SignatureAlgorithmIdentifier $_signatureAlgorithm
32
	 */
33
	protected $_signatureAlgorithm;
34
	
35
	/**
36
	 * Signature value.
37
	 *
38
	 * @var Signature $_signatureValue
39
	 */
40
	protected $_signatureValue;
41
	
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param AttributeCertificateInfo $acinfo
46
	 * @param SignatureAlgorithmIdentifier $algo
47
	 * @param Signature $signature
48
	 */
49 5
	public function __construct(AttributeCertificateInfo $acinfo, 
50
			SignatureAlgorithmIdentifier $algo, Signature $signature) {
51 5
		$this->_acinfo = $acinfo;
52 5
		$this->_signatureAlgorithm = $algo;
53 5
		$this->_signatureValue = $signature;
54 5
	}
55
	
56
	/**
57
	 * Initialize from ASN.1.
58
	 *
59
	 * @param Sequence $seq
60
	 * @return self
61
	 */
62 3 View Code Duplication
	public static function fromASN1(Sequence $seq) {
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...
63 3
		$acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence());
64 3
		$algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
65 3
		if (!$algo instanceof SignatureAlgorithmIdentifier) {
66
			throw new \UnexpectedValueException(
67
				"Unsupported signature algorithm " . $algo->oid() . ".");
68
		}
69 3
		$signature = Signature::fromASN1($seq->at(2)->asBitString());
70 3
		return new self($acinfo, $algo, $signature);
71
	}
72
	
73
	/**
74
	 * Initialize from DER data.
75
	 *
76
	 * @param string $data
77
	 * @return self
78
	 */
79 1
	public static function fromDER($data) {
80 1
		return self::fromASN1(Sequence::fromDER($data));
81
	}
82
	
83
	/**
84
	 * Initialize from PEM.
85
	 *
86
	 * @param PEM $pem
87
	 * @throws \UnexpectedValueException
88
	 * @return self
89
	 */
90 2
	public static function fromPEM(PEM $pem) {
91 2
		if ($pem->type() !== PEM::TYPE_ATTRIBUTE_CERTIFICATE) {
92 1
			throw new \UnexpectedValueException("Invalid PEM type.");
93
		}
94 1
		return self::fromDER($pem->data());
95
	}
96
	
97
	/**
98
	 * Get attribute certificate info.
99
	 *
100
	 * @return AttributeCertificateInfo
101
	 */
102 2
	public function acinfo() {
103 2
		return $this->_acinfo;
104
	}
105
	
106
	/**
107
	 * Get signature algorithm identifier.
108
	 *
109
	 * @return SignatureAlgorithmIdentifier
110
	 */
111 2
	public function signatureAlgorithm() {
112 2
		return $this->_signatureAlgorithm;
113
	}
114
	
115
	/**
116
	 * Get signature value.
117
	 *
118
	 * @return Signature
119
	 */
120 1
	public function signatureValue() {
121 1
		return $this->_signatureValue;
122
	}
123
	
124
	/**
125
	 * Get ASN.1 structure.
126
	 *
127
	 * @return Sequence
128
	 */
129 3
	public function toASN1() {
130 3
		return new Sequence($this->_acinfo->toASN1(), 
131 3
			$this->_signatureAlgorithm->toASN1(), 
132 3
			$this->_signatureValue->toBitString());
133
	}
134
	
135
	/**
136
	 * Get attribute certificate as a DER.
137
	 *
138
	 * @return string
139
	 */
140 2
	public function toDER() {
141 2
		return $this->toASN1()->toDER();
142
	}
143
	
144
	/**
145
	 * Get attribute certificate as a PEM.
146
	 *
147
	 * @return PEM
148
	 */
149 2
	public function toPEM() {
150 2
		return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER());
151
	}
152
	
153
	/**
154
	 * Verify signature.
155
	 *
156
	 * @param Crypto $crypto
157
	 * @param PublicKeyInfo $pubkey_info Signer's public key
158
	 * @return bool
159
	 */
160 2
	public function verify(Crypto $crypto, PublicKeyInfo $pubkey_info) {
161 2
		$data = $this->_acinfo->toASN1()->toDER();
162 2
		return $crypto->verify($data, $this->_signatureValue, $pubkey_info, 
163 2
			$this->_signatureAlgorithm);
164
	}
165
	
166
	/**
167
	 * Get attribute certificate as a PEM formatted string.
168
	 *
169
	 * @return string
170
	 */
171 1
	public function __toString() {
172 1
		return $this->toPEM()->string();
173
	}
174
}
175