Skip to content

Commit 191d9f8

Browse files
committed
refactor for pull request
1 parent 180c27d commit 191d9f8

18 files changed

Lines changed: 197 additions & 24 deletions

src/Maps/OCSPRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Falseclock\AdvancedCMS\Maps;
1212

13-
use Adapik\CMS\Maps\Signature;
1413
use FG\ASN1\Identifier;
1514

1615
abstract class OCSPRequest

src/Maps/Signature.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Signature
4+
*
5+
* @author Nurlan Mukhanov <nurike@gmail.com>
6+
* @copyright 2020 Nurlan Mukhanov
7+
* @license https://en.wikipedia.org/wiki/MIT_License MIT License
8+
* @link https://github.com/Falseclock/AdvancedCMS
9+
*/
10+
11+
namespace Falseclock\AdvancedCMS\Maps;
12+
13+
use Adapik\CMS\Maps\AlgorithmIdentifier;
14+
use Adapik\CMS\Maps\Certificate;
15+
use FG\ASN1\Identifier;
16+
17+
abstract class Signature
18+
{
19+
/**
20+
* Signature ::= SEQUENCE {
21+
* signatureAlgorithm AlgorithmIdentifier,
22+
* signature BIT STRING,
23+
* certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL
24+
* }
25+
*/
26+
const MAP = [
27+
'type' => Identifier::SEQUENCE,
28+
'children' => [
29+
'signatureAlgorithm' => AlgorithmIdentifier::MAP,
30+
'signature' => ['type' => Identifier::BITSTRING],
31+
'certs' => [
32+
'constant' => 0,
33+
'explicit' => true,
34+
'optional' => true,
35+
'min' => 1,
36+
'max' => -1,
37+
'type' => Identifier::SEQUENCE,
38+
'children' => Certificate::MAP
39+
],
40+
],
41+
];
42+
}

src/OCSPRequest.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
use Adapik\CMS\Certificate;
1515
use Adapik\CMS\CMSBase;
1616
use Adapik\CMS\Exception\FormatException;
17-
use Adapik\CMS\Signature;
1817
use Exception;
1918
use FG\ASN1\Exception\ParserException;
2019
use FG\ASN1\ExplicitlyTaggedObject;
20+
use FG\ASN1\Universal\BitString;
2121
use FG\ASN1\Universal\Integer;
2222
use FG\ASN1\Universal\NullObject;
2323
use FG\ASN1\Universal\ObjectIdentifier;
@@ -46,7 +46,7 @@ class OCSPRequest extends CMSBase
4646
* @return OCSPRequest
4747
* @throws FormatException
4848
*/
49-
public static function createFromContent(string $content): self
49+
public static function createFromContent(string $content): CMSBase
5050
{
5151
return new self(self::makeFromContent($content, Maps\OCSPRequest::class, Sequence::class));
5252
}
@@ -58,6 +58,7 @@ public static function createFromContent(string $content): self
5858
*
5959
* @return OCSPRequest
6060
* @throws FormatException
61+
* @throws ParserException
6162
*/
6263
public static function createSimple(Certificate $publicCertificate, Certificate $intermediateCertificate, string $hashAlgorithmOID = Algorithm::OID_SHA1): self
6364
{
@@ -86,10 +87,9 @@ public static function createSimple(Certificate $publicCertificate, Certificate
8687
]
8788
),
8889
# issuerNameHash
89-
Algorithm::hashValue($hashAlgorithmOID, $intermediateCertificate->getSubject()->getBinary()),
90+
OctetString::createFromString(self::getNameHash($hashAlgorithmOID, $intermediateCertificate)),
9091
# issuerKeyHash
91-
Algorithm::hashValue($hashAlgorithmOID, $intermediateCertificate->getPublicKey()->getKey()->getBinary()),
92-
# serialNumber
92+
OctetString::createFromString(self::getKeyHash($hashAlgorithmOID, $intermediateCertificate)), # serialNumber
9393
Integer::create($publicCertificate->getSerial())
9494
]
9595
)
@@ -148,4 +148,48 @@ public function getOptionalSignature(): ?Signature
148148

149149
return null;
150150
}
151+
152+
/**
153+
* @param string $algorithmOID
154+
* @param Certificate $certificate
155+
* @return string
156+
* @throws FormatException
157+
* @throws ParserException
158+
*/
159+
private static function getNameHash(string $algorithmOID, Certificate $certificate): string
160+
{
161+
$binary = $certificate->getBinary();
162+
/** @var Sequence $certificate */
163+
$certificate = Sequence::fromBinary($binary);
164+
return Algorithm::hashValue($algorithmOID, self::_getTBSCertificate($certificate)->getChildren()[5]->getBinary());
165+
}
166+
167+
/**
168+
* @param string $algorithmOID
169+
* @param Certificate $certificate
170+
* @return string
171+
* @throws FormatException
172+
* @throws ParserException
173+
*/
174+
private static function getKeyHash(string $algorithmOID, Certificate $certificate): string
175+
{
176+
$binary = $certificate->getBinary();
177+
/** @var Sequence $certificate */
178+
$certificate = Sequence::fromBinary($binary);
179+
$child = self::_getTBSCertificate($certificate)->getChildren()[6];
180+
/** @var BitString $octet */
181+
$octet = $child->findChildrenByType(BitString::class)[0];
182+
183+
return Algorithm::hashValue($algorithmOID, hex2bin($octet->getStringValue()));
184+
}
185+
186+
/**
187+
* @param Sequence $certificate
188+
* @return Sequence
189+
* @throws Exception
190+
*/
191+
private static function _getTBSCertificate(Sequence $certificate): Sequence
192+
{
193+
return $certificate->findChildrenByType(Sequence::class)[0];
194+
}
151195
}

