Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Tests/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,34 @@ public function testGetKeyType()
{
$this->assertSame($this->type, $this->object->getType());
}

/**
* @testdox Debugging output does not contain the key material
*/
public function testDebugOutputIsRedacted()
{
$key = new Key('test', 'secret-private-material', 'secret-public-material');

ob_start();
var_dump($key);
$dump = ob_get_clean();

$printed = print_r($key, true);

foreach ([$dump, $printed] as $output) {
$this->assertStringContainsString('test', $output);
$this->assertStringNotContainsString('secret-', $output);
}
}

/**
* @testdox The private key is not exposed in stack traces
*/
public function testPrivateKeyIsSensitiveParameter()
{
$parameter = (new \ReflectionMethod(Key::class, '__construct'))->getParameters()[1];

$this->assertSame('private', $parameter->getName());
$this->assertCount(1, $parameter->getAttributes(\SensitiveParameter::class));
}
}
62 changes: 62 additions & 0 deletions Tests/SensitiveParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Crypt\Tests;

use Joomla\Crypt\Cipher\Crypto;
use Joomla\Crypt\Cipher\OpenSSL;
use Joomla\Crypt\Cipher\Sodium;
use Joomla\Crypt\Crypt;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Checks that parameters carrying key material are marked with #[\SensitiveParameter].
*/
class SensitiveParameterTest extends TestCase
{
/**
* Parameters that carry key material
*
* @return array
*/
public static function dataSensitiveParameters(): array
{
return [
'Crypto::encrypt()' => [Crypto::class, 'encrypt', 'key'],
'Crypto::decrypt()' => [Crypto::class, 'decrypt', 'key'],
'OpenSSL::encrypt()' => [OpenSSL::class, 'encrypt', 'key'],
'OpenSSL::decrypt()' => [OpenSSL::class, 'decrypt', 'key'],
'OpenSSL::generateKey()' => [OpenSSL::class, 'generateKey', 'options'],
'Sodium::encrypt()' => [Sodium::class, 'encrypt', 'key'],
'Sodium::decrypt()' => [Sodium::class, 'decrypt', 'key'],
'Crypt::__construct()' => [Crypt::class, '__construct', 'key'],
'Crypt::setKey()' => [Crypt::class, 'setKey', 'key'],
];
}

/**
* @testdox Key material is marked as sensitive so it is hidden in stack traces
*
* @param string $class The class name
* @param string $method The method name
* @param string $parameter The parameter name
*/
#[DataProvider('dataSensitiveParameters')]
public function testParameterIsSensitive(string $class, string $method, string $parameter)
{
foreach ((new \ReflectionMethod($class, $method))->getParameters() as $reflection) {
if ($reflection->getName() === $parameter) {
$this->assertCount(1, $reflection->getAttributes(\SensitiveParameter::class));

return;
}
}

$this->fail("$class::$method() has no parameter \$$parameter");
}
}
12 changes: 12 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ $b = $cipher->generateKey(['passphrase' => '/var/www/keys/tenant-b.dat']);
If you must keep this cipher, pass the secret itself rather than a path, make sure it is at least
as long as the method's key size, and generate it with `random_bytes()`.

## Keeping key material out of logs

`Key` hides its private and public parts from `var_dump()`, `print_r()` and other debugging output
(`__debugInfo()` shows only the type). The parameters that carry key material — the `Key`
constructor's `$private`, the `$key` of `Crypt::__construct()`, `Crypt::setKey()` and the ciphers'
`encrypt()`/`decrypt()`, and the `$options` of `OpenSSL::generateKey()` — are marked
`#[\SensitiveParameter]`, so PHP replaces them in stack traces.

A `Key` can still be serialized, and the result contains the private key in clear text. Do not put
`Key` objects into sessions, caches or anything else that is serialized; store the private key in
your secret storage and rebuild the `Key` from it.

## Not part of this package

* No password hashing — that lives in `joomla/authentication` (`Password\BCryptHandler` and the
Expand Down
4 changes: 2 additions & 2 deletions src/Cipher/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Crypto implements CipherInterface
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
public function decrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'crypto') {
Expand Down Expand Up @@ -69,7 +69,7 @@ public function decrypt($data, Key $key)
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
public function encrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'crypto') {
Expand Down
6 changes: 3 additions & 3 deletions src/Cipher/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(string $iv, string $method)
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
public function decrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'openssl') {
Expand Down Expand Up @@ -93,7 +93,7 @@ public function decrypt($data, Key $key)
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
public function encrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'openssl') {
Expand All @@ -119,7 +119,7 @@ public function encrypt($data, Key $key)
* @since 2.0.0
* @throws InvalidKeyException if the key cannot be generated
*/
public function generateKey(array $options = [])
public function generateKey(#[\SensitiveParameter] array $options = [])
{
$passphrase = $options['passphrase'] ?? false;

Expand Down
4 changes: 2 additions & 2 deletions src/Cipher/Sodium.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Sodium implements CipherInterface
* @throws DecryptionException if the data cannot be decrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function decrypt($data, Key $key)
public function decrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'sodium') {
Expand Down Expand Up @@ -85,7 +85,7 @@ public function decrypt($data, Key $key)
* @throws EncryptionException if the data cannot be encrypted
* @throws InvalidKeyTypeException if the key is not valid for the cipher
*/
public function encrypt($data, Key $key)
public function encrypt($data, #[\SensitiveParameter] Key $key)
{
// Validate key.
if ($key->getType() !== 'sodium') {
Expand Down
4 changes: 2 additions & 2 deletions src/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Crypt
*
* @since 1.0
*/
public function __construct(?CipherInterface $cipher = null, ?Key $key = null)
public function __construct(?CipherInterface $cipher = null, #[\SensitiveParameter] ?Key $key = null)
{
// Set the encryption cipher.
$this->cipher = $cipher ?: new Crypto();
Expand Down Expand Up @@ -116,7 +116,7 @@ public function generateKey(array $options = [])
*
* @since 1.0
*/
public function setKey(Key $key)
public function setKey(#[\SensitiveParameter] Key $key)
{
$this->key = $key;

Expand Down
21 changes: 20 additions & 1 deletion src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Key
*
* @since 1.0
*/
public function __construct(string $type, string $private, string $public)
public function __construct(string $type, #[\SensitiveParameter] string $private, string $public)
{
// Set the key type.
$this->type = $type;
Expand All @@ -59,6 +59,25 @@ public function __construct(string $type, string $private, string $public)
$this->public = $public;
}

/**
* Hide the key material from var_dump(), print_r() and similar debugging output.
*
* The public part is hidden as well: for a symmetric cipher it may be secret, as it is for keys generated by
* Cipher\Crypto before 4.1.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function __debugInfo(): array
{
return [
'type' => $this->type,
'private' => '*** redacted ***',
'public' => '*** redacted ***',
];
}

/**
* Retrieve the private key
*
Expand Down
Loading