@@ -142,6 +142,11 @@ discard block |
||
| 142 | 142 | return $this->makeBox($message, $nonce, $senderPrivateKey, $recipientPublicKey); |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | + /** |
|
| 146 | + * @param string $senderPrivateKey |
|
| 147 | + * @param string $recipientPublicKey |
|
| 148 | + * @param string $nonce |
|
| 149 | + */ |
|
| 145 | 150 | final public function encryptFileMessage(UploadFileResult $uploadFileResult, |
| 146 | 151 | EncryptResult $encryptResult, |
| 147 | 152 | UploadFileResult $thumbnailUploadFileResult = null, |
@@ -190,9 +195,9 @@ discard block |
||
| 190 | 195 | * make a secret box |
| 191 | 196 | * |
| 192 | 197 | * @param $data |
| 193 | - * @param $nonce |
|
| 198 | + * @param string $nonce |
|
| 194 | 199 | * @param $key |
| 195 | - * @return mixed |
|
| 200 | + * @return string |
|
| 196 | 201 | */ |
| 197 | 202 | abstract protected function makeSecretBox($data, $nonce, $key); |
| 198 | 203 | |
@@ -334,6 +339,9 @@ discard block |
||
| 334 | 339 | return hash_hmac('sha256', $phoneNoClean, self::PHONENO_HMAC_KEY); |
| 335 | 340 | } |
| 336 | 341 | |
| 342 | + /** |
|
| 343 | + * @return string |
|
| 344 | + */ |
|
| 337 | 345 | abstract protected function createRandom($size); |
| 338 | 346 | |
| 339 | 347 | /** |
@@ -376,7 +384,7 @@ discard block |
||
| 376 | 384 | abstract public function validate(); |
| 377 | 385 | |
| 378 | 386 | /** |
| 379 | - * @param $data |
|
| 387 | + * @param string $data |
|
| 380 | 388 | * @return EncryptResult |
| 381 | 389 | */ |
| 382 | 390 | public final function encryptFile($data) { |
@@ -405,6 +413,10 @@ discard block |
||
| 405 | 413 | return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
| 406 | 414 | } |
| 407 | 415 | |
| 416 | + /** |
|
| 417 | + * @param string $data |
|
| 418 | + * @param string $key |
|
| 419 | + */ |
|
| 408 | 420 | public final function decryptFileThumbnail($data, $key) { |
| 409 | 421 | $result = $this->openSecretBox($data, self::FILE_THUMBNAIL_NONCE, $key); |
| 410 | 422 | return false === $result ? null : $result; |
@@ -524,8 +524,8 @@ |
||
| 524 | 524 | # partly taken from https://github.com/symfony/polyfill-php56/blob/master/Php56.php#L45-L51 |
| 525 | 525 | $ret = 0; |
| 526 | 526 | for ($i = 0; $i < strlen($str1); ++$i) { |
| 527 | - $ret |= ord($str1[$i]) ^ ord($str2[$i]); |
|
| 528 | - } |
|
| 527 | + $ret |= ord($str1[$i]) ^ ord($str2[$i]); |
|
| 528 | + } |
|
| 529 | 529 | return 0 === $result; |
| 530 | 530 | } |
| 531 | 531 | } |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | * @return CryptTool |
| 43 | 43 | */ |
| 44 | 44 | public static function getInstance() { |
| 45 | - if(null === self::$instance) { |
|
| 46 | - foreach(array( |
|
| 45 | + if (null === self::$instance) { |
|
| 46 | + foreach (array( |
|
| 47 | 47 | function() { |
| 48 | 48 | return self::createInstance(self::TYPE_SODIUM); |
| 49 | 49 | }, |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | return self::createInstance(self::TYPE_SALT); |
| 52 | 52 | }) as $instanceGenerator) { |
| 53 | 53 | $i = $instanceGenerator->__invoke(); |
| 54 | - if(null !== $i) { |
|
| 54 | + if (null !== $i) { |
|
| 55 | 55 | self::$instance = $i; |
| 56 | 56 | break; |
| 57 | 57 | } |
@@ -66,18 +66,18 @@ discard block |
||
| 66 | 66 | * @return null|CryptTool null on unknown type |
| 67 | 67 | */ |
| 68 | 68 | public static function createInstance($type) { |
| 69 | - switch($type) { |
|
| 69 | + switch ($type) { |
|
| 70 | 70 | case self::TYPE_SODIUM: |
| 71 | 71 | $instance = new CryptToolSodium(); |
| 72 | - if(false === $instance->isSupported()) { |
|
| 72 | + if (false === $instance->isSupported()) { |
|
| 73 | 73 | //try to instance old version of sodium wrapper |
| 74 | 74 | /** @noinspection PhpDeprecationInspection */ |
| 75 | 75 | $instance = new CryptToolSodiumDep(); |
| 76 | 76 | } |
| 77 | - return $instance->isSupported() ? $instance :null; |
|
| 77 | + return $instance->isSupported() ? $instance : null; |
|
| 78 | 78 | case self::TYPE_SALT: |
| 79 | 79 | $instance = new CryptToolSalt(); |
| 80 | - return $instance->isSupported() ? $instance :null; |
|
| 80 | + return $instance->isSupported() ? $instance : null; |
|
| 81 | 81 | default: |
| 82 | 82 | return null; |
| 83 | 83 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | final public function encryptMessageText($text, $senderPrivateKey, $recipientPublicKey, $nonce) { |
| 107 | 107 | /* prepend type byte (0x01) to message data */ |
| 108 | - $textBytes = "\x01" . $text; |
|
| 108 | + $textBytes = "\x01".$text; |
|
| 109 | 109 | |
| 110 | 110 | /* determine random amount of PKCS7 padding */ |
| 111 | 111 | $padbytes = $this->generatePadBytes(); |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | $senderPrivateKey, |
| 131 | 131 | $recipientPublicKey, |
| 132 | 132 | $nonce) { |
| 133 | - $message = "\x02" . $this->hex2bin($uploadFileResult->getBlobId()); |
|
| 133 | + $message = "\x02".$this->hex2bin($uploadFileResult->getBlobId()); |
|
| 134 | 134 | $message .= pack('V', $encryptResult->getSize()); |
| 135 | 135 | $message .= $encryptResult->getNonce(); |
| 136 | 136 | |
@@ -161,11 +161,11 @@ discard block |
||
| 161 | 161 | 'i' => 0 |
| 162 | 162 | ); |
| 163 | 163 | |
| 164 | - if($thumbnailUploadFileResult !== null && strlen($thumbnailUploadFileResult->getBlobId()) > 0) { |
|
| 164 | + if ($thumbnailUploadFileResult !== null && strlen($thumbnailUploadFileResult->getBlobId()) > 0) { |
|
| 165 | 165 | $messageContent['t'] = $thumbnailUploadFileResult->getBlobId(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - $message = "\x17" . json_encode($messageContent); |
|
| 168 | + $message = "\x17".json_encode($messageContent); |
|
| 169 | 169 | |
| 170 | 170 | /* determine random amount of PKCS7 padding */ |
| 171 | 171 | $padbytes = $this->generatePadBytes(); |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | |
| 239 | 239 | /* remove padding */ |
| 240 | 240 | $padbytes = ord($data[strlen($data)-1]); |
| 241 | - $realDataLength = strlen($data) - $padbytes; |
|
| 241 | + $realDataLength = strlen($data)-$padbytes; |
|
| 242 | 242 | if ($realDataLength < 1) { |
| 243 | 243 | throw new BadMessageException(); |
| 244 | 244 | } |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | return new TextMessage(substr($data, 1)); |
| 265 | 265 | case DeliveryReceipt::TYPE_CODE: |
| 266 | 266 | /* Delivery receipt */ |
| 267 | - if ($realDataLength < (self::MESSAGE_ID_LEN-2) || (($realDataLength - 2) % self::MESSAGE_ID_LEN) != 0) { |
|
| 267 | + if ($realDataLength < (self::MESSAGE_ID_LEN-2) || (($realDataLength-2)%self::MESSAGE_ID_LEN) != 0) { |
|
| 268 | 268 | throw new BadMessageException(); |
| 269 | 269 | } |
| 270 | 270 | |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | return new DeliveryReceipt($receiptType, $messageIds); |
| 275 | 275 | case ImageMessage::TYPE_CODE: |
| 276 | 276 | /* Image Message */ |
| 277 | - if ($realDataLength != 1 + self::BLOB_ID_LEN + self::IMAGE_FILE_SIZE_LEN + self::IMAGE_NONCE_LEN) { |
|
| 277 | + if ($realDataLength != 1+self::BLOB_ID_LEN+self::IMAGE_FILE_SIZE_LEN+self::IMAGE_NONCE_LEN) { |
|
| 278 | 278 | throw new BadMessageException(); |
| 279 | 279 | } |
| 280 | 280 | |
@@ -285,12 +285,12 @@ discard block |
||
| 285 | 285 | case FileMessage::TYPE_CODE: |
| 286 | 286 | /* Image Message */ |
| 287 | 287 | $decodeResult = json_decode(substr($data, 1), true); |
| 288 | - if(null === $decodeResult || false === $decodeResult) { |
|
| 288 | + if (null === $decodeResult || false === $decodeResult) { |
|
| 289 | 289 | throw new BadMessageException(); |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | $values = AssocArray::byJsonString(substr($data, 1), array('b', 't', 'k', 'm', 'n', 's')); |
| 293 | - if(null === $values) { |
|
| 293 | + if (null === $values) { |
|
| 294 | 294 | throw new BadMessageException(); |
| 295 | 295 | } |
| 296 | 296 | |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | * @return null|string |
| 393 | 393 | */ |
| 394 | 394 | public final function decryptFile($data, $key) { |
| 395 | - $result = $this->openSecretBox($data, self::FILE_NONCE, $key); |
|
| 395 | + $result = $this->openSecretBox($data, self::FILE_NONCE, $key); |
|
| 396 | 396 | return false === $result ? null : $result; |
| 397 | 397 | } |
| 398 | 398 | |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | */ |
| 404 | 404 | public final function encryptFileThumbnail($data, $key) { |
| 405 | 405 | $box = $this->makeSecretBox($data, self::FILE_THUMBNAIL_NONCE, $key); |
| 406 | - return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
|
| 406 | + return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | public final function decryptFileThumbnail($data, $key) { |
@@ -450,7 +450,7 @@ discard block |
||
| 450 | 450 | */ |
| 451 | 451 | private function generatePadBytes() { |
| 452 | 452 | $padbytes = 0; |
| 453 | - while($padbytes < 1 || $padbytes > 255) { |
|
| 453 | + while ($padbytes < 1 || $padbytes > 255) { |
|
| 454 | 454 | $padbytes = ord($this->createRandom(1)); |
| 455 | 455 | } |
| 456 | 456 | return $padbytes; |
@@ -202,12 +202,12 @@ discard block |
||
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | if ($tlsCipher = $this->setting->getTlsOption(ConnectionSettings::tlsOptionCipher, null)) { |
| 205 | - if(true === is_string($tlsCipher)) { |
|
| 205 | + if (true === is_string($tlsCipher)) { |
|
| 206 | 206 | $options[CURLOPT_SSL_CIPHER_LIST] = $tlsCipher; |
| 207 | 207 | } |
| 208 | 208 | } |
| 209 | 209 | if ($pinnedKey = $this->setting->getTlsOption(ConnectionSettings::tlsOptionPinnedKey, Constants::DEFAULT_PINNED_KEY)) { |
| 210 | - if(true === is_string($pinnedKey)) { |
|
| 210 | + if (true === is_string($pinnedKey)) { |
|
| 211 | 211 | $options[CURLOPT_PINNEDPUBLICKEY] = $pinnedKey; |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | return $this->call($command->getPath(), |
| 240 | 240 | $this->createDefaultOptions($progress), |
| 241 | 241 | $params, |
| 242 | - function ($httpCode, $response) use ($command) { |
|
| 242 | + function($httpCode, $response) use ($command) { |
|
| 243 | 243 | return $command->parseResult($httpCode, $response); |
| 244 | 244 | }); |
| 245 | 245 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $options[CURLOPT_HTTPHEADER] = array( |
| 258 | 258 | 'Content-Type: application/x-www-form-urlencoded'); |
| 259 | 259 | |
| 260 | - return $this->call($command->getPath(), $options, null, function ($httpCode, $response) use ($command) { |
|
| 260 | + return $this->call($command->getPath(), $options, null, function($httpCode, $response) use ($command) { |
|
| 261 | 261 | return $command->parseResult($httpCode, $response); |
| 262 | 262 | }); |
| 263 | 263 | } |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | 'blob' => $command->getData() |
| 278 | 278 | ); |
| 279 | 279 | |
| 280 | - return $this->call($command->getPath(), $options, $params, function ($httpCode, $response) use ($command) { |
|
| 280 | + return $this->call($command->getPath(), $options, $params, function($httpCode, $response) use ($command) { |
|
| 281 | 281 | return $command->parseResult($httpCode, $response); |
| 282 | 282 | }); |
| 283 | 283 | } |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | |
| 305 | 305 | $response = curl_exec($session); |
| 306 | 306 | if (false === $response) { |
| 307 | - throw new Exception($path . ' ' . curl_error($session)); |
|
| 307 | + throw new Exception($path.' '.curl_error($session)); |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | $httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE); |
@@ -49,20 +49,20 @@ |
||
| 49 | 49 | $this->host = $host; |
| 50 | 50 | |
| 51 | 51 | // TLS options |
| 52 | - if(null !== $tlsOptions && is_array($tlsOptions)) { |
|
| 53 | - if(true === array_key_exists(self::tlsOptionForceHttps, $tlsOptions)) { |
|
| 52 | + if (null !== $tlsOptions && is_array($tlsOptions)) { |
|
| 53 | + if (true === array_key_exists(self::tlsOptionForceHttps, $tlsOptions)) { |
|
| 54 | 54 | $this->tlsOptions[self::tlsOptionForceHttps] = $tlsOptions[self::tlsOptionForceHttps] === true; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if(true === array_key_exists(self::tlsOptionVersion, $tlsOptions)) { |
|
| 57 | + if (true === array_key_exists(self::tlsOptionVersion, $tlsOptions)) { |
|
| 58 | 58 | $this->tlsOptions[self::tlsOptionVersion] = $tlsOptions[self::tlsOptionVersion]; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if(true === array_key_exists(self::tlsOptionCipher, $tlsOptions)) { |
|
| 61 | + if (true === array_key_exists(self::tlsOptionCipher, $tlsOptions)) { |
|
| 62 | 62 | $this->tlsOptions[self::tlsOptionCipher] = $tlsOptions[self::tlsOptionCipher]; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if(true === array_key_exists(self::tlsOptionPinnedKey, $tlsOptions)) { |
|
| 65 | + if (true === array_key_exists(self::tlsOptionPinnedKey, $tlsOptions)) { |
|
| 66 | 66 | $this->tlsOptions[self::tlsOptionPinnedKey] = $tlsOptions[self::tlsOptionPinnedKey]; |
| 67 | 67 | } |
| 68 | 68 | } |
@@ -2,11 +2,11 @@ discard block |
||
| 2 | 2 | require_once("Salt/autoload.php"); |
| 3 | 3 | |
| 4 | 4 | //define possibly missing constants |
| 5 | -defined('CURL_SSLVERSION_DEFAULT') || define('CURL_SSLVERSION_DEFAULT', 0); |
|
| 6 | -defined('CURL_SSLVERSION_TLSv1') || define('CURL_SSLVERSION_TLSv1', 1); |
|
| 7 | -defined('CURL_SSLVERSION_TLSv1_1') || define('CURL_SSLVERSION_TLSv1_1', 5); |
|
| 8 | -defined('CURL_SSLVERSION_TLSv1_2') || define('CURL_SSLVERSION_TLSv1_2', 6); |
|
| 9 | -defined('CURLOPT_PINNEDPUBLICKEY') || define('CURLOPT_PINNEDPUBLICKEY', 10230); |
|
| 5 | +defined('CURL_SSLVERSION_DEFAULT') || define('CURL_SSLVERSION_DEFAULT', 0); |
|
| 6 | +defined('CURL_SSLVERSION_TLSv1') || define('CURL_SSLVERSION_TLSv1', 1); |
|
| 7 | +defined('CURL_SSLVERSION_TLSv1_1') || define('CURL_SSLVERSION_TLSv1_1', 5); |
|
| 8 | +defined('CURL_SSLVERSION_TLSv1_2') || define('CURL_SSLVERSION_TLSv1_2', 6); |
|
| 9 | +defined('CURLOPT_PINNEDPUBLICKEY') || define('CURLOPT_PINNEDPUBLICKEY', 10230); |
|
| 10 | 10 | |
| 11 | 11 | //define autoloader |
| 12 | 12 | $d = dirname(__FILE__); |
@@ -16,12 +16,12 @@ discard block |
||
| 16 | 16 | $fileName = ''; |
| 17 | 17 | if ($lastNsPos = strrpos($className, '\\')) { |
| 18 | 18 | $namespace = substr($className, 0, $lastNsPos); |
| 19 | - $className = substr($className, $lastNsPos + 1); |
|
| 20 | - $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
|
| 19 | + $className = substr($className, $lastNsPos+1); |
|
| 20 | + $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR; |
|
| 21 | 21 | } |
| 22 | - $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
| 22 | + $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php'; |
|
| 23 | 23 | |
| 24 | - if(true === file_exists( $d.'/'.$fileName)) { |
|
| 24 | + if (true === file_exists($d.'/'.$fileName)) { |
|
| 25 | 25 | require $d.'/'.$fileName; |
| 26 | 26 | } |
| 27 | 27 | }); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | define('MSGAPI_SDK_VERSION', $sdkVersion); |
| 31 | 31 | $cryptTool = Threema\MsgApi\Tools\CryptTool::getInstance(); |
| 32 | 32 | |
| 33 | -if(null === $cryptTool) { |
|
| 33 | +if (null === $cryptTool) { |
|
| 34 | 34 | throw new \Threema\Core\Exception("no supported crypt-tool"); |
| 35 | 35 | } |
| 36 | 36 | |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | * @param object $res |
| 77 | 77 | * @return SendE2EResult |
| 78 | 78 | */ |
| 79 | - public function parseResult($httpCode, $res){ |
|
| 79 | + public function parseResult($httpCode, $res) { |
|
| 80 | 80 | return new SendE2EResult($httpCode, $res); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | * @param object $res |
| 77 | 77 | * @return SendE2EResult |
| 78 | 78 | */ |
| 79 | - public function parseResult($httpCode, $res){ |
|
| 79 | + public function parseResult($httpCode, $res) { |
|
| 80 | 80 | return new SendE2EResult($httpCode, $res); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | * @param object $res |
| 77 | 77 | * @return SendE2EResult |
| 78 | 78 | */ |
| 79 | - public function parseResult($httpCode, $res){ |
|
| 79 | + public function parseResult($httpCode, $res) { |
|
| 80 | 80 | return new SendE2EResult($httpCode, $res); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | * @param object $res |
| 77 | 77 | * @return SendE2EResult |
| 78 | 78 | */ |
| 79 | - public function parseResult($httpCode, $res){ |
|
| 79 | + public function parseResult($httpCode, $res) { |
|
| 80 | 80 | return new SendE2EResult($httpCode, $res); |
| 81 | 81 | } |
| 82 | 82 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | * @param object $res |
| 77 | 77 | * @return SendE2EResult |
| 78 | 78 | */ |
| 79 | - public function parseResult($httpCode, $res){ |
|
| 79 | + public function parseResult($httpCode, $res) { |
|
| 80 | 80 | return new SendE2EResult($httpCode, $res); |
| 81 | 81 | } |
| 82 | 82 | } |