Skip to content

Commit

Permalink
Prevent resource leaks. See #32
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Feb 19, 2017
1 parent bad8bd7 commit 1fad40d
Showing 1 changed file with 69 additions and 38 deletions.
107 changes: 69 additions & 38 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ public static function box($inputFile, $outputFile, $nonce, $keyPair)
throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size) || !is_resource($ifp)) {
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -101,18 +105,22 @@ public static function box_open($inputFile, $outputFile, $nonce, $ecdhKeypair)
throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size) || !is_resource($ifp)) {
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -156,18 +164,22 @@ public static function box_seal($inputFile, $outputFile, $publicKey)
throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size) || !is_resource($ifp)) {
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -250,18 +262,22 @@ public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair)

$publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size) || !is_resource($ifp)) {
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -327,18 +343,22 @@ public static function secretbox($inputFile, $outputFile, $nonce, $key)
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size) || !is_resource($ifp)) {
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -385,19 +405,22 @@ public static function secretbox_open($inputFile, $outputFile, $nonce, $key)
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
}

/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');

/** @var int $size */
$size = filesize($inputFile);
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

if (!is_int($size) || !is_resource($ifp)) {
/** @var resource $ifp */
$ifp = fopen($inputFile, 'rb');
if (!is_resource($ifp)) {
throw new Error('Could not open input file for reading');
}

/** @var resource $ofp */
$ofp = fopen($outputFile, 'wb');
if (!is_resource($ofp)) {
fclose($ifp);
throw new Error('Could not open output file for writing');
}

Expand Down Expand Up @@ -436,13 +459,16 @@ public static function sign($filePath, $secretKey)
throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');
}

/** @var resource $fp */
$fp = fopen($filePath, 'rb');

/** @var int $size */
$size = filesize($filePath);
if (!is_int($size) || !is_resource($fp)) {
throw new Error('Could not open file for reading');
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$fp = fopen($filePath, 'rb');
if (!is_resource($fp)) {
throw new Error('Could not open input file for reading');
}

/** @var string $az */
Expand Down Expand Up @@ -527,18 +553,10 @@ public static function verify($sig, $filePath, $publicKey)
if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) {
throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
}

/** @var resource $fp */
$fp = fopen($filePath, 'rb');

/** @var int $size */
$size = filesize($filePath);
if (!is_int($size) || !is_resource($fp)) {
throw new Error('Could not open file for reading');
}
if (self::strlen($sig) < 64) {
throw new Exception('Signature is too short');
}

if (ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
throw new Exception('S < L - Invalid signature');
}
Expand All @@ -556,6 +574,19 @@ public static function verify($sig, $filePath, $publicKey)
throw new Exception('All zero public key');
}


/** @var int $size */
$size = filesize($filePath);
if (!is_int($size)) {
throw new Error('Could not obtain the file size');
}

/** @var resource $ifp */
$fp = fopen($filePath, 'rb');
if (!is_resource($fp)) {
throw new Error('Could not open input file for reading');
}

/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
$orig = ParagonIE_Sodium_Compat::$fastMult;

Expand Down

0 comments on commit 1fad40d

Please sign in to comment.