Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cookiescrypter.php
1<?php
2
3namespace Bitrix\Main\Web;
4
9
10final class CookiesCrypter
11{
12 public const COOKIE_MAX_SIZE = 4096;
14
15 private const SIGN_PREFIX = '-crpt-';
16 private const CIPHER_KEY_SUFFIX = 'cookiecrypter';
17
19 protected $cipherKey;
21 protected $cipher;
22
23 public function __construct()
24 {}
25
26 protected function buildCipher(): self
27 {
28 if ($this->cipher)
29 {
30 return $this;
31 }
32
33 $configuration = Config\Configuration::getInstance();
34
35 $this->cipher = new Cipher();
36 $this->cipherKey = $configuration->get('crypto')['crypto_key'] ?? null;
37 if (!$this->cipherKey)
38 {
39 throw new SystemException('There is no crypto[crypto_key] in .settings.php. Generate it.');
40 }
41 $this->cipherKey = $this->prependSuffixToKey($this->cipherKey);
42
43 return $this;
44 }
45
46 protected function prependSuffixToKey(string $key): string
47 {
48 return $key . self::CIPHER_KEY_SUFFIX;
49 }
50
55 public function encrypt(CryptoCookie $cookie): iterable
56 {
57 $result = [];
58 $encryptedValue = $this->encryptValue($cookie->getValue());
59 foreach ($this->packCookie($cookie, $encryptedValue) as $partCookie)
60 {
61 $result[] = $partCookie;
62 }
63
64 return $result;
65 }
66
67 public function decrypt(string $name, string $value, iterable $cookies): string
68 {
69 if (!$this->shouldDecrypt($name, $value))
70 {
71 return $value;
72 }
73
74 try
75 {
76 return $this->unpackCookie($value, $cookies);
77 }
78 catch (SecurityException $e)
79 {
80 //just skip cookies which we can't decrypt.
81 }
82
83 return '';
84 }
85
91 protected function packCookie(CryptoCookie $cookie, string $encryptedValue): iterable
92 {
93 $length = strlen($encryptedValue);
94 $maxContentLength = static::COOKIE_MAX_SIZE - static::COOKIE_RESERVED_SUFFIX_BYTES - strlen($cookie->getName());
95
96 $i = 0;
97 $parts = ($length / $maxContentLength);
98 $pack = [];
99 do
100 {
101 $startPosition = $i * $maxContentLength;
102 $partCookie = new Cookie("{$cookie->getName()}_{$i}", substr($encryptedValue, $startPosition, $maxContentLength));
103 $cookie->copyAttributesTo($partCookie);
104 $pack["{$cookie->getOriginalName()}_{$i}"] = $partCookie;
105
106 $i++;
107 }
108 while($parts > $i);
109
110 $mainCookie = new Cookie($cookie->getName(), $this->prependSign(implode(',', array_keys($pack))));
111 $cookie->copyAttributesTo($mainCookie);
112
113 array_unshift($pack, $mainCookie);
114
115 return $pack;
116 }
117
118 protected function unpackCookie(string $mainCookie, iterable $cookies): string
119 {
120 $mainCookie = $this->removeSign($mainCookie);
121 $packedNames = array_flip(array_filter(explode(',', $mainCookie)));
122 $parts = [];
123
124 foreach ($cookies as $name => $value)
125 {
126 if (!isset($packedNames[$name]))
127 {
128 continue;
129 }
130
131 $parts[$packedNames[$name]] = $value;
132 if (count($parts) === count($packedNames))
133 {
134 break;
135 }
136 }
137 ksort($parts);
138 $encryptedValue = implode('', $parts);
139
140 return $this->decryptValue($encryptedValue);
141 }
142
143 protected function encryptValue(string $value): string
144 {
145 $this->buildCipher();
146 if (function_exists('gzencode'))
147 {
148 $value = gzencode($value);
149 }
150
151 return $this->encodeUrlSafeB64($this->cipher->encrypt($value, $this->getCipherKey()));
152 }
153
154 protected function decryptValue(string $value): string
155 {
156 $this->buildCipher();
157
158 $value = $this->cipher->decrypt($this->decodeUrlSafeB64($value), $this->getCipherKey());
159 if (function_exists('gzdecode'))
160 {
161 $value = gzdecode($value);
162 }
163
164 return $value;
165 }
166
167 private function decodeUrlSafeB64($input)
168 {
169 $padLength = 4 - strlen($input) % 4;
170 $input .= str_repeat('=', $padLength);
171
172 return base64_decode(strtr($input, '-_', '+/'));
173 }
174
175 private function encodeUrlSafeB64($input)
176 {
177 return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
178 }
179
180 public function shouldEncrypt(Cookie $cookie): bool
181 {
182 return $cookie instanceof CryptoCookie;
183 }
184
185 public function shouldDecrypt(string $cookieName, string $cookieValue): bool
186 {
187 return strpos($cookieValue, self::SIGN_PREFIX) === 0;
188 }
189
190 protected function prependSign(string $value): string
191 {
192 return self::SIGN_PREFIX . $value;
193 }
194
195 protected function removeSign(string $value): string
196 {
197 return substr($value, strlen(self::SIGN_PREFIX));
198 }
199
200 public function getCipherKey(): string
201 {
202 return $this->cipherKey;
203 }
204}
packCookie(CryptoCookie $cookie, string $encryptedValue)
shouldDecrypt(string $cookieName, string $cookieValue)
encrypt(CryptoCookie $cookie)
decrypt(string $name, string $value, iterable $cookies)
unpackCookie(string $mainCookie, iterable $cookies)