Skip to content

Commit 633f23d

Browse files
committed
Migrate to value types
1 parent feb95e1 commit 633f23d

File tree

220 files changed

+3373
-2349
lines changed

Some content is hidden

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

220 files changed

+3373
-2349
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@
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 validDigestValue(mixed $value, string $message = '', string $exception = '')
16+
* @method static void validECPoint(mixed $value, string $message = '', string $exception = '')
17+
* @method static void validHMACOutputLength(mixed $value, string $message = '', string $exception = '')
18+
* @method static void validKeySize(mixed $value, string $message = '', string $exception = '')
19+
* @method static void nullOrValidCryptoBinary(mixed $value, string $message = '', string $exception = '')
20+
* @method static void nullOrValidDigestValue(mixed $value, string $message = '', string $exception = '')
21+
* @method static void nullOrValidECPoint(mixed $value, string $message = '', string $exception = '')
22+
* @method static void nullOrValidHMACOutputLength(mixed $value, string $message = '', string $exception = '')
23+
* @method static void nullOrValidKeySize(mixed $value, string $message = '', string $exception = '')
24+
* @method static void allValidCryptoBinary(mixed $value, string $message = '', string $exception = '')
25+
* @method static void allValidDigestValue(mixed $value, string $message = '', string $exception = '')
26+
* @method static void allValidECPoint(mixed $value, string $message = '', string $exception = '')
27+
* @method static void allValidHMACOutputLength(mixed $value, string $message = '', string $exception = '')
28+
* @method static void allValidKeyValue(mixed $value, string $message = '', string $exception = '')
1329
*/
1430
class Assert extends BaseAssert
1531
{
32+
use CryptoBinaryTrait;
33+
use DigestValueTrait;
34+
use ECPointTrait;
35+
use HMACOutputLengthTrait;
36+
use KeySizeTrait;
1637
}

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/DigestValueTrait.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 DigestValueTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validDigestValue(string $value, string $message = ''): void
19+
{
20+
parent::validBase64Binary(
21+
$value,
22+
$message ?: '%s is not a valid xs:DigestValue',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

src/Assert/ECPointTrait.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 ECPointTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validECPoint(string $value, string $message = ''): void
19+
{
20+
Assert::validCryptoBinary(
21+
$value,
22+
$message ?: '%s is not a valid dsig11:ECPointType',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

src/Assert/HMACOutputLengthTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 HMACOutputLengthTrait
13+
{
14+
/**
15+
* The HMAC algorithm (RFC2104 [HMAC]) takes the output (truncation) length in bits as a parameter;
16+
* this specification REQUIRES that the truncation length be a multiple of 8 (i.e. fall on a byte boundary)
17+
* because Base64 encoding operates on full bytes
18+
*
19+
* @var string
20+
*/
21+
private static string $HMACOutputLength_regex = '/^([1-9]\d*)$/D';
22+
23+
24+
/**
25+
* @param string $value
26+
* @param string $message
27+
*/
28+
protected static function validHMACOutputLength(string $value, string $message = ''): void
29+
{
30+
parent::regex(
31+
$value,
32+
self::$HMACOutputLength_regex,
33+
$message ?: '%s is not a valid ds:HMACOutputLengthType',
34+
InvalidArgumentException::class,
35+
);
36+
}
37+
}

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+
}

0 commit comments

Comments
 (0)