|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class OpenSSLAbstract |
| 4 | + * |
| 5 | + * @created 10.08.2024 |
| 6 | + * @author smiley <[email protected]> |
| 7 | + * @copyright 2024 smiley |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace chillerlan\JOSE\Algorithms; |
| 13 | + |
| 14 | +use chillerlan\JOSE\Key\JWK; |
| 15 | +use OpenSSLAsymmetricKey; |
| 16 | +use RuntimeException; |
| 17 | +use function openssl_pkey_get_details; |
| 18 | +use function openssl_pkey_get_private; |
| 19 | +use function openssl_pkey_get_public; |
| 20 | +use function openssl_sign; |
| 21 | +use function openssl_verify; |
| 22 | + |
| 23 | +/** |
| 24 | + * PHP 8.1 under Windows might get hit with a bunch of OpenSSL errors: |
| 25 | + * |
| 26 | + * error:25070067:DSO support routines:DSO_load:could not load the shared library |
| 27 | + * error:0E07506E:configuration file routines:module_load_dso:error loading dso |
| 28 | + * error:0E076071:configuration file routines:module_run:unknown module name |
| 29 | + * error:0909006C:PEM routines:get_name:no start line |
| 30 | + * |
| 31 | + * derived from web-token/jwt-framework |
| 32 | + * |
| 33 | + * @link https://github.com/web-token/jwt-framework/blob/4af252f28996bfb8ce5eac78037a555dce222829/src/Library/Signature/Algorithm/Util/RSA.php |
| 34 | + */ |
| 35 | +abstract class OpenSSLAbstract extends JWAAbstract{ |
| 36 | + |
| 37 | + protected const KEYTYPE = -1; |
| 38 | + |
| 39 | + private string|null $passphrase; |
| 40 | + |
| 41 | + public function __construct(JWK $jwk, string $algo, string|null $passphrase = null){ |
| 42 | + parent::__construct($jwk, $algo); |
| 43 | + |
| 44 | + $this->passphrase = $passphrase; |
| 45 | + } |
| 46 | + |
| 47 | + abstract protected function checkKeyLength(int $bits):bool; |
| 48 | + |
| 49 | + protected function signMessage(string $message):string{ |
| 50 | + |
| 51 | + if(openssl_sign($message, $signature, $this->jwk->getPrivateKey(), $this::SUPPORTED_ALGOS[$this->algo]) === false){ |
| 52 | + throw new RuntimeException('openssl_sign error'); |
| 53 | + } |
| 54 | + |
| 55 | + return $signature; |
| 56 | + } |
| 57 | + |
| 58 | + protected function verifySignature(string $message, string $signature):bool{ |
| 59 | + $verified = openssl_verify($message, $signature, $this->getPublicKey(), $this::SUPPORTED_ALGOS[$this->algo]); |
| 60 | + |
| 61 | + return $verified === 1; |
| 62 | + } |
| 63 | + |
| 64 | + protected function getPrivateKey():OpenSSLAsymmetricKey{ |
| 65 | + /** @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal */ |
| 66 | + return $this->verifyKey(openssl_pkey_get_private($this->jwk->getPrivateKey(), $this->passphrase)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return array<string, string|array<string, string>> |
| 71 | + * @throws \RuntimeException |
| 72 | + */ |
| 73 | + protected function getPrivateKeyDetails():array{ |
| 74 | + $details = openssl_pkey_get_details($this->getPrivateKey()); |
| 75 | + |
| 76 | + if($details === false){ |
| 77 | + throw new RuntimeException('could not get private key details'); |
| 78 | + } |
| 79 | + |
| 80 | + return $details; |
| 81 | + } |
| 82 | + |
| 83 | + protected function getPublicKey():OpenSSLAsymmetricKey{ |
| 84 | + return $this->verifyKey(openssl_pkey_get_public($this->jwk->getPublicKey())); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return array<string, string|array<string, string>> |
| 89 | + * @throws \RuntimeException |
| 90 | + */ |
| 91 | + protected function getPublicKeyDetails():array{ |
| 92 | + $details = openssl_pkey_get_details($this->getPublicKey()); |
| 93 | + |
| 94 | + if($details === false){ |
| 95 | + throw new RuntimeException('could not get public key details'); |
| 96 | + } |
| 97 | + |
| 98 | + return $details; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @throws \RuntimeException |
| 103 | + */ |
| 104 | + private function verifyKey(OpenSSLAsymmetricKey|false $key):OpenSSLAsymmetricKey{ |
| 105 | + |
| 106 | + if($key === false){ |
| 107 | + throw new RuntimeException('invalid key'); |
| 108 | + } |
| 109 | + |
| 110 | + $details = openssl_pkey_get_details($key); |
| 111 | + |
| 112 | + if($details === false){ |
| 113 | + throw new RuntimeException('could not get key details'); |
| 114 | + } |
| 115 | + |
| 116 | + if($details['type'] !== $this::KEYTYPE){ |
| 117 | + throw new RuntimeException('invalid key type'); |
| 118 | + } |
| 119 | + |
| 120 | + if(!$this->checkKeyLength($details['bits'])){ |
| 121 | + throw new RuntimeException('invalid key length'); |
| 122 | + } |
| 123 | + |
| 124 | + return $key; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments