Skip to content

Commit

Permalink
Merge pull request #56 from namshi/PHP7-support
Browse files Browse the repository at this point in the history
Added PHP7 support
  • Loading branch information
cirpo committed Nov 13, 2015
2 parents 7bd69cc + e67510f commit 624261a
Show file tree
Hide file tree
Showing 24 changed files with 227 additions and 188 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"autoload": {
"psr-0": {
"Namshi\\JOSE": "src/"
"Namshi\\JOSE": "src/",
"Namshi\\JOSE\\Test": "test/"
}
},
"require": {
Expand Down
37 changes: 25 additions & 12 deletions src/Namshi/JOSE/JWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use InvalidArgumentException;
use Namshi\JOSE\Base64\Base64Encoder;
use Namshi\JOSE\Base64\Base64UrlSafeEncoder;
use Namshi\JOSE\Signer\SignerInterface;
use Namshi\JOSE\Base64\Encoder;
use Namshi\JOSE\Signer\SignerInterface;

/**
* Class representing a JSON Web Signature.
Expand All @@ -20,20 +20,27 @@ class JWS extends JWT
protected $supportedEncryptionEngines = array('OpenSSL', 'SecLib');

/**
* Constructor
* Constructor.
*
* @param array $header An associative array of headers. The value can be any type accepted by json_encode or a JSON serializable object
*
* @see http://php.net/manual/en/function.json-encode.php
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
* @see https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#section-4
*
* @param string $encryptionEngine
* }
* }
*/
public function __construct($header = array(), $encryptionEngine = "OpenSSL")
public function __construct($header = array(), $encryptionEngine = 'OpenSSL')
{
if (!in_array($encryptionEngine, $this->supportedEncryptionEngines)) {
throw new InvalidArgumentException(sprintf("Encryption engine %s is not supported", $encryptionEngine));
throw new InvalidArgumentException(sprintf('Encryption engine %s is not supported', $encryptionEngine));
}

if ('SecLib' === $encryptionEngine && version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
throw new InvalidArgumentException("phpseclib 1.0.0(LTS), even the latest 2.0.0, doesn't support PHP7 yet");
}

$this->encryptionEngine = $encryptionEngine;

parent::__construct(array(), $header);
Expand All @@ -42,8 +49,9 @@ public function __construct($header = array(), $encryptionEngine = "OpenSSL")
/**
* Signs the JWS signininput.
*
* @param resource $key
* @param resource $key
* @param optional string $password
*
* @return string
*/
public function sign($key, $password = null)
Expand All @@ -65,7 +73,7 @@ public function getSignature()
return $this->signature;
}

return null;
return;
}

