PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / Ramsey / Uuid / Generator / DceSecurityGenerator.php

DceSecurityGenerator.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at lib/packages/Ramsey/Uuid/Generator/DceSecurityGenerator.php

134 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace WP_DEFENDER_VENDOR\Ramsey\Uuid\Generator;
16
17 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Converter\NumberConverterInterface;
18 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Exception\DceSecurityException;
19 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Provider\DceSecurityProviderInterface;
20 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Type\Hexadecimal;
21 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Type\Integer as IntegerObject;
22 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Uuid;
23
24 use function hex2bin;
25 use function in_array;
26 use function pack;
27 use function str_pad;
28 use function strlen;
29 use function substr_replace;
30
31 use const STR_PAD_LEFT;
32
33 /**
34 * DceSecurityGenerator generates strings of binary data based on a local domain, local identifier, node ID, clock
35 * sequence, and the current time
36 */
37 class DceSecurityGenerator implements DceSecurityGeneratorInterface
38 {
39 private const DOMAINS = [
40 Uuid::DCE_DOMAIN_PERSON,
41 Uuid::DCE_DOMAIN_GROUP,
42 Uuid::DCE_DOMAIN_ORG,
43 ];
44
45 /**
46 * Upper bounds for the clock sequence in DCE Security UUIDs.
47 */
48 private const CLOCK_SEQ_HIGH = 63;
49
50 /**
51 * Lower bounds for the clock sequence in DCE Security UUIDs.
52 */
53 private const CLOCK_SEQ_LOW = 0;
54
55 public function __construct(
56 private NumberConverterInterface $numberConverter,
57 private TimeGeneratorInterface $timeGenerator,
58 private DceSecurityProviderInterface $dceSecurityProvider,
59 ) {
60 }
61
62 public function generate(
63 int $localDomain,
64 ?IntegerObject $localIdentifier = null,
65 ?Hexadecimal $node = null,
66 ?int $clockSeq = null,
67 ): string {
68 if (!in_array($localDomain, self::DOMAINS)) {
69 throw new DceSecurityException('Local domain must be a valid DCE Security domain');
70 }
71
72 if ($localIdentifier && $localIdentifier->isNegative()) {
73 throw new DceSecurityException(
74 'Local identifier out of bounds; it must be a value between 0 and 4294967295',
75 );
76 }
77
78 if ($clockSeq > self::CLOCK_SEQ_HIGH || $clockSeq < self::CLOCK_SEQ_LOW) {
79 throw new DceSecurityException('Clock sequence out of bounds; it must be a value between 0 and 63');
80 }
81
82 switch ($localDomain) {
83 case Uuid::DCE_DOMAIN_ORG:
84 if ($localIdentifier === null) {
85 throw new DceSecurityException('A local identifier must be provided for the org domain');
86 }
87
88 break;
89 case Uuid::DCE_DOMAIN_PERSON:
90 if ($localIdentifier === null) {
91 $localIdentifier = $this->dceSecurityProvider->getUid();
92 }
93
94 break;
95 case Uuid::DCE_DOMAIN_GROUP:
96 default:
97 if ($localIdentifier === null) {
98 $localIdentifier = $this->dceSecurityProvider->getGid();
99 }
100
101 break;
102 }
103
104 $identifierHex = $this->numberConverter->toHex($localIdentifier->toString());
105
106 // The maximum value for the local identifier is 0xffffffff, or 4,294,967,295. This is 8 hexadecimal digits, so
107 // if the length of hexadecimal digits is greater than 8, we know the value is greater than 0xffffffff.
108 if (strlen($identifierHex) > 8) {
109 throw new DceSecurityException(
110 'Local identifier out of bounds; it must be a value between 0 and 4294967295',
111 );
112 }
113
114 $domainByte = pack('n', $localDomain)[1];
115 $identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT));
116
117 if ($node instanceof Hexadecimal) {
118 $node = $node->toString();
119 }
120
121 // Shift the clock sequence 8 bits to the left, so it matches 0x3f00.
122 if ($clockSeq !== null) {
123 $clockSeq = $clockSeq << 8;
124 }
125
126 $bytes = $this->timeGenerator->generate($node, $clockSeq);
127
128 // Replace bytes in the time-based UUID with DCE Security values.
129 $bytes = substr_replace($bytes, $identifierBytes, 0, 4);
130
131 return substr_replace($bytes, $domainByte, 9, 1);
132 }
133 }
134