Skip to content

Commit

Permalink
Merge ed76c28 into 77a4f89
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Jan 25, 2018
2 parents 77a4f89 + ed76c28 commit 8df4ae9
Show file tree
Hide file tree
Showing 27 changed files with 486 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/Alerts/CannotCloneKey.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class CannotCloneKey extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/CannotPerformOperation.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class CannotPerformOperation extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/CannotSerializeKey.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class CannotSerializeKey extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/ConfigDirectiveNotFound.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class ConfigDirectiveNotFound extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/FileAccessDenied.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class FileAccessDenied extends FileError
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/FileError.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class FileError extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/FileModified.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class FileModified extends FileError
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/HaliteAlert.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class HaliteAlert extends \Exception
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidDigestLength.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidDigestLength extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidFlags.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidFlags extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidKey.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidKey extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidMessage.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidMessage extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidSalt.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidSalt extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidSignature.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidSignature extends HaliteAlert
{
Expand Down
1 change: 1 addition & 0 deletions src/Alerts/InvalidType.php
Expand Up @@ -9,6 +9,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* @codeCoverageIgnore
*/
class InvalidType extends HaliteAlert
{
Expand Down
2 changes: 2 additions & 0 deletions src/Cookie.php
Expand Up @@ -35,6 +35,8 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @codeCoverageIgnore
*/
final class Cookie
{
Expand Down
3 changes: 3 additions & 0 deletions src/KeyPair.php
Expand Up @@ -39,6 +39,7 @@ class KeyPair
* Hide this from var_dump(), etc.
*
* @return array
* @codeCoverageIgnore
*/
public function __debugInfo()
{
Expand All @@ -52,6 +53,7 @@ public function __debugInfo()
* Get a Key object for the public key
*
* @return PublicKey
* @codeCoverageIgnore
*/
public function getPublicKey()
{
Expand All @@ -62,6 +64,7 @@ public function getPublicKey()
* Get a Key object for the secret key
*
* @return SecretKey
* @codeCoverageIgnore
*/
public function getSecretKey()
{
Expand Down
8 changes: 3 additions & 5 deletions src/SignatureKeyPair.php
Expand Up @@ -42,12 +42,11 @@ final class SignatureKeyPair extends KeyPair

/**
* Pass it a secret key, it will automatically generate a public key
*
*
* @param array<int, Key> $keys
*
* @throws CannotPerformOperation
* @throws InvalidKey
* @throws InvalidType
* @throws \TypeError
*/
public function __construct(Key ...$keys)
{
Expand Down Expand Up @@ -125,8 +124,7 @@ public function __construct(Key ...$keys)
* @param SignatureSecretKey $secret
* @return void
*
* @throws CannotPerformOperation
* @throws InvalidType
* @throws \TypeError
*/
protected function setupKeyPair(SignatureSecretKey $secret): void
{
Expand Down
20 changes: 20 additions & 0 deletions src/Stream/MutableFile.php
Expand Up @@ -28,6 +28,7 @@
*/
class MutableFile implements StreamInterface
{
const ALLOWED_MODES = ['r+', 'r+b', 'wb', 'w+', 'w+b', 'c', 'c+', 'cb', 'c+b'];
const CHUNK = 8192; // PHP's fread() buffer is set to 8192 by default

/**
Expand Down Expand Up @@ -59,17 +60,36 @@ class MutableFile implements StreamInterface
public function __construct($file)
{
if (\is_string($file)) {
if (!\is_readable($file)) {
throw new FileAccessDenied(
'Could not open file for reading'
);
}
if (!\is_writable($file)) {
throw new FileAccessDenied(
'Could not open file for writing'
);
}
$fp = \fopen($file, 'wb');
// @codeCoverageIgnoreStart
if (!\is_resource($fp)) {
throw new FileAccessDenied(
'Could not open file for reading'
);
}
// @codeCoverageIgnoreEnd
$this->fp = $fp;
$this->closeAfter = true;
$this->pos = 0;
$this->stat = \fstat($this->fp);
} elseif (\is_resource($file)) {
/** @var array<string, string> $metadata */
$metadata = \stream_get_meta_data($file);
if (!\in_array($metadata['mode'], self::ALLOWED_MODES, true)) {
throw new FileAccessDenied(
'Resource is in ' . $metadata['mode'] . ' mode, which is not allowed.'
);
}
$this->fp = $file;
$this->pos = \ftell($this->fp);
$this->stat = \fstat($this->fp);
Expand Down
29 changes: 28 additions & 1 deletion src/Stream/ReadOnlyFile.php
Expand Up @@ -29,6 +29,7 @@
*/
class ReadOnlyFile implements StreamInterface
{
const ALLOWED_MODES = ['rb'];
const CHUNK = 8192; // PHP's fread() buffer is set to 8192 by default

/**
Expand Down Expand Up @@ -75,18 +76,32 @@ class ReadOnlyFile implements StreamInterface
public function __construct($file, Key $key = null)
{
if (\is_string($file)) {
if (!\is_readable($file)) {
throw new FileAccessDenied(
'Could not open file for reading'
);
}
$fp = \fopen($file, 'rb');
// @codeCoverageIgnoreStart
if (!\is_resource($fp)) {
throw new FileAccessDenied(
'Could not open file for reading'
);
}
// @codeCoverageIgnoreEnd
$this->fp = $fp;

$this->closeAfter = true;
$this->pos = 0;
$this->stat = \fstat($this->fp);
} elseif (\is_resource($file)) {
/** @var array<string, string> $metadata */
$metadata = \stream_get_meta_data($file);
if (!\in_array($metadata['mode'], self::ALLOWED_MODES, true)) {
throw new FileAccessDenied(
'Resource is in ' . $metadata['mode'] . ' mode, which is not allowed.'
);
}
$this->fp = $file;
$this->pos = \ftell($this->fp);
$this->stat = \fstat($this->fp);
Expand All @@ -95,9 +110,11 @@ public function __construct($file, Key $key = null)
'Argument 1: Expected a filename or resource'
);
}
$this->hashKey = !empty($key)
// @codeCoverageIgnoreStart
$this->hashKey = !empty($key)
? $key->getRawKeyMaterial()
: '';
// @codeCoverageIgnoreEnd
$this->hash = $this->getHash();
}

Expand Down Expand Up @@ -145,7 +162,9 @@ public function getHash(): string
$c = \fread($this->fp, self::CHUNK);
}
if (!\is_string($c)) {
// @codeCoverageIgnoreStart
throw new FileError('Could not read file');
// @codeCoverageIgnoreEnd
}
\sodium_crypto_generichash_update($h, $c);
}
Expand Down Expand Up @@ -206,14 +225,18 @@ public function readBytes(int $num, bool $skipTests = false): string
}
do {
if ($remaining <= 0) {
// @codeCoverageIgnoreStart
break;
// @codeCoverageIgnoreEnd
}
/** @var string $read */
$read = \fread($this->fp, $remaining);
if (!\is_string($read)) {
// @codeCoverageIgnoreStart
throw new FileAccessDenied(
'Could not read from the file'
);
// @codeCoverageIgnoreEnd
}
$buf .= $read;
$readSize = Binary::safeStrlen($read);
Expand Down Expand Up @@ -250,9 +273,11 @@ public function reset(int $position = 0): bool
if (\fseek($this->fp, $position, SEEK_SET) === 0) {
return true;
}
// @codeCoverageIgnoreStart
throw new CannotPerformOperation(
'fseek() failed'
);
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -266,9 +291,11 @@ public function reset(int $position = 0): bool
public function toctouTest()
{
if (\ftell($this->fp) !== $this->pos) {
// @codeCoverageIgnoreStart
throw new FileModified(
'Read-only file has been modified since it was opened for reading'
);
// @codeCoverageIgnoreEnd
}
$stat = \fstat($this->fp);
if ($stat['size'] !== $this->stat['size']) {
Expand Down
7 changes: 7 additions & 0 deletions src/Util.php
Expand Up @@ -33,6 +33,7 @@ final class Util
/**
* Don't allow this to be instantiated.
* @throws \Error
* @codeCoverageIgnore
*/
final private function __construct()
{
Expand Down Expand Up @@ -64,6 +65,7 @@ public static function chrToInt(string $chr): int
* @param int $length
* @return string
* @throws CannotPerformOperation
* @throws \TypeError
*/
public static function hash(
string $input,
Expand Down Expand Up @@ -124,7 +126,9 @@ public static function hkdfBlake2b(
}
// "If [salt] not provided, is set to a string of HashLen zeroes."
if (empty($salt)) {
// @codeCoverageIgnoreStart
$salt = \str_repeat("\x00", \SODIUM_CRYPTO_GENERICHASH_KEYBYTES);
// @codeCoverageIgnoreEnd
}

// HKDF-Extract:
Expand Down Expand Up @@ -155,11 +159,13 @@ public static function hkdfBlake2b(
/** @var string $orm */
$orm = Binary::safeSubstr($t, 0, $length);

// @codeCoverageIgnoreStart
if (!\is_string($orm)) {
throw new CannotPerformOperation(
'An unknown error has occurred'
);
}
// @codeCoverageIgnoreEnd
return $orm;
}

Expand Down Expand Up @@ -203,6 +209,7 @@ public static function intToChr(int $int): string
* @param int $length
* @return string
* @throws CannotPerformOperation
* @throws \TypeError
*/
public static function keyed_hash(
string $input,
Expand Down

0 comments on commit 8df4ae9

Please sign in to comment.