simplesamlphp /
xml-security
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSecurity\Alg\Signature; |
||
| 6 | |||
| 7 | use SimpleSAML\Assert\Assert; |
||
| 8 | use SimpleSAML\XMLSecurity\Backend; |
||
| 9 | use SimpleSAML\XMLSecurity\Backend\SignatureBackend; |
||
| 10 | use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException; |
||
| 11 | use SimpleSAML\XMLSecurity\Key\KeyInterface; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * An abstract class that implements a generic digital signature algorithm. |
||
| 15 | * |
||
| 16 | * @package simplesamlphp/xml-security |
||
| 17 | */ |
||
| 18 | abstract class AbstractSigner implements SignatureAlgorithmInterface |
||
| 19 | { |
||
| 20 | /** @var string */ |
||
| 21 | protected const DEFAULT_BACKEND = Backend\OpenSSL::class; |
||
| 22 | |||
| 23 | |||
| 24 | /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend */ |
||
| 25 | protected SignatureBackend $backend; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Build a signature algorithm. |
||
| 30 | * |
||
| 31 | * Extend this class to implement your own signers. |
||
| 32 | * |
||
| 33 | * WARNING: remember to adjust the type of the key to the one that works with your algorithm! |
||
| 34 | * |
||
| 35 | * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The signing key. |
||
| 36 | * @param string $algId The identifier of this algorithm. |
||
| 37 | * @param string $digest The identifier of the digest algorithm to use. |
||
| 38 | */ |
||
| 39 | public function __construct( |
||
| 40 | #[\SensitiveParameter] |
||
| 41 | private KeyInterface $key, |
||
| 42 | protected string $algId, |
||
| 43 | protected string $digest, |
||
| 44 | ) { |
||
| 45 | Assert::oneOf( |
||
| 46 | $algId, |
||
| 47 | static::getSupportedAlgorithms(), |
||
| 48 | sprintf('Unsupported algorithm for %s', static::class), |
||
| 49 | UnsupportedAlgorithmException::class, |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend $backend */ |
||
| 53 | $backend = new (static::DEFAULT_BACKEND)(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | $this->setBackend($backend); |
||
| 55 | $this->backend->setDigestAlg($digest); |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function getAlgorithmId(): string |
||
| 63 | { |
||
| 64 | return $this->algId; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getDigest(): string |
||
| 72 | { |
||
| 73 | return $this->digest; |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @return \SimpleSAML\XMLSecurity\Key\KeyInterface |
||
| 79 | */ |
||
| 80 | public function getKey(): KeyInterface |
||
| 81 | { |
||
| 82 | return $this->key; |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | public function setBackend(?SignatureBackend $backend): void |
||
| 90 | { |
||
| 91 | if ($backend === null) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->backend = $backend; |
||
| 96 | $this->backend->setDigestAlg($this->digest); |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Sign a given plaintext with the current algorithm and key. |
||
| 102 | * |
||
| 103 | * @param string $plaintext The plaintext to sign. |
||
| 104 | * |
||
| 105 | * @return string The (binary) signature corresponding to the given plaintext. |
||
| 106 | */ |
||
| 107 | final public function sign(string $plaintext): string |
||
| 108 | { |
||
| 109 | return $this->backend->sign($this->key, $plaintext); |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Verify a signature with the current algorithm and key. |
||
| 115 | * |
||
| 116 | * @param string $plaintext The original signed text. |
||
| 117 | * @param string $signature The (binary) signature to verify. |
||
| 118 | * |
||
| 119 | * @return boolean True if the signature can be verified, false otherwise. |
||
| 120 | */ |
||
| 121 | final public function verify(string $plaintext, string $signature): bool |
||
| 122 | { |
||
| 123 | return $this->backend->verify($this->key, $plaintext, $signature); |
||
| 124 | } |
||
| 125 | } |
||
| 126 |