Skip to content

Commit 990b0cb

Browse files
committed
moving requests to template + coveralls bug
1 parent be19f22 commit 990b0cb

3 files changed

Lines changed: 40 additions & 76 deletions

File tree

src/Template.php

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use FG\ASN1\Exception\ParserException;
1818
use FG\ASN1\ExplicitlyTaggedObject;
1919
use FG\ASN1\Universal\BitString;
20+
use FG\ASN1\Universal\Boolean;
2021
use FG\ASN1\Universal\Integer;
2122
use FG\ASN1\Universal\NullObject;
2223
use FG\ASN1\Universal\ObjectIdentifier;
@@ -25,6 +26,27 @@
2526

2627
class Template
2728
{
29+
/**
30+
* @param OctetString $data
31+
* @param string $hashAlgorithmOID
32+
* @return TimeStampRequest
33+
* @throws FormatException
34+
*/
35+
public static function TimeStampRequest(OctetString $data, string $hashAlgorithmOID = Algorithm::OID_SHA256): TimeStampRequest
36+
{
37+
$tspRequest = Sequence::create([Integer::create(1),Sequence::create([Sequence::create([
38+
ObjectIdentifier::create($hashAlgorithmOID),
39+
NullObject::create(),
40+
]),
41+
OctetString::createFromString(Algorithm::hashValue($hashAlgorithmOID, $data->getBinaryContent()))
42+
]),
43+
Integer::create(rand() << 32 | rand()),
44+
Boolean::create(true),
45+
]);
46+
47+
return new TimeStampRequest($tspRequest);
48+
}
49+
2850
/**
2951
* @param Certificate $publicCertificate
3052
* @param Certificate $intermediateCertificate
@@ -36,45 +58,19 @@ class Template
3658
public static function OCSPRequest(Certificate $publicCertificate, Certificate $intermediateCertificate, string $hashAlgorithmOID = Algorithm::OID_SHA1): OCSPRequest
3759
{
3860
/** @see Maps\TBSRequest */
39-
$tbsRequest = Sequence::create([
40-
// потомки TBSRequest
41-
# -- version
42-
# -- requestorName
43-
# requestList
44-
Sequence::create([
45-
/** @see Request */
46-
Sequence::create([
47-
// потомки Request
48-
# reqCert
49-
/** @see CertID */
50-
Sequence::create([
51-
// потомки CertID
52-
# hashAlgorithm
53-
/** @see AlgorithmIdentifier */
54-
Sequence::create([
55-
// потомки AlgorithmIdentifier
56-
# algorithm
57-
ObjectIdentifier::create($hashAlgorithmOID),
58-
# parameters
59-
NullObject::create()
61+
$tbsRequest = Sequence::create([Sequence::create([ Sequence::create([ Sequence::create([Sequence::create([ ObjectIdentifier::create($hashAlgorithmOID),NullObject::create()
6062
]
6163
),
62-
# issuerNameHash
6364
OctetString::createFromString(self::getNameHash($hashAlgorithmOID, $intermediateCertificate)),
64-
# issuerKeyHash
6565
OctetString::createFromString(self::getKeyHash($hashAlgorithmOID, $intermediateCertificate)), # serialNumber
6666
Integer::create($publicCertificate->getSerial())
6767
]
6868
)
69-
# singleRequestExtensions
7069
]
7170
)
7271
]
7372
),
74-
# requestExtensions
75-
ExplicitlyTaggedObject::create(2,
76-
Sequence::create([
77-
Sequence::create([
73+
ExplicitlyTaggedObject::create(2,Sequence::create([ Sequence::create([
7874
ObjectIdentifier::create(OCSPRequest::OID_OCSPNonce),
7975
OctetString::createFromString(OctetString::createFromString((string)self::generateNonce())->getBinary())
8076
]
@@ -88,18 +84,6 @@ public static function OCSPRequest(Certificate $publicCertificate, Certificate $
8884
return new OCSPRequest(Sequence::create([$tbsRequest/*, $optionalSignature*/]));
8985
}
9086

91-
92-
/**
93-
* @param int|null $length
94-
* @return string
95-
* @throws Exception
96-
*/
97-
private static function generateNonce(int $length = null): string
98-
{
99-
return random_bytes($length ?? OCSPRequest::OCSP_DEFAULT_NONCE_LENGTH);
100-
}
101-
102-
10387
/**
10488
* @param string $algorithmOID
10589
* @param Certificate $certificate
@@ -115,6 +99,16 @@ private static function getNameHash(string $algorithmOID, Certificate $certifica
11599
return Algorithm::hashValue($algorithmOID, self::_getTBSCertificate($certificate)->getChildren()[5]->getBinary());
116100
}
117101

102+
/**
103+
* @param Sequence $certificate
104+
* @return Sequence
105+
* @throws Exception
106+
*/
107+
private static function _getTBSCertificate(Sequence $certificate): Sequence
108+
{
109+
return $certificate->findChildrenByType(Sequence::class)[0];
110+
}
111+
118112
/**
119113
* @param string $algorithmOID
120114
* @param Certificate $certificate
@@ -135,12 +129,12 @@ public static function getKeyHash(string $algorithmOID, Certificate $certificate
135129
}
136130

137131
/**
138-
* @param Sequence $certificate
139-
* @return Sequence
132+
* @param int|null $length
133+
* @return string
140134
* @throws Exception
141135
*/
142-
private static function _getTBSCertificate(Sequence $certificate): Sequence
136+
private static function generateNonce(int $length = null): string
143137
{
144-
return $certificate->findChildrenByType(Sequence::class)[0];
138+
return random_bytes($length ?? OCSPRequest::OCSP_DEFAULT_NONCE_LENGTH);
145139
}
146140
}

src/TimeStampRequest.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010

1111
namespace Falseclock\AdvancedCMS;
1212

13-
use Adapik\CMS\Algorithm;
1413
use Adapik\CMS\CMSBase;
1514
use Adapik\CMS\Exception\FormatException;
1615
use Adapik\CMS\MessageImprint;
1716
use Exception;
1817
use FG\ASN1\ASN1ObjectInterface;
1918
use FG\ASN1\Universal\Boolean;
2019
use FG\ASN1\Universal\Integer;
21-
use FG\ASN1\Universal\NullObject;
2220
use FG\ASN1\Universal\ObjectIdentifier;
23-
use FG\ASN1\Universal\OctetString;
2421
use FG\ASN1\Universal\Sequence;
2522

2623
/**
@@ -48,34 +45,6 @@ public static function createFromContent(string $content): CMSBase
4845
return new self(self::makeFromContent($content, Maps\TimeStampRequest::class, Sequence::class));
4946
}
5047

51-
/**
52-
* @param OctetString|ASN1ObjectInterface $data data which should be queried with TS request
53-
* @param string $hashAlgorithmOID
54-
* @return TimeStampRequest
55-
* @throws Exception
56-
*/
57-
public static function createSimple(OctetString $data, string $hashAlgorithmOID = Algorithm::OID_SHA256): TimeStampRequest
58-
{
59-
$tspRequest = Sequence::create([
60-
# version
61-
Integer::create(1),
62-
# messageImprint
63-
Sequence::create([
64-
Sequence::create([
65-
ObjectIdentifier::create($hashAlgorithmOID),
66-
NullObject::create(),
67-
]),
68-
OctetString::createFromString(Algorithm::hashValue($hashAlgorithmOID, $data->getBinaryContent()))
69-
]),
70-
# nonce
71-
Integer::create(rand() << 32 | rand()),
72-
# certReq
73-
Boolean::create(true),
74-
]);
75-
76-
return new self($tspRequest);
77-
}
78-
7948
/**
8049
* @return Boolean
8150
* @throws Exception

tests/TimeStampRequestTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Falseclock\AdvancedCMS\Test;
55

66
use Adapik\CMS\MessageImprint;
7+
use Falseclock\AdvancedCMS\Template;
78
use Falseclock\AdvancedCMS\TimeStampRequest;
89
use Falseclock\AdvancedCMS\TimeStampResponse;
910
use FG\ASN1\Universal\Boolean;
@@ -26,7 +27,7 @@ public function testPolicy()
2627
function testCreate()
2728
{
2829
$binary = "123456";
29-
$timeStampRequest = TimeStampRequest::createSimple(OctetString::createFromString($binary));
30+
$timeStampRequest = Template::TimeStampRequest(OctetString::createFromString($binary));
3031

3132
$timeStampRequest = TimeStampRequest::createFromContent($timeStampRequest->getBinary());
3233

0 commit comments

Comments
 (0)