2018-12-18 13:14:26 +00:00
|
|
|
<?php
|
2019-02-16 17:51:24 +00:00
|
|
|
|
2019-01-08 15:14:42 +00:00
|
|
|
namespace ActivityPub\Test\Crypto;
|
2018-12-18 13:14:26 +00:00
|
|
|
|
|
|
|
use ActivityPub\Crypto\RsaKeypair;
|
2019-02-07 03:48:00 +00:00
|
|
|
use ActivityPub\Test\TestConfig\APTestCase;
|
|
|
|
use BadMethodCallException;
|
2018-12-18 13:14:26 +00:00
|
|
|
|
2019-02-07 03:48:00 +00:00
|
|
|
class RsaKeypairTest extends APTestCase
|
2018-12-18 13:14:26 +00:00
|
|
|
{
|
|
|
|
public function testItCreatesKeypair()
|
|
|
|
{
|
|
|
|
$keypair = RsaKeypair::generate();
|
|
|
|
$this->assertStringStartsWith( '-----BEGIN PUBLIC KEY-----', $keypair->getPublicKey() );
|
|
|
|
$this->assertStringEndsWith( '-----END PUBLIC KEY-----', $keypair->getPublicKey() );
|
2018-12-21 14:51:58 +00:00
|
|
|
$this->assertStringStartsWith(
|
|
|
|
'-----BEGIN RSA PRIVATE KEY-----', $keypair->getPrivateKey()
|
|
|
|
);
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'-----END RSA PRIVATE KEY-----', $keypair->getPrivateKey()
|
|
|
|
);
|
2018-12-18 13:14:26 +00:00
|
|
|
}
|
2018-12-18 13:42:09 +00:00
|
|
|
|
|
|
|
public function testItSignsAndValidatesSignatures()
|
|
|
|
{
|
|
|
|
$keypair = RsaKeypair::generate();
|
|
|
|
$data = 'This is some data';
|
|
|
|
$signature = $keypair->sign( $data );
|
|
|
|
$this->assertInternalType( 'string', $signature );
|
|
|
|
$this->assertNotEmpty( $signature );
|
|
|
|
$verified = $keypair->verify( $data, $signature );
|
|
|
|
$this->assertTrue( $verified );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItGivesErrorValidatingInvalidSignature()
|
|
|
|
{
|
|
|
|
$keypair = RsaKeypair::generate();
|
|
|
|
$data = 'This is some data';
|
|
|
|
$signature = 'not a real signature';
|
2019-02-07 03:48:00 +00:00
|
|
|
$this->setExpectedException( \PHPUnit_Framework_Error::class );
|
2019-02-14 03:27:47 +00:00
|
|
|
$keypair->verify( $data, $signature );
|
2018-12-18 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItReturnsNotVerifiedForValidButWrongSignature()
|
|
|
|
{
|
|
|
|
$keypairOne = RsaKeypair::generate();
|
|
|
|
$data = 'This is some data';
|
|
|
|
$signature = $keypairOne->sign( $data );
|
|
|
|
$keypairTwo = RsaKeypair::generate();
|
|
|
|
$verified = $keypairTwo->verify( $data, $signature );
|
|
|
|
$this->assertFalse( $verified );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCreatesValidPublicKeyOnly()
|
|
|
|
{
|
|
|
|
$fullKeypair = RsaKeypair::generate();
|
|
|
|
$publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() );
|
|
|
|
$data = 'This is some data';
|
|
|
|
$signature = $fullKeypair->sign( $data );
|
|
|
|
$verified = $publicKeyOnly->verify( $data, $signature );
|
|
|
|
$this->assertTrue( $verified );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCannotSignWithPublicKeyOnly()
|
|
|
|
{
|
|
|
|
$fullKeypair = RsaKeypair::generate();
|
|
|
|
$publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() );
|
|
|
|
$data = 'This is some data';
|
2019-02-07 03:48:00 +00:00
|
|
|
$this->setExpectedException( BadMethodCallException::class, 'Unable to sign data without a private key' );
|
|
|
|
$publicKeyOnly->sign( $data );
|
2018-12-18 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItSignsAndVerifiesEmptyData()
|
|
|
|
{
|
|
|
|
$keypair = RsaKeypair::generate();
|
|
|
|
$data = '';
|
|
|
|
$signature = $keypair->sign( $data );
|
|
|
|
$verified = $keypair->verify( $data, $signature );
|
|
|
|
$this->assertTrue( $verified );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItHandlesInvalidPublicKeyOnly()
|
|
|
|
{
|
|
|
|
$fullKeypair = RsaKeypair::generate();
|
|
|
|
$publicKeyOnly = RsaKeypair::fromPublicKey( 'not a real public key' );
|
|
|
|
$data = 'This is some data';
|
|
|
|
$signature = $fullKeypair->sign( $data );
|
|
|
|
$verified = $publicKeyOnly->verify( $data, $signature );
|
|
|
|
$this->assertFalse( $verified );
|
|
|
|
}
|
2018-12-18 13:14:26 +00:00
|
|
|
}
|
2019-02-07 03:48:00 +00:00
|
|
|
|