OpenSSLCrypto::getKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\HybridSessions\Crypto;
4
5
/**
6
 * Some cryptography used for Session cookie encryption.
7
 *
8
 */
9
class OpenSSLCrypto implements CryptoHandler
10
{
11
    protected $key;
12
13
    protected $salt;
14
15
    protected $saltedKey;
16
17
    /**
18
     * @return string
19
     */
20
    public function getKey()
21
    {
22
        return $this->key;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getSalt()
29
    {
30
        return $this->salt;
31
    }
32
33
    /**
34
     * @param string $key a per-site secret string which is used as the base encryption key.
35
     * @param string $salt a per-session random string which is used as a salt to generate a per-session key
36
     *
37
     * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session,
38
     * and even modify & re-sign it.
39
     *
40
     * The salt is a random per-session string that is used with the base encryption key to create a per-session key.
41
     * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key.
42
     *
43
     * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't
44
     * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes
45
     * a great salt, so no need to generate & handle another one.
46
     */
47
    public function __construct($key, $salt)
48
    {
49
        $this->key = $key;
50
        $this->salt = $salt;
51
        $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, 0, true);
52
    }
53
54
    /**
55
     * Encrypt and then sign some cleartext
56
     *
57
     * @param string $cleartext - The cleartext to encrypt and sign
58
     * @return string - The encrypted-and-signed message as base64 ASCII.
59
     */
60
    public function encrypt($cleartext)
61
    {
62
        $cipher = "AES-256-CBC";
63
        $ivlen = openssl_cipher_iv_length($cipher);
64
        $iv = openssl_random_pseudo_bytes($ivlen);
65
        $ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
66
        $hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
67
        $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw);
0 ignored issues
show
Unused Code introduced by
$ciphertext is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69
        return base64_encode($iv . $hmac . $ciphertext_raw);
70
    }
71
72
    /**
73
     * Check the signature on an encrypted-and-signed message, and if valid
74
     * decrypt the content
75
     *
76
     * @param string $data - The encrypted-and-signed message as base64 ASCII
77
     *
78
     * @return bool|string - The decrypted cleartext or false if signature failed
79
     */
80
    public function decrypt($data)
81
    {
82
        $c = base64_decode($data);
83
        $cipher = "AES-256-CBC";
84
        $ivlen = openssl_cipher_iv_length($cipher);
85
        $iv = substr($c, 0, $ivlen);
86
        $hmac = substr($c, $ivlen, $sha2len = 32);
87
        $ciphertext_raw = substr($c, $ivlen + $sha2len);
88
        $cleartext = openssl_decrypt($ciphertext_raw, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
89
        $calcmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
90
91
        if (hash_equals($hmac, $calcmac)) {
92
            return $cleartext;
93
        }
94
95
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface SilverStripe\HybridSessi...\CryptoHandler::decrypt of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
96
    }
97
}
98