/**
Expand All @@ -87,22 +95,24 @@ public function getTokenString()
{
$signinInput = $this->generateSigninInput();

return sprintf("%s.%s", $signinInput, $this->encoder->encode($this->getSignature()));
return sprintf('%s.%s', $signinInput, $this->encoder->encode($this->getSignature()));
}

/**
* Creates an instance of a JWS from a JWT.
*
* @param string $jwsTokenString
*
* @return JWS
*
* @throws \InvalidArgumentException
*/
public static function load($jwsTokenString, $allowUnsecure = false, Encoder $encoder = null, $encryptionEngine = 'OpenSSL')
{
if ($encoder === null) {
$encoder = strpbrk($jwsTokenString, '+/=') ? new Base64Encoder() : new Base64UrlSafeEncoder();
}

$parts = explode('.', $jwsTokenString);

if (count($parts) === 3) {
Expand Down Expand Up @@ -133,7 +143,8 @@ public static function load($jwsTokenString, $allowUnsecure = false, Encoder $en
* signature previously stored (@see JWS::load).
*
* @param resource|string $key
* @param string $algo The algorithms this JWS should be signed with. Use it if you want to restrict which algorithms you want to allow to be validated.
* @param string $algo The algorithms this JWS should be signed with. Use it if you want to restrict which algorithms you want to allow to be validated.
*
* @return bool
*/
public function verify($key, $algo = null)
Expand Down Expand Up @@ -161,20 +172,22 @@ public function getEncodedSignature()
/**
* Sets the base64 encoded signature.
*
* @param string $encodedSignature
* @param string $encodedSignature
*
* @return JWS
*/
public function setEncodedSignature($encodedSignature)
{
$this->encodedSignature = $encodedSignature;

return $this;
}

/**
* Returns the signer responsible to encrypting / decrypting this JWS.
*
* @return SignerInterface
*
* @throws \InvalidArgumentException
*/
protected function getSigner()
Expand Down
30 changes: 18 additions & 12 deletions src/Namshi/JOSE/Signer/OpenSSL/ECDSA.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Namshi\JOSE\Signer\OpenSSL;

use \File_ASN1;
use File_ASN1;

/**
* Class responsible to sign inputs with the a ECDSA algorithm, after hashing it.
*/
abstract class ECDSA extends PublicKey
{
public function __constuct()
{
if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
throw new \InvalidArgumentException("phpseclib 1.0.0(LTS), even the latest 2.0.0, doesn't support PHP7 yet");
}
}

/**
* @inheritdoc
* {@inheritdoc}
*/
protected function supportsKey($key)
{
Expand All @@ -26,8 +32,8 @@ protected function supportsKey($key)
'5.6' => '5.6.0',
);

if (isset($minVersions[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION]) &&
version_compare(PHP_VERSION, $minVersions[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION], '<')) {
if (isset($minVersions[PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION]) &&
version_compare(PHP_VERSION, $minVersions[PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION], '<')) {
return false;
}

Expand All @@ -37,9 +43,9 @@ protected function supportsKey($key)
return false;
}
$publicKey = trim($matches[1]);
$asn1 = new File_ASN1();
$asn1 = new File_ASN1();

/**
/*
* http://tools.ietf.org/html/rfc3279#section-2.2.3
* AlgorithmIdentifier ::= SEQUENCE {
* algorithm OBJECT IDENTIFIER,
Expand All @@ -51,7 +57,7 @@ protected function supportsKey($key)
*
*/
$asnAlgorithmIdentifier = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'type' => FILE_ASN1_TYPE_SEQUENCE,
'children' => array(
'ansi-X9-62' => array(
'type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER,
Expand All @@ -62,31 +68,31 @@ protected function supportsKey($key)
),
);

/**
/*
* http://tools.ietf.org/html/rfc5280#section-4.1
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING
* }
*/
$asnSubjectPublicKeyInfo = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'type' => FILE_ASN1_TYPE_SEQUENCE,
'children' => array(
'algorithm' => $asnAlgorithmIdentifier,
'algorithm' => $asnAlgorithmIdentifier,
'subjectPublicKey' => array(
'type' => FILE_ASN1_TYPE_BIT_STRING,
),
),
);

$decoded = $asn1->decodeBER(base64_decode($publicKey));
$decoded = $asn1->decodeBER(base64_decode($publicKey));
$mappedDetails = $asn1->asn1map($decoded[0], $asnSubjectPublicKeyInfo);

return isset($mappedDetails['algorithm']['id-ecSigType']) ? $this->getSupportedECDSACurve() === $mappedDetails['algorithm']['id-ecSigType'] : false;
}

/**
* @inheritdoc
* {@inheritdoc}
*/
protected function getSupportedPrivateKeyType()
{
Expand Down
15 changes: 7 additions & 8 deletions tests/Namshi/JOSE/Test/BCJWSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* BC test for base64 url-safe fix
* Test that tokens generated the old way (non url-safe) will work with url-safe base64 decoding
* Test that tokens generated the old way (non url-safe) will work with url-safe base64 decoding.
*/
class BCJWSTest extends TestCase
{
Expand All @@ -17,22 +17,21 @@ class BCJWSTest extends TestCase
public function testTestBC()
{
$data = array(
array("order_nr" => "ae123123"),
array("username" => "asdasdasd"),
array("anything" => "!@#$%^&*()_+")
array('order_nr' => 'ae123123'),
array('username' => 'asdasdasd'),
array('anything' => '!@#$%^&*()_+'),
);

foreach ($data as $payload) {
$jwsOld = new JWS(array("alg" => "RS256"));
$jwsOld = new JWS(array('alg' => 'RS256'));
$jwsOld->setEncoder(new Base64Encoder());
$jwsOld->setPayload($payload);
$jwsOld->sign(openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE));
$jwsOld->sign(openssl_pkey_get_private(SSL_KEYS_PATH.'private.key', self::SSL_KEY_PASSPHRASE));

$t = $jwsOld->getTokenString();

$jwsNew = JWS::load($t);
$this->assertTrue($jwsNew->verify(openssl_pkey_get_public(SSL_KEYS_PATH . "public.key")));
$this->assertTrue($jwsNew->verify(openssl_pkey_get_public(SSL_KEYS_PATH.'public.key')));
}
}

}

0 comments on commit 624261a

Please sign in to comment.