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