| Conditions | 10 |
| Paths | 16 |
| Total Lines | 122 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function testSignatures(): void |
||
| 53 | { |
||
| 54 | if (!class_exists(self::$testedClass)) { |
||
| 55 | $this->markTestSkipped( |
||
| 56 | 'Unable to run ' . self::class . '::testSignatures(). Please set ' . self::class |
||
| 57 | . ':$testedClass to a class-string representing the XML-class being tested', |
||
| 58 | ); |
||
| 59 | } elseif (empty(self::$xmlRepresentation)) { |
||
| 60 | $this->markTestSkipped( |
||
| 61 | 'Unable to run ' . self::class . '::testSignatures(). Please set ' . self::class |
||
| 62 | . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested', |
||
| 63 | ); |
||
| 64 | } else { |
||
| 65 | $algorithms = array_keys(C::$RSA_DIGESTS); |
||
| 66 | foreach ($algorithms as $algorithm) { |
||
| 67 | if ( |
||
| 68 | boolval(OPENSSL_VERSION_NUMBER >= hexdec('0x30000000')) === true |
||
| 69 | && ($algorithm === C::SIG_RSA_SHA1 || $algorithm === C::SIG_RSA_RIPEMD160) |
||
| 70 | ) { |
||
| 71 | // OpenSSL 3.0 disabled SHA1 and RIPEMD160 support |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | // |
||
| 76 | // sign with two certificates |
||
| 77 | // |
||
| 78 | $signer = (new SignatureAlgorithmFactory([]))->getAlgorithm( |
||
| 79 | $algorithm, |
||
| 80 | PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::PRIVATE_KEY), |
||
| 81 | ); |
||
| 82 | |||
| 83 | $keyInfo = new KeyInfo([ |
||
| 84 | new X509Data([new X509Certificate( |
||
| 85 | PEMCertificatesMock::getPlainPublicKeyContents(PEMCertificatesMock::PUBLIC_KEY), |
||
| 86 | )]), |
||
| 87 | new X509Data([new X509Certificate( |
||
| 88 | PEMCertificatesMock::getPlainPublicKeyContents(PEMCertificatesMock::OTHER_PUBLIC_KEY), |
||
| 89 | )]), |
||
| 90 | ]); |
||
| 91 | |||
| 92 | $unsigned = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement); |
||
| 93 | $unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $keyInfo); |
||
| 94 | $signed = self::$testedClass::fromXML($unsigned->toXML()); |
||
| 95 | $this->assertEquals( |
||
| 96 | $algorithm, |
||
| 97 | $signed->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(), |
||
| 98 | ); |
||
| 99 | |||
| 100 | // verify signature |
||
| 101 | $verifier = (new SignatureAlgorithmFactory([]))->getAlgorithm( |
||
| 102 | $signed->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(), |
||
| 103 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY), |
||
| 104 | ); |
||
| 105 | |||
| 106 | try { |
||
| 107 | $verified = $signed->verify($verifier); |
||
| 108 | } catch ( |
||
| 109 | NoSignatureFoundException | |
||
| 110 | InvalidArgumentException | |
||
| 111 | SignatureVerificationFailedException $s |
||
| 112 | ) { |
||
| 113 | $this->fail(sprintf('%s: %s', $algorithm, $e->getMessage())); |
||
| 114 | } |
||
| 115 | $this->assertInstanceOf(self::$testedClass, $verified); |
||
| 116 | |||
| 117 | $this->assertEquals( |
||
| 118 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY), |
||
| 119 | $verified->getVerifyingKey(), |
||
| 120 | sprintf('No validating certificate for algorithm: %s', $algorithm), |
||
| 121 | ); |
||
| 122 | |||
| 123 | // |
||
| 124 | // sign without certificates |
||
| 125 | // |
||
| 126 | $unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, null); |
||
| 127 | $signed = self::$testedClass::fromXML($unsigned->toXML()); |
||
| 128 | |||
| 129 | // verify signature |
||
| 130 | try { |
||
| 131 | $verified = $signed->verify($verifier); |
||
| 132 | } catch ( |
||
| 133 | NoSignatureFoundException | |
||
| 134 | InvalidArgumentException | |
||
| 135 | SignatureVerificationFailedException $e |
||
| 136 | ) { |
||
| 137 | $this->fail(sprintf('%s: %s', $algorithm, $e->getMessage())); |
||
| 138 | } |
||
| 139 | $this->assertInstanceOf(self::$testedClass, $verified); |
||
| 140 | |||
| 141 | $this->assertEquals( |
||
| 142 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY), |
||
| 143 | $verified->getVerifyingKey(), |
||
| 144 | sprintf('No validating certificate for algorithm: %s', $algorithm), |
||
| 145 | ); |
||
| 146 | |||
| 147 | // |
||
| 148 | // verify with wrong key |
||
| 149 | // |
||
| 150 | $signer = (new SignatureAlgorithmFactory([]))->getAlgorithm( |
||
| 151 | $algorithm, |
||
| 152 | PEMCertificatesMock::getPrivateKey(PEMCertificatesMock::OTHER_PRIVATE_KEY), |
||
| 153 | ); |
||
| 154 | $unsigned->sign($signer, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, null); |
||
| 155 | $signed = self::$testedClass::fromXML($unsigned->toXML()); |
||
| 156 | |||
| 157 | // verify signature |
||
| 158 | try { |
||
| 159 | $verified = $signed->verify($verifier); |
||
| 160 | $this->fail('Signature validated correctly with wrong certificate.'); |
||
| 161 | } catch ( |
||
| 162 | NoSignatureFoundException | |
||
| 163 | InvalidArgumentException | |
||
| 164 | SignatureVerificationFailedException $e |
||
| 165 | ) { |
||
| 166 | $this->assertEquals('Failed to verify signature.', $e->getMessage()); |
||
| 167 | } |
||
| 168 | $this->assertInstanceOf(self::$testedClass, $verified); |
||
| 169 | |||
| 170 | $this->assertEquals( |
||
| 171 | PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY), |
||
| 172 | $verified->getVerifyingKey(), |
||
| 173 | sprintf('No validating certificate for algorithm: %s', $algorithm), |
||
| 174 | ); |
||
| 179 |