limoncello-php-dist /
crypt
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Limoncello\Crypt; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Copyright 2015-2019 [email protected] |
||
| 7 | * |
||
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
| 9 | * you may not use this file except in compliance with the License. |
||
| 10 | * You may obtain a copy of the License at |
||
| 11 | * |
||
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 13 | * |
||
| 14 | * Unless required by applicable law or agreed to in writing, software |
||
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 17 | * See the License for the specific language governing permissions and |
||
| 18 | * limitations under the License. |
||
| 19 | */ |
||
| 20 | |||
| 21 | use Generator; |
||
| 22 | use Limoncello\Crypt\Exceptions\CryptException; |
||
| 23 | use function assert; |
||
| 24 | use function file_exists; |
||
| 25 | use function openssl_pkey_free; |
||
| 26 | use function openssl_pkey_get_details; |
||
| 27 | use function strlen; |
||
| 28 | use function substr; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @package Limoncello\Crypt |
||
| 32 | */ |
||
| 33 | abstract class BaseAsymmetricCrypt extends BaseCrypt |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var resource|null |
||
| 37 | */ |
||
| 38 | private $key = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int|null |
||
| 42 | */ |
||
| 43 | private $keyBytes = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Destructor. |
||
| 47 | */ |
||
| 48 | 5 | public function __destruct() |
|
| 49 | { |
||
| 50 | 5 | $this->closeKey(); |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return self |
||
| 55 | */ |
||
| 56 | 5 | public function closeKey(): self |
|
| 57 | { |
||
| 58 | 5 | if ($this->key !== null) { |
|
| 59 | 5 | openssl_pkey_free($this->key); |
|
| 60 | 5 | $this->key = null; |
|
| 61 | 5 | $this->keyBytes = null; |
|
| 62 | } |
||
| 63 | |||
| 64 | 5 | return $this; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return resource|null |
||
| 69 | */ |
||
| 70 | 3 | protected function getKey() |
|
| 71 | { |
||
| 72 | 3 | return $this->key; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param resource $key |
||
| 77 | * |
||
| 78 | * @return self |
||
| 79 | */ |
||
| 80 | 5 | protected function setKey($key): self |
|
| 81 | { |
||
| 82 | 5 | assert(is_resource($key) === true); |
|
| 83 | |||
| 84 | 5 | $this->closeKey(); |
|
| 85 | 5 | $this->key = $key; |
|
| 86 | |||
| 87 | 5 | return $this; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return int|null |
||
|
0 ignored issues
–
show
|
|||
| 92 | */ |
||
| 93 | 3 | protected function getKeyBytes(): ?int |
|
| 94 | { |
||
| 95 | 3 | if ($this->keyBytes === null && $this->getKey() !== null) { |
|
| 96 | 3 | $this->clearErrors(); |
|
| 97 | 3 | $details = openssl_pkey_get_details($this->getKey()); |
|
| 98 | 3 | $details !== false ?: $this->throwException(new CryptException($this->getErrorMessage())); |
|
| 99 | 3 | $this->keyBytes = $details['bits'] / 8; |
|
|
0 ignored issues
–
show
It seems like
$details['bits'] / 8 can also be of type double. However, the property $keyBytes is declared as type integer|null. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 100 | } |
||
| 101 | |||
| 102 | 3 | return $this->keyBytes; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return int|null |
||
|
0 ignored issues
–
show
|
|||
| 107 | */ |
||
| 108 | 3 | protected function getEncryptChunkSize(): ?int |
|
| 109 | { |
||
| 110 | 3 | $keyBytes = $this->getKeyBytes(); |
|
| 111 | |||
| 112 | // 11 is a kind of magic number related to padding. |
||
| 113 | 3 | $result = $keyBytes === null ? null : $keyBytes - 11; |
|
| 114 | |||
| 115 | 3 | return $result; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return int|null |
||
|
0 ignored issues
–
show
|
|||
| 120 | */ |
||
| 121 | 3 | protected function getDecryptChunkSize(): ?int |
|
| 122 | { |
||
| 123 | 3 | $keyBytes = $this->getKeyBytes(); |
|
| 124 | 3 | $result = $keyBytes === null ? null : $keyBytes; |
|
| 125 | |||
| 126 | 3 | return $result; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $value |
||
| 131 | * @param int $maxSize |
||
| 132 | * |
||
| 133 | * @return Generator |
||
| 134 | */ |
||
| 135 | 3 | protected function chunkString(string $value, int $maxSize): Generator |
|
| 136 | { |
||
| 137 | 3 | $isValidInput = $maxSize > 0; |
|
| 138 | |||
| 139 | 3 | assert($isValidInput === true); |
|
| 140 | |||
| 141 | 3 | if ($isValidInput === true) { |
|
| 142 | 3 | $start = 0; |
|
| 143 | 3 | $length = strlen($value); |
|
| 144 | 3 | if ($length === 0) { |
|
| 145 | 1 | yield $value; |
|
| 146 | } |
||
| 147 | 3 | while ($start < $length) { |
|
| 148 | 3 | yield substr($value, $start, $maxSize); |
|
| 149 | 3 | $start += $maxSize; |
|
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $path |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | 5 | protected function checkIfPathToFileCheckPrefix(string $path): bool |
|
| 160 | { |
||
| 161 | 5 | if (file_exists($path) === true) { |
|
| 162 | 5 | return substr($path, 0, 7) === 'file://'; |
|
| 163 | } |
||
| 164 | |||
| 165 | 3 | return true; |
|
| 166 | } |
||
| 167 | } |
||
| 168 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.