Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Crypto | |
0.00% |
0 / 46 |
|
0.00% |
0 / 5 |
650 | |
0.00% |
0 / 1 |
| encrypt | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| decrypt | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDefaultParams | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
306 | |||
| generateRandomKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateIV | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Ably\Utils; |
| 3 | |
| 4 | use Ably\Models\CipherParams; |
| 5 | use Ably\Exceptions\AblyException; |
| 6 | |
| 7 | /** |
| 8 | * Provides static methods for encryption/decryption |
| 9 | */ |
| 10 | class Crypto { |
| 11 | /** |
| 12 | * Encrypts data and returns binary payload. Automatically updates $cipherParams with a new IV. |
| 13 | * @return string|false Binary payload coposed of concatenated IV and encrypted data, false if unsuccessful. |
| 14 | */ |
| 15 | public static function encrypt( $plaintext, $cipherParams ) { |
| 16 | $raw = defined( 'OPENSSL_RAW_DATA' ) ? OPENSSL_RAW_DATA : true; |
| 17 | |
| 18 | $ciphertext = openssl_encrypt( $plaintext, $cipherParams->getAlgorithmString(), $cipherParams->key, $raw, $cipherParams->iv ); |
| 19 | |
| 20 | if ($ciphertext === false) { |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | $iv = $cipherParams->iv; |
| 25 | |
| 26 | self::updateIV( $cipherParams ); |
| 27 | |
| 28 | return $iv.$ciphertext; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Decrypts payload and returns original data. |
| 33 | * @return string|false Original data as string or string containing binary data, false if unsuccessful. |
| 34 | */ |
| 35 | public static function decrypt( $payload, $cipherParams ) { |
| 36 | $raw = defined( 'OPENSSL_RAW_DATA' ) ? OPENSSL_RAW_DATA : true; |
| 37 | |
| 38 | $ivLength = openssl_cipher_iv_length( $cipherParams->getAlgorithmString() ); |
| 39 | $iv = substr( $payload, 0, $ivLength ); |
| 40 | $ciphertext = substr( $payload, $ivLength ); |
| 41 | return openssl_decrypt( $ciphertext, $cipherParams->getAlgorithmString(), $cipherParams->key, $raw, $iv ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns default encryption parameters. |
| 46 | * @param $params Array Array containing optional cipher parameters. A `key` must be specified. |
| 47 | * The key may be either a binary string or a base64 encoded string, in which case `'base64Key' => true` must be set. |
| 48 | * `iv` can also be provided as binary or base64 string (`'base64IV' => true`), although you shouldn't need it in most cases. |
| 49 | * @return CipherParams Default encryption parameters. |
| 50 | */ |
| 51 | public static function getDefaultParams( $params ) { |
| 52 | if ( !isset( $params['key'] ) ) throw new AblyException ( 'No key specified.', 40003, 400 ); |
| 53 | |
| 54 | $cipherParams = new CipherParams(); |
| 55 | |
| 56 | if ( isset( $params['base64Key'] ) && $params['base64Key'] ) { |
| 57 | $params['key'] = strtr( $params['key'], '_-', '/+' ); |
| 58 | $params['key'] = base64_decode( $params['key'] ); |
| 59 | } |
| 60 | |
| 61 | $cipherParams->key = $params['key']; |
| 62 | $cipherParams->algorithm = isset( $params['algorithm'] ) ? $params['algorithm'] : 'aes'; |
| 63 | |
| 64 | if ($cipherParams->algorithm == 'aes') { |
| 65 | $cipherParams->mode = isset( $params['mode'] ) ? $params['mode'] : 'cbc'; |
| 66 | $cipherParams->keyLength = isset( $params['keyLength'] ) ? $params['keyLength'] : strlen( $cipherParams->key ) * 8; |
| 67 | |
| 68 | if ( !in_array( $cipherParams->keyLength, [ 128, 256 ] ) ) { |
| 69 | throw new AblyException ( 'Unsupported keyLength. Only 128 and 256 bits are supported.', 40003, 400 ); |
| 70 | } |
| 71 | |
| 72 | if ( $cipherParams->keyLength / 8 != strlen( $cipherParams->key ) ) { |
| 73 | throw new AblyException ( 'keyLength does not match the actual key length.', 40003, 400 ); |
| 74 | } |
| 75 | |
| 76 | if ( !in_array( $cipherParams->getAlgorithmString(), [ 'aes-128-cbc', 'aes-256-cbc' ] ) ) { |
| 77 | throw new AblyException ( 'Unsupported cipher configuration "' . $cipherParams->getAlgorithmString() |
| 78 | . '". The supported configurations are aes-128-cbc and aes-256-cbc', 40003, 400 ); |
| 79 | } |
| 80 | } else { |
| 81 | if ( isset( $params['mode'] ) ) $cipherParams->mode = $params['mode']; |
| 82 | if ( isset( $params['keyLength'] ) ) $cipherParams->keyLength = $params['keyLength']; |
| 83 | |
| 84 | if ( !$cipherParams->checkValidAlgorithm() ) { |
| 85 | throw new AblyException( 'The specified algorithm "'.$cipherParams->getAlgorithmString().'"' |
| 86 | . ' is not supported by openssl. See openssl_get_cipher_methods.', 40003, 400 ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( isset( $params['iv'] ) ) { |
| 91 | $cipherParams->iv = $params['iv']; |
| 92 | if ( isset( $params['base64Iv'] ) && $params['base64Iv'] ) { |
| 93 | $cipherParams->iv = strtr( $cipherParams->iv, '_-', '/+' ); |
| 94 | $cipherParams->iv = base64_decode( $cipherParams->iv ); |
| 95 | } |
| 96 | } else { |
| 97 | $cipherParams->generateIV(); |
| 98 | } |
| 99 | |
| 100 | return $cipherParams; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Generates a random encryption key. |
| 105 | * @param $keyLength|null The length of the key to be generated in bits, defaults to 256. |
| 106 | */ |
| 107 | public static function generateRandomKey( $keyLength = 256 ) { |
| 108 | return openssl_random_pseudo_bytes( $keyLength / 8 ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Updates CipherParams' Initialization Vector by encrypting a fixed string with current CipherParams state, thus randomizing it. |
| 113 | */ |
| 114 | protected static function updateIV( CipherParams $cipherParams ) { |
| 115 | $raw = defined( 'OPENSSL_RAW_DATA' ) ? OPENSSL_RAW_DATA : true; |
| 116 | |
| 117 | $ivLength = strlen( $cipherParams->iv ); |
| 118 | |
| 119 | $cipherParams->iv = openssl_encrypt( str_repeat( ' ', $ivLength ), $cipherParams->getAlgorithmString(), $cipherParams->key, $raw, $cipherParams->iv ); |
| 120 | $cipherParams->iv = substr( $cipherParams->iv, 0, $ivLength ); |
| 121 | } |
| 122 | } |