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