1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\WebAuthn;
use Webmozart\Assert\Assert;
use function ord;
use function unpack;
use const INF;
use const NAN;
/**
* Concise Binary Object Representation (CBOR) decoder.
*
* This is not a general purpose CBOR decoder and only implements the CTAP2 canonical CBOR encoding form.
*
* @see https://www.rfc-editor.org/rfc/rfc7049
* @see https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#message-encoding
*/
final class CBORDecoder
{
/**
* @return mixed
*
* @throws WebAuthnException
*/
public function decode(DataStream $stream)
{
return $this->wellFormed($stream);
}
/**
* @see https://www.rfc-editor.org/rfc/rfc7049#appendix-C
*
* @return mixed
*
* @throws WebAuthnException
*/
private function wellFormed(DataStream $stream)
{
// process initial bytes
$initialByte = ord($stream->take(1));
$majorType = $initialByte >> 5;
$value = $additionalInformation = $initialByte & 0x1f;
switch ($additionalInformation) {
case 24:
if ($majorType !== 7) {
$value = ord($stream->take(1));
}
break;
case 25:
if ($majorType !== 7) {
$unpackedValue = unpack('n', $stream->take(2));
Assert::isArray($unpackedValue);
Assert::keyExists($unpackedValue, 1);
Assert::integer($unpackedValue[1]);
$value = $unpackedValue[1];
}
break;
case 26:
if ($majorType !== 7) {
$unpackedValue = unpack('N', $stream->take(4));
Assert::isArray($unpackedValue);
Assert::keyExists($unpackedValue, 1);
Assert::integer($unpackedValue[1]);
$value = $unpackedValue[1];
}
break;
case 27:
if ($majorType !== 7) {
$unpackedValue = unpack('J', $stream->take(8));
Assert::isArray($unpackedValue);
Assert::keyExists($unpackedValue, 1);
Assert::integer($unpackedValue[1]);
$value = $unpackedValue[1];
}
break;
case 28:
case 29:
case 30:
case 31:
throw new WebAuthnException();
}
// process content
switch ($majorType) {
case 0:
return $this->getUnsignedInteger($value);
case 1:
return $this->getNegativeInteger($value);
case 2:
return $this->getByteString($stream, $value);
case 3:
return $this->getTextString($stream, $value);
case 4:
return $this->getList($stream, $value);
case 5:
return $this->getMap($stream, $value);
case 6:
return $this->getTag($stream);
case 7:
return $this->getFloatNumberOrSimpleValue($stream, $value, $additionalInformation);
default:
throw new WebAuthnException();
}
}
private function getUnsignedInteger(int $value): int
{
return $value;
}
private function getNegativeInteger(int $value): int
{
return -1 - $value;
}
/**
* @throws WebAuthnException
*/
private function getByteString(DataStream $stream, int $value): string
{
return $stream->take($value);
}
/**
* @throws WebAuthnException
*/
private function getTextString(DataStream $stream, int $value): string
{
return $stream->take($value);
}
/**
* @psalm-return list<mixed>
*
* @throws WebAuthnException
*/
private function getList(DataStream $stream, int $value): array
{
$list = [];
for ($i = 0; $i < $value; $i++) {
/** @psalm-suppress MixedAssignment */
$list[] = $this->wellFormed($stream);
}
return $list;
}
/**
* @psalm-return array<array-key, mixed>
*
* @throws WebAuthnException
*/
private function getMap(DataStream $stream, int $value): array
{
$map = [];
for ($i = 0; $i < $value; $i++) {
/** @psalm-suppress MixedAssignment, MixedArrayOffset */
$map[$this->wellFormed($stream)] = $this->wellFormed($stream);
}
return $map;
}
/**
* @return mixed
*
* @throws WebAuthnException
*/
private function getTag(DataStream $stream)
{
// 1 embedded data item
return $this->wellFormed($stream);
}
/**
* @return mixed
*
* @throws WebAuthnException
*/
private function getFloatNumberOrSimpleValue(DataStream $stream, int $value, int $additionalInformation)
{
switch ($additionalInformation) {
case 20:
return true;
case 21:
return false;
case 22:
return null;
case 24:
// simple value
return ord($stream->take(1));
case 25:
return $this->getHalfFloat($stream);
case 26:
return $this->getSingleFloat($stream);
case 27:
return $this->getDoubleFloat($stream);
case 31:
// "break" stop code for indefinite-length items
throw new WebAuthnException();
default:
return $value;
}
}
/**
* IEEE 754 Half-Precision Float (16 bits follow)
*
* @see https://www.rfc-editor.org/rfc/rfc7049#appendix-D
*
* @throws WebAuthnException
*/
private function getHalfFloat(DataStream $stream): float
{
$value = unpack('n', $stream->take(2));
Assert::isArray($value);
Assert::keyExists($value, 1);
Assert::integer($value[1]);
$half = $value[1];
$exp = ($half >> 10) & 0x1f;
$mant = $half & 0x3ff;
if ($exp === 0) {
$val = $mant * (2 ** -24);
} elseif ($exp !== 31) {
$val = ($mant + 1024) * (2 ** ($exp - 25));
} else {
$val = $mant === 0 ? INF : NAN;
}
return $half & 0x8000 ? -$val : $val;
}
/**
* IEEE 754 Single-Precision Float (32 bits follow)
*
* @throws WebAuthnException
*/
private function getSingleFloat(DataStream $stream): float
{
$value = unpack('G', $stream->take(4));
Assert::isArray($value);
Assert::keyExists($value, 1);
Assert::float($value[1]);
return $value[1];
}
/**
* IEEE 754 Double-Precision Float (64 bits follow)
*
* @throws WebAuthnException
*/
private function getDoubleFloat(DataStream $stream): float
{
$value = unpack('E', $stream->take(8));
Assert::isArray($value);
Assert::keyExists($value, 1);
Assert::float($value[1]);
return $value[1];
}
}
|