Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SimpleJWT\Crypt namespace, JWT and JWE #60

Merged
merged 7 commits into from
Mar 6, 2022
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## Unreleased

- Changed: Split `SimpleJWT\Crypt` namespace into multiple namespaces, one
for each algorithm type (#60)
- Changed: `JWT` and `JWE` now derives from a common parent class `Token`
- Deprecated: Helper::getObject() and Helper::getJWTObject() have been
replaced by Helper::decode() and Helper::decodeFully() respectively,
and will be removed in future versions
Expand Down
36 changes: 18 additions & 18 deletions src/SimpleJWT/Crypt/AlgorithmFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ class AlgorithmFactory {
/** @var array<string, string> $alg_map */
static $alg_map = [
// Signature algorithms
'/^ES\d+$/' => 'SimpleJWT\Crypt\OpenSSLSig',
'/^ES256K$/' => 'SimpleJWT\Crypt\OpenSSLSig',
'/^RS\d+$/' => 'SimpleJWT\Crypt\OpenSSLSig',
'/^HS\d+$/' => 'SimpleJWT\Crypt\HMAC',
'/^ES\d+$/' => 'SimpleJWT\Crypt\Signature\OpenSSLSig',
'/^ES256K$/' => 'SimpleJWT\Crypt\Signature\OpenSSLSig',
'/^RS\d+$/' => 'SimpleJWT\Crypt\Signature\OpenSSLSig',
'/^HS\d+$/' => 'SimpleJWT\Crypt\Signature\HMAC',

// Key management algorithms (derivation or encryption)
'/^dir$/' => 'SimpleJWT\Crypt\DirectEncryption',
'/^RSA1_5$/' => 'SimpleJWT\Crypt\RSAES',
'/^RSA-OAEP$/' => 'SimpleJWT\Crypt\RSAES',
'/^RSA-OAEP-256$/' => 'SimpleJWT\Crypt\RSAES',
'/^A\d+KW$/' => 'SimpleJWT\Crypt\AESKeyWrap',
'/^PBES2-HS\d+\\+A\d+KW$/' => 'SimpleJWT\Crypt\PBES2',
'/^ECDH-ES$/' => 'SimpleJWT\Crypt\ECDH',
'/^ECDH-ES\\+A\d+KW$/' => 'SimpleJWT\Crypt\ECDH_AESKeyWrap',
'/^dir$/' => 'SimpleJWT\Crypt\KeyManagement\DirectEncryption',
'/^RSA1_5$/' => 'SimpleJWT\Crypt\KeyManagement\RSAES',
'/^RSA-OAEP$/' => 'SimpleJWT\Crypt\KeyManagement\RSAES',
'/^RSA-OAEP-256$/' => 'SimpleJWT\Crypt\KeyManagement\RSAES',
'/^A\d+KW$/' => 'SimpleJWT\Crypt\KeyManagement\AESKeyWrap',
'/^PBES2-HS\d+\\+A\d+KW$/' => 'SimpleJWT\Crypt\KeyManagement\PBES2',
'/^ECDH-ES$/' => 'SimpleJWT\Crypt\KeyManagement\ECDH',
'/^ECDH-ES\\+A\d+KW$/' => 'SimpleJWT\Crypt\KeyManagement\ECDH_AESKeyWrap',

// Content encryption algorithms
'/^A\d+CBC-HS\d+$/' => 'SimpleJWT\Crypt\AESCBC_HMACSHA2',
'/^A\d+GCM$/' => 'SimpleJWT\Crypt\AESGCM'
'/^A\d+CBC-HS\d+$/' => 'SimpleJWT\Crypt\Encryption\AESCBC_HMACSHA2',
'/^A\d+GCM$/' => 'SimpleJWT\Crypt\Encryption\AESGCM'
];

/** @var array<string, string> $use_map */
private static $use_map = [
Algorithm::SIGNATURE_ALGORITHM => 'SimpleJWT\Crypt\SignatureAlgorithm',
Algorithm::ENCRYPTION_ALGORITHM => 'SimpleJWT\Crypt\EncryptionAlgorithm',
Algorithm::KEY_ALGORITHM => 'SimpleJWT\Crypt\KeyManagementAlgorithm'
Algorithm::SIGNATURE_ALGORITHM => 'SimpleJWT\Crypt\Signature\SignatureAlgorithm',
Algorithm::ENCRYPTION_ALGORITHM => 'SimpleJWT\Crypt\Encryption\EncryptionAlgorithm',
Algorithm::KEY_ALGORITHM => 'SimpleJWT\Crypt\KeyManagement\KeyManagementAlgorithm'
];

/**
Expand Down Expand Up @@ -141,7 +141,7 @@ static public function getSupportedAlgs($use) {
* @return void
*/
static public function addNoneAlg() {
self::$alg_map['/^none$/'] = 'SimpleJWT\Crypt\None';
self::$alg_map['/^none$/'] = 'SimpleJWT\Crypt\Signature\None';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Encryption;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Util\Util;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Encryption;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Util\Util;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Encryption;

use SimpleJWT\Crypt\CryptException;

/**
* Interface for content authenticated encryption algorithms.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Util\Util;
use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\SymmetricKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\KeySet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Util\Util;
use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\SymmetricKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\AlgorithmFactory;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\ECKey;
use SimpleJWT\Keys\KeyFactory;
use SimpleJWT\Util\Util;
Expand Down Expand Up @@ -93,7 +96,7 @@ public function deriveKey($keys, &$headers, $kid = null) {
// 1. Get the required key length and alg input into Concat KDF
if (isset($headers['enc'])) {
try {
/** @var \SimpleJWT\Crypt\EncryptionAlgorithm $enc */
/** @var \SimpleJWT\Crypt\Encryption\EncryptionAlgorithm $enc */
$enc = AlgorithmFactory::create($headers['enc'], Algorithm::ENCRYPTION_ALGORITHM);
$size = $enc->getCEKSize();
} catch (\UnexpectedValueException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Keys\KeySet;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\KeySet;
use SimpleJWT\Keys\KeyException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\KeySet;
use SimpleJWT\Keys\KeyException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

/**
* Interface for key management algorithms. These can be *key encryption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Util\Util;
use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\KeySet;
use SimpleJWT\Keys\SymmetricKey;
use SimpleJWT\Util\Util;

/**
* Implements PBES2 key encryption algorithm with AES key wrap.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\KeyManagement;

use SimpleJWT\Util\Util;
use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\Key;
use SimpleJWT\Util\Util;

/**
* Implementation of the RSA Encryption Scheme algorithms, including `RSA1_5` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Signature;

use SimpleJWT\Keys\KeyException;
use SimpleJWT\Util\Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Signature;

use SimpleJWT\Crypt\Algorithm;

/**
* Implements the `none` signature algorithm.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Signature;

use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\KeyException;
use SimpleJWT\Util\ASN1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Signature;

use SimpleJWT\Crypt\Algorithm;
use SimpleJWT\Util\Util;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SimpleJWT\Crypt;
namespace SimpleJWT\Crypt\Signature;

use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Keys\Key;
use SimpleJWT\Keys\KeySet;
use SimpleJWT\Keys\KeyException;
Expand Down
36 changes: 6 additions & 30 deletions src/SimpleJWT/JWE.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,14 @@

use SimpleJWT\Crypt\AlgorithmFactory;
use SimpleJWT\Crypt\CryptException;
use SimpleJWT\Crypt\KeyDerivationAlgorithm;
use SimpleJWT\Crypt\KeyEncryptionAlgorithm;
use SimpleJWT\Crypt\KeyManagement\KeyDerivationAlgorithm;
use SimpleJWT\Crypt\KeyManagement\KeyEncryptionAlgorithm;
use SimpleJWT\Keys\SymmetricKey;
use SimpleJWT\Keys\KeyException;
use SimpleJWT\Util\Helper;
use SimpleJWT\Util\Util;

class JWE {
/** @var string COMPACT_FORMAT Compact JWE serialisation format */
const COMPACT_FORMAT = Helper::COMPACT_FORMAT;
/** @var string JSON_FORMAT JSON JWE serialisation format */
const JSON_FORMAT = Helper::JSON_FORMAT;

class JWE extends Token {
/** @var array<string, mixed> $headers */
protected $headers = ['typ' => 'JWE'];

Expand All @@ -62,7 +57,7 @@ class JWE {
* @param string $plaintext the plaintext to encrypt
*/
public function __construct($headers, $plaintext) {
$this->headers = $headers;
parent::__construct($headers);
$this->plaintext = $plaintext;
}

Expand Down Expand Up @@ -138,7 +133,7 @@ public static function decrypt($token, $keys, $expected_alg) {
if ($headers['alg'] != $expected_alg) throw new InvalidTokenException('Unexpected algorithm', InvalidTokenException::DECRYPTION_ERROR);
$key_enc = AlgorithmFactory::create($headers['alg']);

/** @var \SimpleJWT\Crypt\EncryptionAlgorithm $content_enc */
/** @var \SimpleJWT\Crypt\Encryption\EncryptionAlgorithm $content_enc */
$content_enc = AlgorithmFactory::create($headers['enc']);

if ($key_enc instanceof KeyDerivationAlgorithm) {
Expand Down Expand Up @@ -203,25 +198,6 @@ public static function decrypt($token, $keys, $expected_alg) {
return new JWE($headers, $plaintext);
}

/**
* Returns the JWE's headers.
*
* @return array<string, mixed> the headers
*/
public function getHeaders() {
return $this->headers;
}

/**
* Returns a specified header
*
* @param string $header the header to return
* @return mixed the header value
*/
public function getHeader($header) {
return $this->headers[$header];
}

/**
* Returns the JWE's plaintext
*
Expand Down Expand Up @@ -250,7 +226,7 @@ public function encrypt($keys, $kid = null, $format = self::COMPACT_FORMAT) {

$key_enc = AlgorithmFactory::create($this->headers['alg']);

/** @var \SimpleJWT\Crypt\EncryptionAlgorithm $content_enc */
/** @var \SimpleJWT\Crypt\Encryption\EncryptionAlgorithm $content_enc */
$content_enc = AlgorithmFactory::create($this->headers['enc']);

if ($kid != null) $this->headers['kid'] = $kid;
Expand Down
Loading