Update of /cvsroot/php-blog/additional_plugins/serendipity_event_browserid/vendor/lcobucci/jwt/src/Signer
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv18870/serendipity_event_browserid/vendor/lcobucci/jwt/src/Signer
Added Files:
BaseSigner.php Ecdsa.php Hmac.php Key.php Keychain.php Rsa.php
Log Message:
gitclone.sh autocommit
--- NEW FILE: Rsa.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use InvalidArgumentException;
/**
* Base class for RSASSA-PKCS1 signers
*
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 2.1.0
*/
abstract class Rsa extends BaseSigner
{
/**
* {@inheritdoc}
*/
public function createHash($payload, Key $key)
{
$key = openssl_get_privatekey($key->getContent(), $key->getPassphrase());
$this->validateKey($key);
$signature = '';
if (!openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
throw new InvalidArgumentException(
'There was an error while creating the signature: ' . openssl_error_string()
);
}
return $signature;
}
/**
* {@inheritdoc}
*/
public function doVerify($expected, $payload, Key $key)
{
$key = openssl_get_publickey($key->getContent());
$this->validateKey($key);
return openssl_verify($payload, $expected, $key, $this->getAlgorithm()) === 1;
}
/**
* Validates if the given key is a valid RSA public/private key
*
* @param resource $key
*
* @throws InvalidArgumentException
*/
private function validateKey($key)
{
if ($key === false) {
throw new InvalidArgumentException(
'It was not possible to parse your key, reason: ' . openssl_error_string()
);
}
$details = openssl_pkey_get_details($key);
if (!isset($details['key']) || $details['type'] !== OPENSSL_KEYTYPE_RSA) {
throw new InvalidArgumentException('This key is not compatible with RSA signatures');
}
}
/**
* Returns the algorithm name
*
* @return string
*/
abstract public function getAlgorithm();
}
--- NEW FILE: Hmac.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
/**
* Base class for hmac signers
*
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 0.1.0
*/
abstract class Hmac extends BaseSigner
{
/**
* {@inheritdoc}
*/
public function createHash($payload, Key $key)
{
return hash_hmac($this->getAlgorithm(), $payload, $key->getContent(), true);
}
/**
* {@inheritdoc}
*/
public function doVerify($expected, $payload, Key $key)
{
if (!is_string($expected)) {
return false;
}
$callback = function_exists('hash_equals') ? 'hash_equals' : [$this, 'hashEquals'];
return call_user_func($callback, $expected, $this->createHash($payload, $key));
}
/**
* PHP < 5.6 timing attack safe hash comparison
*
* @param string $expected
* @param string $generated
*
* @return boolean
*/
public function hashEquals($expected, $generated)
{
$expectedLength = strlen($expected);
if ($expectedLength !== strlen($generated)) {
return false;
}
$res = 0;
for ($i = 0; $i < $expectedLength; ++$i) {
$res |= ord($expected[$i]) ^ ord($generated[$i]);
}
return $res === 0;
}
/**
* Returns the algorithm name
*
* @return string
*/
abstract public function getAlgorithm();
}
--- NEW FILE: Ecdsa.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
use Mdanter\Ecc\Crypto\Signature\Signature;
use Mdanter\Ecc\Crypto\Signature\Signer;
use Mdanter\Ecc\EccFactory;
use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
use Mdanter\Ecc\Random\RandomGeneratorFactory;
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
/**
* Base class for ECDSA signers
*
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 2.1.0
*/
abstract class Ecdsa extends BaseSigner
{
/**
* @var Adapter
*/
private $adapter;
/**
* @var Signer
*/
private $signer;
/**
* @var KeyParser
*/
private $parser;
/**
* @param Adapter $adapter
* @param EcdsaSigner $signer
* @param KeyParser $parser
*/
public function __construct(Adapter $adapter = null, Signer $signer = null, KeyParser $parser = null)
{
$this->adapter = $adapter ?: EccFactory::getAdapter();
$this->signer = $signer ?: EccFactory::getSigner($this->adapter);
$this->parser = $parser ?: new KeyParser($this->adapter);
}
/**
* {@inheritdoc}
*/
public function createHash(
$payload,
Key $key,
RandomNumberGeneratorInterface $generator = null
) {
$privateKey = $this->parser->getPrivateKey($key);
$generator = $generator ?: RandomGeneratorFactory::getRandomGenerator();
return $this->createSignatureHash(
$this->signer->sign(
$privateKey,
$this->createSigningHash($payload),
$generator->generate($privateKey->getPoint()->getOrder())
)
);
}
/**
* Creates a binary signature with R and S coordinates
*
* @param Signature $signature
*
* @return string
*/
private function createSignatureHash(Signature $signature)
{
$length = $this->getSignatureLength();
return pack(
'H*',
sprintf(
'%s%s',
str_pad($this->adapter->decHex($signature->getR()), $length, '0', STR_PAD_LEFT),
str_pad($this->adapter->decHex($signature->getS()), $length, '0', STR_PAD_LEFT)
)
);
}
/**
* Creates a hash using the signer algorithm with given payload
*
* @param string $payload
*
* @return int|string
*/
private function createSigningHash($payload)
{
return $this->adapter->hexDec(hash($this->getAlgorithm(), $payload));
}
/**
* {@inheritdoc}
*/
public function doVerify($expected, $payload, Key $key)
{
return $this->signer->verify(
$this->parser->getPublicKey($key),
$this->extractSignature($expected),
$this->createSigningHash($payload)
);
}
/**
* Extracts R and S values from given data
*
* @param string $value
*
* @return \Mdanter\Ecc\Crypto\Signature\Signature
*/
private function extractSignature($value)
{
$length = $this->getSignatureLength();
$value = unpack('H*', $value)[1];
return new Signature(
$this->adapter->hexDec(substr($value, 0, $length)),
$this->adapter->hexDec(substr($value, $length))
);
}
/**
* Returns the lenght of signature parts
*
* @return int
*/
abstract public function getSignatureLength();
/**
* Returns the name of algorithm to be used to create the signing hash
*
* @return string
*/
abstract public function getAlgorithm();
}
--- NEW FILE: Key.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use InvalidArgumentException;
/**
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 3.0.4
*/
final class Key
{
/**
* @var string
*/
private $content;
/**
* @var string
*/
private $passphrase;
/**
* @param string $content
* @param string $passphrase
*/
public function __construct($content, $passphrase = null)
{
$this->setContent($content);
$this->passphrase = $passphrase;
}
/**
* @param string $content
*
* @throws InvalidArgumentException
*/
private function setContent($content)
{
if (strpos($content, 'file://') === 0) {
$content = $this->readFile($content);
}
$this->content = $content;
}
/**
* @param string $content
*
* @return string
*
* @throws \InvalidArgumentException
*/
private function readFile($content)
{
$file = substr($content, 7);
if (!is_readable($file)) {
throw new \InvalidArgumentException('You must inform a valid key file');
}
return file_get_contents($file);
}
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @return string
*/
public function getPassphrase()
{
return $this->passphrase;
}
}
--- NEW FILE: BaseSigner.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signature;
use Lcobucci\JWT\Signer;
/**
* Base class for signers
*
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 0.1.0
*/
abstract class BaseSigner implements Signer
{
/**
* {@inheritdoc}
*/
public function modifyHeader(array &$headers)
{
$headers['alg'] = $this->getAlgorithmId();
}
/**
* {@inheritdoc}
*/
public function sign($payload, $key)
{
return new Signature($this->createHash($payload, $this->getKey($key)));
}
/**
* {@inheritdoc}
*/
public function verify($expected, $payload, $key)
{
return $this->doVerify($expected, $payload, $this->getKey($key));
}
/**
* @param Key|string $key
*
* @return Key
*/
private function getKey($key)
{
if (is_string($key)) {
$key = new Key($key);
}
return $key;
}
/**
* Creates a hash with the given data
*
* @param string $payload
* @param Key $key
*
* @return string
*/
abstract public function createHash($payload, Key $key);
/**
* Creates a hash with the given data
*
* @param string $payload
* @param Key $key
*
* @return boolean
*/
abstract public function doVerify($expected, $payload, Key $key);
}
--- NEW FILE: Keychain.php ---
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
/**
* A utilitarian class that encapsulates the retrieval of public and private keys
*
* @author LuÃs Otávio Cobucci Oblonczyk <lco...@gm...>
* @since 2.1.0
*
* @deprecated Since we've removed OpenSSL from ECDSA there's no reason to use this class
*/
class Keychain
{
/**
* Returns a private key from file path or content
*
* @param string $key
* @param string $passphrase
*
* @return Key
*/
public function getPrivateKey($key, $passphrase = null)
{
return new Key($key, $passphrase);
}
/**
* Returns a public key from file path or content
*
* @param string $certificate
*
* @return Key
*/
public function getPublicKey($certificate)
{
return new Key($certificate);
}
}
|