1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
<?php
/*
* This file is part of the PHPASN1 library.
*
* Copyright © Friedrich Große <friedrich.grosse@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FG\X509\CSR;
use FG\ASN1\OID;
use FG\ASN1\Universal\Integer;
use FG\ASN1\Universal\BitString;
use FG\ASN1\Universal\Sequence;
use FG\X509\CertificateSubject;
use FG\X509\AlgorithmIdentifier;
use FG\X509\PublicKey;
class CSR extends Sequence
{
const CSR_VERSION_NR = 0;
protected $subject;
protected $publicKey;
protected $signature;
protected $signatureAlgorithm;
protected $startSequence;
/**
* @param string $commonName
* @param string $email
* @param string $organization
* @param string $locality
* @param string $state
* @param string $country
* @param string $organizationalUnit
* @param string $publicKey
* @param string $signature
* @param string $signatureAlgorithm
*/
public function __construct($commonName, $email, $organization, $locality, $state, $country, $organizationalUnit, $publicKey, $signature = null, $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE)
{
$this->subject = new CertificateSubject(
$commonName,
$email,
$organization,
$locality,
$state,
$country,
$organizationalUnit
);
$this->publicKey = $publicKey;
$this->signature = $signature;
$this->signatureAlgorithm = $signatureAlgorithm;
if (isset($signature)) {
$this->createCSRSequence();
}
}
protected function createCSRSequence()
{
$versionNr = new Integer(self::CSR_VERSION_NR);
$publicKey = new PublicKey($this->publicKey);
$signature = new BitString($this->signature);
$signatureAlgorithm = new AlgorithmIdentifier($this->signatureAlgorithm);
$certRequestInfo = new Sequence($versionNr, $this->subject, $publicKey);
// Clear the underlying Construct
$this->rewind();
$this->children = [];
$this->addChild($certRequestInfo);
$this->addChild($signatureAlgorithm);
$this->addChild($signature);
}
public function getSignatureSubject()
{
$versionNr = new Integer(self::CSR_VERSION_NR);
$publicKey = new PublicKey($this->publicKey);
$certRequestInfo = new Sequence($versionNr, $this->subject, $publicKey);
return $certRequestInfo->getBinary();
}
public function setSignature($signature, $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE)
{
$this->signature = $signature;
$this->signatureAlgorithm = $signatureAlgorithm;
$this->createCSRSequence();
}
public function __toString()
{
$tmp = base64_encode($this->getBinary());
for ($i = 0; $i < strlen($tmp); $i++) {
if (($i + 2) % 65 == 0) {
$tmp = substr($tmp, 0, $i + 1)."\n".substr($tmp, $i + 1);
}
}
$result = '-----BEGIN CERTIFICATE REQUEST-----'.PHP_EOL;
$result .= $tmp.PHP_EOL;
$result .= '-----END CERTIFICATE REQUEST-----';
return $result;
}
public function getVersion()
{
return self::CSR_VERSION_NR;
}
public function getOrganizationName()
{
return $this->subject->getOrganization();
}
public function getLocalName()
{
return $this->subject->getLocality();
}
public function getState()
{
return $this->subject->getState();
}
public function getCountry()
{
return $this->subject->getCountry();
}
public function getOrganizationalUnit()
{
return $this->subject->getOrganizationalUnit();
}
public function getPublicKey()
{
return $this->publicKey;
}
public function getSignature()
{
return $this->signature;
}
public function getSignatureAlgorithm()
{
return $this->signatureAlgorithm;
}
}
|