| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Signature\Serializer; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 9 |
use Jose\Component\Core\Util\JsonConverter; |
| 10 |
use Jose\Component\Signature\JWS; |
| 11 |
use LogicException; |
| 12 |
use function array_key_exists; |
| 13 |
use function count; |
| 14 |
use function is_array; |
| 15 |
use function is_string; |
| 16 |
|
| 17 |
final class JSONGeneralSerializer extends Serializer |
| 18 |
{ |
| 19 |
public const NAME = 'jws_json_general'; |
| 20 |
|
| 21 |
public function displayName(): string |
| 22 |
{ |
| 23 |
return 'JWS JSON General'; |
| 24 |
} |
| 25 |
|
| 26 |
public function name(): string |
| 27 |
{ |
| 28 |
return self::NAME; |
| 29 |
} |
| 30 |
|
| 31 |
public function serialize(JWS $jws, ?int $signatureIndex = null): string |
| 32 |
{ |
| 33 |
if ($jws->countSignatures() === 0) { |
| 34 |
throw new LogicException('No signature.'); |
| 35 |
} |
| 36 |
|
| 37 |
$data = []; |
| 38 |
$this->checkPayloadEncoding($jws); |
| 39 |
|
| 40 |
if ($jws->isPayloadDetached() === false) { |
| 41 |
$data['payload'] = $jws->getEncodedPayload(); |
| 42 |
} |
| 43 |
|
| 44 |
$data['signatures'] = []; |
| 45 |
foreach ($jws->getSignatures() as $signature) { |
| 46 |
$tmp = [ |
| 47 |
'signature' => Base64UrlSafe::encodeUnpadded($signature->getSignature()), |
| 48 |
]; |
| 49 |
$values = [ |
| 50 |
'protected' => $signature->getEncodedProtectedHeader(), |
| 51 |
'header' => $signature->getHeader(), |
| 52 |
]; |
| 53 |
|
| 54 |
foreach ($values as $key => $value) { |
| 55 |
if ((is_string($value) && $value !== '') || (is_array($value) && count($value) !== 0)) { |
| 56 |
$tmp[$key] = $value; |
| 57 |
} |
| 58 |
} |
| 59 |
$data['signatures'][] = $tmp; |
| 60 |
} |
| 61 |
|
| 62 |
return JsonConverter::encode($data); |
| 63 |
} |
| 64 |
|
| 65 |
public function unserialize(string $input): JWS |
| 66 |
{ |
| 67 |
$data = JsonConverter::decode($input); |
| 68 |
if (! is_array($data)) { |
| 69 |
throw new InvalidArgumentException('Unsupported input.'); |
| 70 |
} |
| 71 |
if (! isset($data['signatures'])) { |
| 72 |
throw new InvalidArgumentException('Unsupported input.'); |
| 73 |
} |
| 74 |
|
| 75 |
$isPayloadEncoded = null; |
| 76 |
$rawPayload = $data['payload'] ?? null; |
| 77 |
$signatures = []; |
| 78 |
foreach ($data['signatures'] as $signature) { |
| 79 |
if (! isset($signature['signature'])) { |
| 80 |
throw new InvalidArgumentException('Unsupported input.'); |
| 81 |
} |
| 82 |
[$encodedProtectedHeader, $protectedHeader, $header] = $this->processHeaders($signature); |
| 83 |
$signatures[] = [ |
| 84 |
'signature' => Base64UrlSafe::decodeNoPadding($signature['signature']), |
| 85 |
'protected' => $protectedHeader, |
| 86 |
'encoded_protected' => $encodedProtectedHeader, |
| 87 |
'header' => $header, |
| 88 |
]; |
| 89 |
$isPayloadEncoded = $this->processIsPayloadEncoded($isPayloadEncoded, $protectedHeader); |
| 90 |
} |
| 91 |
|
| 92 |
$payload = $this->processPayload($rawPayload, $isPayloadEncoded); |
| 93 |
$jws = new JWS($payload, $rawPayload); |
| 94 |
foreach ($signatures as $signature) { |
| 95 |
$jws = $jws->addSignature( |
| 96 |
$signature['signature'], |
| 97 |
$signature['protected'], |
| 98 |
$signature['encoded_protected'], |
| 99 |
$signature['header'] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
return $jws; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private function processIsPayloadEncoded(?bool $isPayloadEncoded, array $protectedHeader): bool |
| 110 |
{ |
| 111 |
if ($isPayloadEncoded === null) { |
| 112 |
return $this->isPayloadEncoded($protectedHeader); |
| 113 |
} |
| 114 |
if ($this->isPayloadEncoded($protectedHeader) !== $isPayloadEncoded) { |
| 115 |
throw new InvalidArgumentException('Foreign payload encoding detected.'); |
| 116 |
} |
| 117 |
|
| 118 |
return $isPayloadEncoded; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function processHeaders(array $signature): array |
| 126 |
{ |
| 127 |
$encodedProtectedHeader = $signature['protected'] ?? null; |
| 128 |
$protectedHeader = $encodedProtectedHeader === null ? [] : JsonConverter::decode( |
| 129 |
Base64UrlSafe::decodeNoPadding($encodedProtectedHeader) |
| 130 |
); |
| 131 |
$header = array_key_exists('header', $signature) ? $signature['header'] : []; |
| 132 |
|
| 133 |
return [$encodedProtectedHeader, $protectedHeader, $header]; |
| 134 |
} |
| 135 |
|
| 136 |
private function processPayload(?string $rawPayload, ?bool $isPayloadEncoded): ?string |
| 137 |
{ |
| 138 |
if ($rawPayload === null) { |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
return $isPayloadEncoded === false ? $rawPayload : Base64UrlSafe::decodeNoPadding($rawPayload); |
| 143 |
} |
| 144 |
|
| 145 |
private function checkPayloadEncoding(JWS $jws): void |
| 146 |
{ |
| 147 |
if ($jws->isPayloadDetached()) { |
| 148 |
return; |
| 149 |
} |
| 150 |
$is_encoded = null; |
| 151 |
foreach ($jws->getSignatures() as $signature) { |
| 152 |
if ($is_encoded === null) { |
| 153 |
$is_encoded = $this->isPayloadEncoded($signature->getProtectedHeader()); |
| 154 |
} |
| 155 |
if ($is_encoded !== $this->isPayloadEncoded($signature->getProtectedHeader())) { |
| 156 |
throw new LogicException('Foreign payload encoding detected.'); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |