|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Encryption\Serializer; |
|
15
|
|
|
|
|
16
|
|
|
use Base64Url\Base64Url; |
|
17
|
|
|
use Jose\Component\Encryption\JWE; |
|
18
|
|
|
use Jose\Component\Encryption\Recipient; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CompactSerializer. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class CompactSerializer implements JWESerializerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
public const NAME = 'jwe_compact'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function displayName(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return 'JWE Compact'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function name(): string |
|
39
|
|
|
{ |
|
40
|
|
|
return self::NAME; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function serialize(JWE $jwe, ?int $recipientIndex = null): string |
|
47
|
|
|
{ |
|
48
|
|
|
if (null === $recipientIndex) { |
|
49
|
|
|
$recipientIndex = 0; |
|
50
|
|
|
} |
|
51
|
|
|
$recipient = $jwe->getRecipient($recipientIndex); |
|
52
|
|
|
|
|
53
|
|
|
$this->checkHasNoAAD($jwe); |
|
54
|
|
|
$this->checkHasSharedProtectedHeaders($jwe); |
|
55
|
|
|
$this->checkRecipientHasNoHeaders($jwe, $recipientIndex); |
|
56
|
|
|
|
|
57
|
|
|
return sprintf( |
|
58
|
|
|
'%s.%s.%s.%s.%s', |
|
59
|
|
|
$jwe->getEncodedSharedProtectedHeaders(), |
|
60
|
|
|
Base64Url::encode(null === $recipient->getEncryptedKey() ? '' : $recipient->getEncryptedKey()), |
|
61
|
|
|
Base64Url::encode(null === $jwe->getIV() ? '' : $jwe->getIV()), |
|
62
|
|
|
Base64Url::encode($jwe->getCiphertext()), |
|
63
|
|
|
Base64Url::encode(null === $jwe->getTag() ? '' : $jwe->getTag()) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function unserialize(string $input): JWE |
|
71
|
|
|
{ |
|
72
|
|
|
$parts = explode('.', $input); |
|
73
|
|
|
if (5 !== count($parts)) { |
|
74
|
|
|
throw new \InvalidArgumentException('Unsupported input'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
|
|
$encodedSharedProtectedHeader = $parts[0]; |
|
79
|
|
|
$sharedProtectedHeader = json_decode(Base64Url::decode($encodedSharedProtectedHeader), true); |
|
80
|
|
|
$encryptedKey = empty($parts[1]) ? null : Base64Url::decode($parts[1]); |
|
81
|
|
|
$iv = Base64Url::decode($parts[2]); |
|
82
|
|
|
$ciphertext = Base64Url::decode($parts[3]); |
|
83
|
|
|
$tag = Base64Url::decode($parts[4]); |
|
84
|
|
|
|
|
85
|
|
|
return JWE::create( |
|
86
|
|
|
$ciphertext, |
|
87
|
|
|
$iv, |
|
88
|
|
|
$tag, |
|
89
|
|
|
null, |
|
90
|
|
|
[], |
|
91
|
|
|
$sharedProtectedHeader, |
|
92
|
|
|
$encodedSharedProtectedHeader, |
|
93
|
|
|
[Recipient::create([], $encryptedKey)]); |
|
94
|
|
|
} catch (\Exception $e) { |
|
95
|
|
|
throw new \InvalidArgumentException('Unsupported input'); |
|
96
|
|
|
} catch (\Error $e) { |
|
97
|
|
|
throw new \InvalidArgumentException('Unsupported input'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param JWE $jwe |
|
103
|
|
|
*/ |
|
104
|
|
|
private function checkHasNoAAD(JWE $jwe) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!empty($jwe->getAAD())) { |
|
107
|
|
|
throw new \LogicException('This JWE has AAD and cannot be converted into Compact JSON.'); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param JWE $jwe |
|
113
|
|
|
* @param int $id |
|
114
|
|
|
*/ |
|
115
|
|
|
private function checkRecipientHasNoHeaders(JWE $jwe, int $id) |
|
116
|
|
|
{ |
|
117
|
|
|
if (!empty($jwe->getSharedHeaders()) || !empty($jwe->getRecipient($id)->getHeaders())) { |
|
118
|
|
|
throw new \LogicException('This JWE has shared headers or recipient headers and cannot be converted into Compact JSON.'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param JWE $jwe |
|
124
|
|
|
*/ |
|
125
|
|
|
private function checkHasSharedProtectedHeaders(JWE $jwe) |
|
126
|
|
|
{ |
|
127
|
|
|
if (empty($jwe->getSharedProtectedHeaders())) { |
|
128
|
|
|
throw new \LogicException('This JWE does not have shared protected headers and cannot be converted into Compact JSON.'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|