RsaTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Lcobucci\JWT\Signer;
  3. use InvalidArgumentException;
  4. use Lcobucci\JWT\Keys;
  5. use PHPUnit\Framework\TestCase;
  6. use const OPENSSL_ALGO_SHA256;
  7. use function openssl_pkey_get_private;
  8. use function openssl_pkey_get_public;
  9. use function openssl_sign;
  10. use function openssl_verify;
  11. final class RsaTest extends TestCase
  12. {
  13. use Keys;
  14. /**
  15. * @test
  16. *
  17. * @covers \Lcobucci\JWT\Signer\Rsa::createHash
  18. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  19. * @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
  20. * @covers \Lcobucci\JWT\Signer\OpenSSL
  21. * @covers \Lcobucci\JWT\Signer\BaseSigner
  22. *
  23. * @uses \Lcobucci\JWT\Signer\Key
  24. * @uses \Lcobucci\JWT\Signature
  25. */
  26. public function createHashShouldReturnAValidOpensslSignature()
  27. {
  28. $payload = 'testing';
  29. $signer = $this->getSigner();
  30. $signature = $signer->sign($payload, self::$rsaKeys['private']);
  31. $publicKey = openssl_pkey_get_public(self::$rsaKeys['public']->getContent());
  32. self::assertInternalType('resource', $publicKey);
  33. self::assertSame(1, openssl_verify($payload, $signature, $publicKey, OPENSSL_ALGO_SHA256));
  34. }
  35. /**
  36. * @test
  37. *
  38. * @covers \Lcobucci\JWT\Signer\Rsa::createHash
  39. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  40. * @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
  41. * @covers \Lcobucci\JWT\Signer\OpenSSL
  42. * @covers \Lcobucci\JWT\Signer\BaseSigner
  43. *
  44. * @uses \Lcobucci\JWT\Signer\Key
  45. */
  46. public function createHashShouldRaiseAnExceptionWhenKeyIsInvalid()
  47. {
  48. $key = <<<KEY
  49. -----BEGIN RSA PRIVATE KEY-----
  50. MGECAQACEQC4MRKSVsq5XnRBrJoX6+rnAgMBAAECECO8SZkgw6Yg66A6SUly/3kC
  51. CQDtPXZtCQWJuwIJAMbBu17GDOrFAggopfhNlFcjkwIIVjb7G+U0/TECCEERyvxP
  52. TWdN
  53. -----END RSA PRIVATE KEY-----
  54. KEY;
  55. $signer = $this->getSigner();
  56. $this->expectException(InvalidArgumentException::class);
  57. $this->expectExceptionMessage('There was an error while creating the signature');
  58. $signer->sign('testing', new Key($key));
  59. }
  60. /**
  61. * @test
  62. *
  63. * @covers \Lcobucci\JWT\Signer\Rsa::createHash
  64. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  65. * @covers \Lcobucci\JWT\Signer\OpenSSL
  66. * @covers \Lcobucci\JWT\Signer\BaseSigner
  67. *
  68. * @uses \Lcobucci\JWT\Signer\Key
  69. */
  70. public function createHashShouldRaiseAnExceptionWhenKeyIsNotParseable()
  71. {
  72. $signer = $this->getSigner();
  73. $this->expectException(InvalidArgumentException::class);
  74. $this->expectExceptionMessage('It was not possible to parse your key');
  75. $signer->sign('testing', new Key('blablabla'));
  76. }
  77. /**
  78. * @test
  79. *
  80. * @covers \Lcobucci\JWT\Signer\Rsa::createHash
  81. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  82. * @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
  83. * @covers \Lcobucci\JWT\Signer\OpenSSL
  84. * @covers \Lcobucci\JWT\Signer\BaseSigner
  85. *
  86. * @uses \Lcobucci\JWT\Signer\Key
  87. */
  88. public function createHashShouldRaiseAnExceptionWhenKeyTypeIsNotRsa()
  89. {
  90. $signer = $this->getSigner();
  91. $this->expectException(InvalidArgumentException::class);
  92. $this->expectExceptionMessage('This key is not compatible with this signer');
  93. $signer->sign('testing', self::$ecdsaKeys['private']);
  94. }
  95. /**
  96. * @test
  97. *
  98. * @covers \Lcobucci\JWT\Signer\Rsa::doVerify
  99. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  100. * @covers \Lcobucci\JWT\Signer\Rsa::getKeyType
  101. * @covers \Lcobucci\JWT\Signer\OpenSSL
  102. * @covers \Lcobucci\JWT\Signer\BaseSigner
  103. *
  104. * @uses \Lcobucci\JWT\Signer\Key
  105. */
  106. public function doVerifyShouldReturnTrueWhenSignatureIsValid()
  107. {
  108. $payload = 'testing';
  109. $privateKey = openssl_pkey_get_private(self::$rsaKeys['private']->getContent());
  110. self::assertInternalType('resource', $privateKey);
  111. $signature = '';
  112. openssl_sign($payload, $signature, $privateKey, OPENSSL_ALGO_SHA256);
  113. $signer = $this->getSigner();
  114. self::assertTrue($signer->verify($signature, $payload, self::$rsaKeys['public']));
  115. }
  116. /**
  117. * @test
  118. *
  119. * @covers \Lcobucci\JWT\Signer\Rsa::doVerify
  120. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  121. * @covers \Lcobucci\JWT\Signer\OpenSSL
  122. * @covers \Lcobucci\JWT\Signer\BaseSigner
  123. *
  124. * @uses \Lcobucci\JWT\Signer\Key
  125. */
  126. public function doVerifyShouldRaiseAnExceptionWhenKeyIsNotParseable()
  127. {
  128. $signer = $this->getSigner();
  129. $this->expectException(InvalidArgumentException::class);
  130. $this->expectExceptionMessage('It was not possible to parse your key');
  131. $signer->verify('testing', 'testing', new Key('blablabla'));
  132. }
  133. /**
  134. * @test
  135. *
  136. * @covers \Lcobucci\JWT\Signer\Rsa::doVerify
  137. * @covers \Lcobucci\JWT\Signer\Rsa::validateKey
  138. * @covers \Lcobucci\JWT\Signer\OpenSSL
  139. * @covers \Lcobucci\JWT\Signer\BaseSigner
  140. *
  141. * @uses \Lcobucci\JWT\Signer\Key
  142. */
  143. public function doVerifyShouldRaiseAnExceptionWhenKeyTypeIsNotRsa()
  144. {
  145. $signer = $this->getSigner();
  146. $this->expectException(InvalidArgumentException::class);
  147. $this->expectExceptionMessage('It was not possible to parse your key');
  148. $signer->verify('testing', 'testing', self::$ecdsaKeys['private']);
  149. }
  150. private function getSigner()
  151. {
  152. $signer = $this->getMockForAbstractClass(Rsa::class);
  153. $signer->method('getAlgorithm')
  154. ->willReturn(OPENSSL_ALGO_SHA256);
  155. $signer->method('getAlgorithmId')
  156. ->willReturn('RS256');
  157. return $signer;
  158. }
  159. }