Skip to content

Commit 9e83fa1

Browse files
committed
Migrate to value types
1 parent feb95e1 commit 9e83fa1

File tree

206 files changed

+2857
-2323
lines changed

Some content is hidden

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

206 files changed

+2857
-2323
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"ext-spl": "*",
4545

4646
"simplesamlphp/assert": "~1.8.0",
47-
"simplesamlphp/xml-common": "~1.24.0"
47+
"simplesamlphp/xml-common": "dev-feature/xsd-types"
4848
},
4949
"require-dev": {
5050
"simplesamlphp/simplesamlphp-test-framework": "~1.8.0"

src/Assert/Assert.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
* SimpleSAML\XMLSecurity\Assert\Assert wrapper class
1111
*
1212
* @package simplesamlphp/xml-security
13+
*
14+
* @method static void validCryptoBinary(mixed $value, string $message = '', string $exception = '')
15+
* @method static void validKeySize(mixed $value, string $message = '', string $exception = '')
16+
* @method static void nullOrValidCryptoBinary(mixed $value, string $message = '', string $exception = '')
17+
* @method static void nullOrValidKeySize(mixed $value, string $message = '', string $exception = '')
18+
* @method static void allValidCryptoBinary(mixed $value, string $message = '', string $exception = '')
19+
* @method static void allValidKeyValue(mixed $value, string $message = '', string $exception = '')
1320
*/
1421
class Assert extends BaseAssert
1522
{
23+
use CryptoBinaryTrait;
24+
use KeySizeTrait;
1625
}

src/Assert/CryptoBinaryTrait.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-security
11+
*/
12+
trait CryptoBinaryTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validCryptoBinary(string $value, string $message = ''): void
19+
{
20+
parent::validBase64Binary(
21+
$value,
22+
$message ?: '%s is not a valid xs:cryptoBinary',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

src/Assert/KeySizeTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-security
11+
*/
12+
trait KeySizeTrait
13+
{
14+
/**
15+
* The size in bits of the key to be derived from the shared secret as the UTF-8 string for the corresponding
16+
* decimal integer with only digits in the string and no leading zeros.
17+
*
18+
* @var string
19+
*/
20+
private static string $keySize_regex = '/^([1-9]\d+)$/D';
21+
22+
23+
/**
24+
* @param string $value
25+
* @param string $message
26+
*/
27+
protected static function validKeySize(string $value, string $message = ''): void
28+
{
29+
parent::regex(
30+
$value,
31+
self::$keySize_regex,
32+
$message ?: '%s is not a valid xenc:keySizeType',
33+
InvalidArgumentException::class,
34+
);
35+
}
36+
}

src/Constants.php

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,52 @@
1212
class Constants extends \SimpleSAML\XML\Constants
1313
{
1414
/**
15-
* Digest algorithms
15+
* Symmetric key wrap algorithms
16+
*/
17+
public const KEY_WRAP_3DES = 'http://www.w3.org/2001/04/xmlenc#kw-tripledes';
18+
public const KEY_WRAP_AES128 = 'http://www.w3.org/2001/04/xmlenc#kw-aes128';
19+
public const KEY_WRAP_AES192 = 'http://www.w3.org/2001/04/xmlenc#kw-aes192';
20+
public const KEY_WRAP_AES256 = 'http://www.w3.org/2001/04/xmlenc#kw-aes256';
21+
22+
/** @var string[] */
23+
public static array $KEY_WRAP_ALGORITHMS = [
24+
self::KEY_WRAP_3DES,
25+
self::KEY_WRAP_AES128,
26+
self::KEY_WRAP_AES192,
27+
self::KEY_WRAP_AES256,
28+
];
29+
30+
31+
/**
32+
* Key derivation algorithms
33+
*/
34+
public const KEY_DERIVATION_CONCATKDF = 'http://www.w3.org/2009/xmlenc11#ConcatKDF';
35+
public const KEY_DERIVATION_PBKDF2 = 'http://www.w3.org/2009/xmlenc11#pbkdf2';
36+
37+
/** @var string[] */
38+
public static array $KEY_DERIVATION_ALGORITHMS = [
39+
self::KEY_DERIVATION_CONCATKDF,
40+
self::KEY_DERIVATION_PBKDF2,
41+
];
42+
43+
44+
/**
45+
* Key agreement algorithms
46+
*/
47+
public const KEY_AGREEMENT_ECDH_ES = 'http://www.w3.org/2009/xmlenc11#ECDH-ES';
48+
public const KEY_AGREEMENT_DH = 'http://www.w3.org/2001/04/xmlenc#dh';
49+
public const KEY_AGREEMENT_DH_ES = 'http://www.w3.org/2009/xmlenc11#dh-es';
50+
51+
/** @var string[] */
52+
public static array $KEY_AGREEMENT_ALGORITHMS = [
53+
self::KEY_AGREEMENT_ECDH_ES,
54+
self::KEY_AGREEMENT_DH,
55+
self::KEY_AGREEMENT_DH_ES,
56+
];
57+
58+
59+
/**
60+
* Message digest algorithms
1661
*/
1762
public const DIGEST_SHA1 = 'http://www.w3.org/2000/09/xmldsig#sha1';
1863
public const DIGEST_SHA224 = 'http://www.w3.org/2001/04/xmldsig-more#sha224';
@@ -31,12 +76,14 @@ class Constants extends \SimpleSAML\XML\Constants
3176
self::DIGEST_RIPEMD160 => 'ripemd160',
3277
];
3378

79+
3480
/**
3581
* Padding schemas
3682
*/
3783
public const PADDING_PKCS1 = "PKCS1";
3884
public const PADDING_PKCS1_OAEP = "OAEP";
3985

86+
4087
/**
4188
* Block encryption algorithms
4289
*/
@@ -81,6 +128,7 @@ class Constants extends \SimpleSAML\XML\Constants
81128
self::BLOCK_ENC_AES256_GCM => 32,
82129
];
83130

131+
84132
/**
85133
* Key transport algorithms
86134
*/
@@ -95,13 +143,27 @@ class Constants extends \SimpleSAML\XML\Constants
95143
self::KEY_TRANSPORT_OAEP_MGF1P,
96144
];
97145

146+
98147
/**
99148
* Canonicalization algorithms
100149
*/
101150
public const C14N_INCLUSIVE_WITH_COMMENTS = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments';
102151
public const C14N_INCLUSIVE_WITHOUT_COMMENTS = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315';
103152
public const C14N_EXCLUSIVE_WITH_COMMENTS = 'http://www.w3.org/2001/10/xml-exc-c14n#WithComments';
104153
public const C14N_EXCLUSIVE_WITHOUT_COMMENTS = 'http://www.w3.org/2001/10/xml-exc-c14n#';
154+
public const C14N11_INCLUSIVE_WITH_COMMENTS = 'http://www.w3.org/2006/12/xml-c14n11';
155+
public const C14N11_INCLUSIVE_WITHOUT_COMMENTS = 'http://www.w3.org/2006/12/xml-c14n11#WithComments';
156+
157+
/** @var string[] */
158+
public static array $CANONICALIZATION_ALGORITHMS = [
159+
self::C14N_INCLUSIVE_WITH_COMMENTS,
160+
self::C14N_INCLUSIVE_WITHOUT_COMMENTS,
161+
self::C14N_EXCLUSIVE_WITH_COMMENTS,
162+
self::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
163+
self::C14N11_INCLUSIVE_WITH_COMMENTS,
164+
self::C14N11_INCLUSIVE_WITHOUT_COMMENTS,
165+
];
166+
105167

106168
/**
107169
* Signature algorithms
@@ -139,6 +201,19 @@ class Constants extends \SimpleSAML\XML\Constants
139201
self::SIG_HMAC_RIPEMD160 => self::DIGEST_RIPEMD160,
140202
];
141203

204+
205+
/**
206+
* Encoding algorithms
207+
*/
208+
public const ENCODING_BASE64 = 'http://www.w3.org/2000/09/xmldsig#base64';
209+
210+
211+
/**
212+
* Transforms algorithms
213+
*/
214+
public const TRANSFORMS_BASE64 = 'http://www.w3.org/2000/09/xmldsig#base64';
215+
216+
142217
/**
143218
* XML & XPath namespaces and identifiers
144219
*/
@@ -153,7 +228,4 @@ class Constants extends \SimpleSAML\XML\Constants
153228
public const XMLENC_ELEMENT = 'http://www.w3.org/2001/04/xmlenc#Element';
154229
public const XMLENC_ENCRYPTEDKEY = 'http://www.w3.org/2001/04/xmlenc#EncryptedKey';
155230
public const XMLENC_EXI = 'http://www.w3.org/2009/xmlenc11#EXI';
156-
157-
// The namespace for the Elliptic Curve Diffie-Hellman Ephemeral Static (ECDH-ES) algorithm
158-
public const XMLENC11_ECDH_ES = 'http://www.w3.org/2009/xmlenc11#ECDH-ES';
159231
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Exception;
6+
7+
/**
8+
* This exception may be raised when a violation of the xmldsig specification is detected
9+
*
10+
* @package simplesamlphp/xml-security
11+
*/
12+
class ProtocolViolationException extends RuntimeException
13+
{
14+
/**
15+
* @param string|null $message
16+
*/
17+
public function __construct(?string $message = null)
18+
{
19+
if ($message === null) {
20+
if (defined('static::DEFAULT_MESSAGE')) {
21+
$message = static::DEFAULT_MESSAGE;
22+
} else {
23+
$message = 'A violation of the XML Signature Syntax and Processing specification occurred.';
24+
}
25+
}
26+
27+
parent::__construct($message);
28+
}
29+
}

src/TestUtils/SignedElementTestTrait.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\XMLSecurity\TestUtils;
66

77
use DOMDocument;
8+
use SimpleSAML\XML\Type\Base64BinaryValue;
89
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
910
use SimpleSAML\XMLSecurity\Constants as C;
1011
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
@@ -81,12 +82,20 @@ public function testSignatures(): void
8182
);
8283

