@@ -177,68 +177,15 @@ protected function getSignatureDetails($path, $signatureFieldName)
177177
178178 protected function validate ($ path , $ signatureFieldName , $ certificate )
179179 {
180- list ($ tmpFile , $ asn1 ) = $ this ->getSignatureDetails ($ path , $ signatureFieldName );
181-
182- $ contentType = $ asn1 ->getChild (0 )->getValue ();
183- $ contentType = \SetaPDF_Signer_Asn1_Oid::decode ($ contentType );
184- $ this ->assertEquals ('1.2.840.113549.1.7.2 ' , $ contentType );
185-
186- /** @var \SetaPDF_Signer_Asn1_Element $content */
187- $ content = $ asn1 ->getChild (1 );
188- $ signedData = $ content ->getChild (0 );
189-
190- $ digestAlgorithms = $ signedData ->getChild (1 );
191- $ hashes = array ();
192- foreach ($ digestAlgorithms ->getChildren () AS $ algorithm ) {
193- $ algorithmOid = \SetaPDF_Signer_Asn1_Oid::decode ($ algorithm ->getChild (0 )->getValue ());
194- $ digest = \SetaPDF_Signer_Digest::getByOid ($ algorithmOid );
195- $ hashes [$ digest ] = hash_file ($ digest , $ tmpFile ->getPath (), true );
196- }
197-
198- // ensure that no eContent is used
199- $ encapContentInfo = $ signedData ->getChild (2 );
200- $ this ->assertEquals (1 , $ encapContentInfo ->getChildCount ());
201-
202- $ signerInfos = $ signedData ->getChild ($ signedData ->getChildCount () - 1 );
203- // only one SignerInfo
204- $ this ->assertEquals (1 , $ signerInfos ->getChildCount ());
205- $ signerInfo = $ signerInfos ->getChild (0 );
206- // get digest algo and check if it was defined in digestAlgorithms
207- $ digestAlgorithmOid = \SetaPDF_Signer_Asn1_Oid::decode ($ signerInfo ->getChild (2 )->getChild (0 )->getValue ());
208- $ digest = \SetaPDF_Signer_Digest::getByOid ($ digestAlgorithmOid );
209- $ this ->assertTrue (isset ($ hashes [$ digest ]));
210-
211- // Check for signed attributes
212- if ($ signerInfo ->getChild (3 )->getIdent () === "\xA0" ) { // [0] IMPLICIT
213- $ _signedAttributes = $ signerInfo ->getChild (3 )->getChildren ();
214- $ signedAttributes = array ();
215- foreach ($ _signedAttributes as $ attribute ) {
216- $ attrType = $ attribute ->getChild (0 )->getValue ();
217- $ attrTypeOid = \SetaPDF_Signer_Asn1_Oid::decode ($ attrType );
218- $ signedAttributes [$ attrTypeOid ] = $ attribute ->getChild (1 );
219- }
220-
221- // check for mandatory attributes
222- $ this ->assertTrue (isset ($ signedAttributes ['1.2.840.113549.1.9.3 ' ])); // content-type
223- $ this ->assertTrue (isset ($ signedAttributes ['1.2.840.113549.1.9.4 ' ])); // message-digest
180+ $ document = \SetaPDF_Core_Document::loadByFilename ($ path );
181+ $ result = \SetaPDF_Signer_ValidationRelatedInfo_IntegrityResult::create ($ document , $ signatureFieldName );
224182
225- // hashes match?
226- $ this ->assertEquals ($ hashes [$ digest ], $ signedAttributes ['1.2.840.113549.1.9.4 ' ]->getChild (0 )->getValue ());
227-
228- $ data = $ signerInfo ->getChild (3 )->__toString ();
229- $ data [0 ] = \SetaPDF_Signer_Asn1_Element::SET | \SetaPDF_Signer_Asn1_Element::IS_CONSTRUCTED ;
230- $ signatureValue = $ signerInfo ->getChild (5 )->getValue ();
231-
232- } else {
233- $ data = file_get_contents ($ tmpFile ->getPath ());
234- $ signatureValue = $ signerInfo ->getChild (4 )->getValue ();
235- }
183+ $ this ->assertTrue ($ result ->isValid ());
236184
237- while (\openssl_error_string ());
185+ $ certificate = new \SetaPDF_Signer_X509_Certificate ($ certificate );
186+ $ signingCertificate = $ result ->getSignedData ()->getSigningCertificate ();
238187
239- $ pkey = \openssl_pkey_get_public ($ certificate );
240- $ res = \openssl_verify ($ data , $ signatureValue , $ pkey , $ digest );
241- $ this ->assertEquals (1 , $ res , openssl_error_string ());
188+ $ this ->assertEquals ($ certificate ->getDigest (), $ signingCertificate ->getDigest ());
242189
243190 return true ;
244191 }
0 commit comments