src/OCSPResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class OCSPResponse extends CMSBase
3535
* @return OCSPResponse
3636
* @throws FormatException
3737
*/
38-
public static function createFromContent(string $content): self
38+
public static function createFromContent(string $content): CMSBase
3939
{
4040
return new self(self::makeFromContent($content, Maps\OCSPResponse::class, Sequence::class));
4141
}

src/OCSPResponseStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OCSPResponseStatus extends CMSBase
3232
* @return OCSPResponseStatus
3333
* @throws FormatException
3434
*/
35-
public static function createFromContent(string $content): self
35+
public static function createFromContent(string $content): CMSBase
3636
{
3737
return new self(self::makeFromContent($content, Maps\OCSPResponseStatus::class, Enumerated::class));
3838
}

src/PKIStatusInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PKIStatusInfo extends CMSBase
3434
* @return PKIStatusInfo
3535
* @throws FormatException
3636
*/
37-
public static function createFromContent(string $content): self
37+
public static function createFromContent(string $content): CMSBase
3838
{
3939
return new self(self::makeFromContent($content, Maps\PKIStatusInfo::class, Sequence::class));
4040
}

src/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Request extends CMSBase
3535
* @return Request
3636
* @throws FormatException
3737
*/
38-
public static function createFromContent(string $content): self
38+
public static function createFromContent(string $content): CMSBase
3939
{
4040
return new self(self::makeFromContent($content, Maps\Request::class, Sequence::class));
4141
}

src/ResponseBytes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ResponseBytes extends CMSBase
3434
* @return ResponseBytes
3535
* @throws FormatException
3636
*/
37-
public static function createFromContent(string $content): self
37+
public static function createFromContent(string $content): CMSBase
3838
{
3939
return new self(self::makeFromContent($content, Maps\ResponseBytes::class, Sequence::class));
4040
}

src/Signature.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Signature
4+
*
5+
* @author Nurlan Mukhanov <nurike@gmail.com>
6+
* @copyright 2020 Nurlan Mukhanov
7+
* @license https://en.wikipedia.org/wiki/MIT_License MIT License
8+
* @link https://github.com/Falseclock/AdvancedCMS
9+
*/
10+
11+
namespace Falseclock\AdvancedCMS;
12+
13+
use Adapik\CMS\AlgorithmIdentifier;
14+
use Adapik\CMS\Certificate;
15+
use Adapik\CMS\CMSBase;
16+
use Adapik\CMS\Exception\FormatException;
17+
use FG\ASN1\Exception\ParserException;
18+
use FG\ASN1\ExplicitlyTaggedObject;
19+
use FG\ASN1\Universal\BitString;
20+
use FG\ASN1\Universal\Sequence;
21+
22+
/**
23+
* Class Signature
24+
*
25+
* @see Maps\Signature
26+
* @package Adapik\CMS
27+
*/
28+
class Signature extends CMSBase
29+
{
30+
/**
31+
* @var Sequence
32+
*/
33+
protected $object;
34+
35+
/**
36+
* @param string $content
37+
* @return Signature
38+
* @throws FormatException
39+
*/
40+
public static function createFromContent(string $content): CMSBase
41+
{
42+
return new self(self::makeFromContent($content, Maps\Signature::class, Sequence::class));
43+
}
44+
45+
/**
46+
* @return AlgorithmIdentifier
47+
*/
48+
public function getSignatureAlgorithm(): AlgorithmIdentifier
49+
{
50+
$signatureAlgorithm = $this->object->getChildren()[0];
51+
52+
return new AlgorithmIdentifier($signatureAlgorithm);
53+
}
54+
55+
/**
56+
* @return BitString
57+
* @throws ParserException
58+
*/
59+
public function getSignature(): BitString
60+
{
61+
$binary = $this->object->getChildren()[1]->getBinary();
62+
63+
return BitString::fromBinary($binary);
64+
}
65+
66+
/**
67+
* @return Certificate[]
68+
*/
69+
public function getCerts(): array
70+
{
71+
$certificates = [];
72+
73+
if (count($this->object->getChildren()) == 3) {
74+
/** @var ExplicitlyTaggedObject $certs */
75+
$certs = $this->object->getChildren()[2];
76+
77+
foreach ($certs->getChildren() as $cert) {
78+
$certificates[] = new Certificate($cert->getChildren()[0]);
79+
}
80+
}
81+
return $certificates;
82+
}
83+
}

src/SignedData.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Falseclock\AdvancedCMS;
1313

14+
use Adapik\CMS\CMSBase;
1415
use Adapik\CMS\Exception\FormatException;
1516
use Exception;
1617
use FG\ASN1\ExplicitlyTaggedObject;
@@ -31,7 +32,7 @@ class SignedData extends \Adapik\CMS\SignedData
3132
* @return SignedData
3233
* @throws FormatException
3334
*/
34-
public static function createFromContent(string $content): self
35+
public static function createFromContent(string $content): CMSBase
3536
{
3637
return new self(self::makeFromContent($content, \Adapik\CMS\Maps\SignedData::class, Sequence::class));
3738
}

0 commit comments

Comments
 (0)