Completed
Push — master ( 668fd8...cdc0a6 )
by Florent
12:11
created

AESKW::checkKey()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 8.8571
cc 5
eloc 5
nc 3
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Algorithm\KeyEncryption;
13
14
use Base64Url\Base64Url;
15
use Jose\Object\JWKInterface;
16
17
/**
18
 * Class AESKW.
19
 */
20
abstract class AESKW implements KeyEncryptionInterface
21
{
22
    /**
23
     * @param \Jose\Object\JWKInterface $key
24
     * @param string       $cek
25
     * @param array        $header
26
     *
27
     * @return mixed
28
     */
29
    public function encryptKey(JWKInterface $key, $cek, array &$header)
30
    {
31
        $this->checkKey($key);
32
        $wrapper = $this->getWrapper();
33
34
        return $wrapper->wrap(Base64Url::decode($key->get('k')), $cek);
35
    }
36
37
    /**
38
     * @param \Jose\Object\JWKInterface $key
39
     * @param string       $encryted_cek
40
     * @param array        $header
41
     *
42
     * @return mixed
43
     */
44
    public function decryptKey(JWKInterface $key, $encryted_cek, array $header)
45
    {
46
        $this->checkKey($key);
47
        $wrapper = $this->getWrapper();
48
49
        return $wrapper->unwrap(Base64Url::decode($key->get('k')), $encryted_cek);
50
    }
51
52
    /**
53
     * @param \Jose\Object\JWKInterface $key
54
     */
55
    protected function checkKey(JWKInterface $key)
56
    {
57
        if (!$key->has('kty') || 'oct' !== $key->get('kty') || !$key->has('k')) {
58
            throw new \InvalidArgumentException('The key is not valid');
59
        }
60
        if ($this->getKeySize() !== strlen(Base64Url::decode($key->get('k')))) {
61
            throw new \InvalidArgumentException('The key size is not valid');
62
        }
63
    }
64
65
    /**
66
     * @return int
67
     */
68
    abstract protected function getKeySize();
69
70
    /**
71
     * @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW
72
     */
73
    abstract protected function getWrapper();
74
}
75