1
|
|
|
<?php namespace jwe\impl; |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 OpenStack Foundation |
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
* Unless required by applicable law or agreed to in writing, software |
9
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
10
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11
|
|
|
* See the License for the specific language governing permissions and |
12
|
|
|
* limitations under the License. |
13
|
|
|
**/ |
14
|
|
|
use jwe\exceptions\JWEInvalidCompactFormatException; |
15
|
|
|
use jwt\IBasicJWT; |
16
|
|
|
use jwt\utils\JWTRawSerializer; |
17
|
|
|
use jwt\utils\JOSEHeaderSerializer; |
18
|
|
|
/** |
19
|
|
|
* Class JWESerializer |
20
|
|
|
* @package jwe\impl |
21
|
|
|
* @access internal |
22
|
|
|
*/ |
23
|
|
|
final class JWESerializer |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param IJWESnapshot $jwe_snapshot |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
static public function serialize(IJWESnapshot $jwe_snapshot) |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
list($header, $enc_cek, $iv, $cipher_text, $tag) = $jwe_snapshot->take(); |
34
|
|
|
|
35
|
|
|
$header = JWEJOSEHeaderSerializer::serialize($header); |
36
|
|
|
$enc_cek = JWTRawSerializer::serialize($enc_cek); |
37
|
|
|
$iv = JWTRawSerializer::serialize($iv); |
38
|
|
|
$cipher_text = JWTRawSerializer::serialize($cipher_text); |
39
|
|
|
$tag = JWTRawSerializer::serialize($tag); |
40
|
|
|
|
41
|
|
|
return sprintf('%s.%s.%s.%s.%s', $header, $enc_cek, $iv, $cipher_text, $tag); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $input |
46
|
|
|
* @return array |
47
|
|
|
* @throws JWEInvalidCompactFormatException |
48
|
|
|
*/ |
49
|
|
|
static public function deserialize($input){ |
50
|
|
|
$parts = explode(IBasicJWT::SegmentSeparator, $input); |
51
|
|
|
if (count($parts) !== 5) throw new JWEInvalidCompactFormatException; |
52
|
|
|
|
53
|
|
|
$header = JWEJOSEHeaderSerializer::deserialize($parts[0]); |
54
|
|
|
$enc_cek = JWTRawSerializer::deserialize($parts[1]); |
55
|
|
|
$iv = JWTRawSerializer::deserialize($parts[2]); |
56
|
|
|
$cipher_text = JWTRawSerializer::deserialize($parts[3]); |
57
|
|
|
$tag = JWTRawSerializer::deserialize($parts[4]); |
58
|
|
|
|
59
|
|
|
return array($header, $enc_cek, $iv, $cipher_text, $tag); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |