1717use FG \ASN1 \Exception \ParserException ;
1818use FG \ASN1 \ExplicitlyTaggedObject ;
1919use FG \ASN1 \Universal \BitString ;
20+ use FG \ASN1 \Universal \Boolean ;
2021use FG \ASN1 \Universal \Integer ;
2122use FG \ASN1 \Universal \NullObject ;
2223use FG \ASN1 \Universal \ObjectIdentifier ;
2526
2627class 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}
0 commit comments