Skip to content

Commit dc430d7

Browse files
authored
Merge pull request #2 from simplesamlphp/signedelts
Rewrite signed elements structure
2 parents 2a51dcb + 32ac524 commit dc430d7

File tree

73 files changed

+2365
-719
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2365
-719
lines changed

bin/generate_CustomSignable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require_once('../vendor/autoload.php');
4+
5+
use SimpleSAML\XML\DOMDocumentFactory;
6+
use SimpleSAML\XMLSecurity\Test\XML\CustomSignable;
7+
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
8+
use SimpleSAML\XMLSecurity\XMLSecurityKey;
9+
10+
$chunk = DOMDocumentFactory::fromString('<ssp:Some>Chunk</ssp:Some>')->documentElement;
11+
$signable = new CustomSignable($chunk);
12+
13+
$privateKey = PEMCertificatesMock::getPrivateKey(XMLSecurityKey::RSA_SHA256, PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY);
14+
$x = $signable->sign($privateKey);
15+
echo $x;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
"robrichards/xmlseclibs": "^3.1.1",
4242
"simplesamlphp/assert": "~0.2.6",
43-
"simplesamlphp/xml-common": "^0.7.1"
43+
"simplesamlphp/xml-common": "^0.8.0"
4444
},
4545
"require-dev": {
4646
"simplesamlphp/simplesamlphp-test-framework": "^1.0.5"

src/Alg/Signature/AbstractSigner.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SimpleSAML\XMLSecurity\Alg\Signature;
46

7+
use SimpleSAML\Assert\Assert;
58
use SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm;
69
use SimpleSAML\XMLSecurity\Backend\SignatureBackend;
10+
use SimpleSAML\XMLSecurity\Exception\RuntimeException;
711
use SimpleSAML\XMLSecurity\Key\AbstractKey;
812

913
/**
1014
* An abstract class that implements a generic digital signature algorithm.
1115
*
12-
* @package SimpleSAML\XMLSecurity\\Alg\Signature
16+
* @package simplesamlphp/xml-security
1317
*/
1418
abstract class AbstractSigner implements SignatureAlgorithm
1519
{
1620
/** @var \SimpleSAML\XMLSecurity\Key\AbstractKey */
17-
protected AbstractKey $key;
21+
private AbstractKey $key;
1822

1923
/** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend */
2024
protected SignatureBackend $backend;
@@ -25,22 +29,46 @@ abstract class AbstractSigner implements SignatureAlgorithm
2529
/** @var string */
2630
protected string $digest;
2731

32+
/** @var string */
33+
protected string $algId;
34+
2835

2936
/**
3037
* Build a signature algorithm.
3138
*
39+
* Extend this class to implement your own signers.
40+
*
41+
* WARNING: remember to adjust the type of the key to the one that works with your algorithm!
42+
*
3243
* @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The signing key.
44+
* @param string $algId The identifier of this algorithm.
3345
* @param string $digest The identifier of the digest algorithm to use.
3446
*/
35-
public function __construct(AbstractKey $key, string $digest)
47+
public function __construct(AbstractKey $key, string $algId, string $digest)
3648
{
49+
Assert::oneOf(
50+
$algId,
51+
static::getSupportedAlgorithms(),
52+
'Unsupported algorithm for ' . static::class,
53+
RuntimeException::class
54+
);
3755
$this->key = $key;
56+
$this->algId = $algId;
3857
$this->digest = $digest;
3958
$this->backend = new $this->default_backend();
4059
$this->backend->setDigestAlg($digest);
4160
}
4261

4362

63+
/**
64+
* @return string
65+
*/
66+
public function getAlgorithmId(): string
67+
{
68+
return $this->algId;
69+
}
70+
71+
4472
/**
4573
* @return string
4674
*/
@@ -50,6 +78,15 @@ public function getDigest(): string
5078
}
5179

5280

81+
/**
82+
* @return AbstractKey
83+
*/
84+
public function getKey(): AbstractKey
85+
{
86+
return $this->key;
87+
}
88+
89+
5390
/**
5491
* @param \SimpleSAML\XMLSecurity\Backend\SignatureBackend $backend
5592
*/
@@ -67,7 +104,7 @@ public function setBackend(SignatureBackend $backend): void
67104
*
68105
* @return string The (binary) signature corresponding to the given plaintext.
69106
*/
70-
public function sign(string $plaintext): string
107+
final public function sign(string $plaintext): string
71108
{
72109
return $this->backend->sign($this->key, $plaintext);
73110
}
@@ -81,7 +118,7 @@ public function sign(string $plaintext): string
81118
*
82119
* @return boolean True if the signature can be verified, false otherwise.
83120
*/
84-
public function verify(string $plaintext, string $signature): bool
121+
final public function verify(string $plaintext, string $signature): bool
85122
{
86123
return $this->backend->verify($this->key, $plaintext, $signature);
87124
}

src/Alg/Signature/HMAC.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SimpleSAML\XMLSecurity\Alg\Signature;
46

57
use SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm;
68
use SimpleSAML\XMLSecurity\Backend\HMAC as HMAC_Backend;
7-
use SimpleSAML\XMLSecurity\Constants;
9+
use SimpleSAML\XMLSecurity\Constants as C;
810
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
911

1012
/**
1113
* Class implementing the HMAC signature algorithm
1214
*
13-
* @package SimpleSAML\XMLSecurity\Alg\Signature
15+
* @package simplesamlphp/xml-security
1416
*/
15-
class HMAC extends AbstractSigner implements SignatureAlgorithm
17+
final class HMAC extends AbstractSigner implements SignatureAlgorithm
1618
{
1719
/** @var string */
1820
protected string $default_backend = HMAC_Backend::class;
@@ -22,10 +24,19 @@ class HMAC extends AbstractSigner implements SignatureAlgorithm
2224
* HMAC constructor.
2325
*
2426
* @param \SimpleSAML\XMLSecurity\Key\SymmetricKey $key The symmetric key to use.
25-
* @param string $digest The identifier of the digest algorithm to use.
27+
* @param string $algId The identifier of this algorithm.
28+
*/
29+
public function __construct(SymmetricKey $key, string $algId = C::SIG_HMAC_SHA256)
30+
{
31+
parent::__construct($key, $algId, C::$HMAC_DIGESTS[$algId]);
32+
}
33+
34+
35+
/**
36+
* @inheritDoc
2637
*/
27-
public function __construct(SymmetricKey $key, string $digest = Constants::DIGEST_SHA1)
38+
public static function getSupportedAlgorithms(): array
2839
{
29-
parent::__construct($key, $digest);
40+
return array_keys(C::$HMAC_DIGESTS);
3041
}
3142
}

src/Alg/Signature/RSA.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SimpleSAML\XMLSecurity\Alg\Signature;
46

57
use SimpleSAML\XMLSecurity\Alg\SignatureAlgorithm;
68
use SimpleSAML\XMLSecurity\Backend\OpenSSL;
7-
use SimpleSAML\XMLSecurity\Constants;
9+
use SimpleSAML\XMLSecurity\Constants as C;
810
use SimpleSAML\XMLSecurity\Key\AsymmetricKey;
911

1012
/**
1113
* Class implementing the RSA signature algorithm.
1214
*
13-
* @package SimpleSAML\XMLSecurity\Alg\Signature
15+
* @package simplesamlphp/xml-security
1416
*/
15-
class RSA extends AbstractSigner implements SignatureAlgorithm
17+
final class RSA extends AbstractSigner implements SignatureAlgorithm
1618
{
1719
/** @var string */
1820
protected string $default_backend = OpenSSL::class;
@@ -22,10 +24,19 @@ class RSA extends AbstractSigner implements SignatureAlgorithm
2224
* RSA constructor.
2325
*
2426
* @param \SimpleSAML\XMLSecurity\Key\AsymmetricKey $key The asymmetric key (either public or private) to use.
25-
* @param string $digest The identifier of the digest algorithm to use.
27+
* @param string $algId The identifier of this algorithm.
28+
*/
29+
public function __construct(AsymmetricKey $key, string $algId = C::SIG_RSA_SHA256)
30+
{
31+
parent::__construct($key, $algId, C::$RSA_DIGESTS[$algId]);
32+
}
33+
34+
35+
/**
36+
* @inheritDoc
2637
*/
27-
public function __construct(AsymmetricKey $key, string $digest = Constants::DIGEST_SHA1)
38+
public static function getSupportedAlgorithms(): array
2839
{
29-
parent::__construct($key, $digest);
40+
return array_keys(C::$RSA_DIGESTS);
3041
}
3142
}

0 commit comments

Comments
 (0)