8384
$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-
)]),
85+
new X509Data([
86+
new X509Certificate(
87+
Base64BinaryValue::fromString(
88+
PEMCertificatesMock::getPlainPublicKeyContents(PEMCertificatesMock::PUBLIC_KEY)
89+
),
90+
),
91+
]),
92+
new X509Data([
93+
new X509Certificate(
94+
Base64BinaryValue::fromString(
95+
PEMCertificatesMock::getPlainPublicKeyContents(PEMCertificatesMock::OTHER_PUBLIC_KEY),
96+
),
97+
),
98+
]),
9099
]);
91100

92101
$unsigned = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
@@ -99,7 +108,7 @@ public function testSignatures(): void
99108

100109
// verify signature
101110
$verifier = (new SignatureAlgorithmFactory([]))->getAlgorithm(
102-
$signed->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(),
111+
$signed->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm()->getValue(),
103112
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY),
104113
);
105114

src/Type/CryptoBinaryValue.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Type;
6+
7+
use SimpleSAML\XML\Exception\SchemaViolationException;
8+
use SimpleSAML\XML\Type\Base64BinaryValue;
9+
use SimpleSAML\XMLSecurity\Assert\Assert;
10+
11+
/**
12+
* @package simplesaml/xml-security
13+
*/
14+
class CryptoBinaryValue extends Base64BinaryValue
15+
{
16+
/**
17+
* Validate the value.
18+
*
19+
* @param string $value
20+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
21+
* @return void
22+
*/
23+
protected function validateValue(string $value): void
24+
{
25+
// Note: value must already be sanitized before validating
26+
Assert::validCryptoBinary($this->sanitizeValue($value), SchemaViolationException::class);
27+
}
28+
}

src/Type/KeySizeValue.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Type;
6+
7+
use SimpleSAML\XML\Exception\SchemaViolationException;
8+
use SimpleSAML\XML\Type\IntegerValue;
9+
use SimpleSAML\XMLSecurity\Assert\Assert;
10+
11+
/**
12+
* @package simplesaml/xml-security
13+
*/
14+
class KeySizeValue extends IntegerValue
15+
{
16+
/**
17+
* Validate the value.
18+
*
19+
* @param string $value
20+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
21+
* @return void
22+
*/
23+
protected function validateValue(string $value): void
24+
{
25+
// Note: value must already be sanitized before validating
26+
Assert::validKeySize($this->sanitizeValue($value), SchemaViolationException::class);
27+
}
28+
}

src/Utils/XML.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function processTransforms(
8989
$arXPath = null;
9090
$prefixList = null;
9191
foreach ($transforms->getTransform() as $transform) {
92-
$canonicalMethod = $transform->getAlgorithm();
92+
$canonicalMethod = $transform->getAlgorithm()->getValue();
9393
switch ($canonicalMethod) {
9494
case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
9595
case C::C14N_EXCLUSIVE_WITH_COMMENTS:

0 commit comments

Comments
 (0)