123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace Aws\CloudFront;
- class Signer
- {
- private $keyPairId;
- private $pkHandle;
-
- public function __construct($keyPairId, $privateKey, $passphrase = "")
- {
- if (!extension_loaded('openssl')) {
-
- throw new \RuntimeException('The openssl extension is required to '
- . 'sign CloudFront urls.');
-
- }
- $this->keyPairId = $keyPairId;
- if (!$this->pkHandle = openssl_pkey_get_private($privateKey, $passphrase)) {
- if (!file_exists($privateKey)) {
- throw new \InvalidArgumentException("PK file not found: $privateKey");
- }
- $this->pkHandle = openssl_pkey_get_private("file://$privateKey", $passphrase);
- if (!$this->pkHandle) {
- $errorMessages = [];
- while(($newMessage = openssl_error_string()) !== false){
- $errorMessages[] = $newMessage;
- }
- throw new \InvalidArgumentException(implode("\n",$errorMessages));
- }
- }
- }
- public function __destruct()
- {
- if (PHP_MAJOR_VERSION < 8) {
- $this->pkHandle && openssl_pkey_free($this->pkHandle);
- }
- }
-
- public function getSignature($resource = null, $expires = null, $policy = null)
- {
- $signatureHash = [];
- if ($policy) {
- $policy = preg_replace('/\s/s', '', $policy);
- $signatureHash['Policy'] = $this->encode($policy);
- } elseif ($resource && $expires) {
- $expires = (int) $expires;
- $policy = $this->createCannedPolicy($resource, $expires);
- $signatureHash['Expires'] = $expires;
- } else {
- throw new \InvalidArgumentException('Either a policy or a resource'
- . ' and an expiration time must be provided.');
- }
- $signatureHash['Signature'] = $this->encode($this->sign($policy));
- $signatureHash['Key-Pair-Id'] = $this->keyPairId;
- return $signatureHash;
- }
- private function createCannedPolicy($resource, $expiration)
- {
- return json_encode([
- 'Statement' => [
- [
- 'Resource' => $resource,
- 'Condition' => [
- 'DateLessThan' => ['AWS:EpochTime' => $expiration],
- ],
- ],
- ],
- ], JSON_UNESCAPED_SLASHES);
- }
- private function sign($policy)
- {
- $signature = '';
-
- if(!openssl_sign($policy, $signature, $this->pkHandle)) {
- $errorMessages = [];
- while(($newMessage = openssl_error_string()) !== false) {
- $errorMessages[] = $newMessage;
- }
-
- $exceptionMessage = "An error has occurred when signing the policy";
- if (count($errorMessages) > 0) {
- $exceptionMessage = implode("\n", $errorMessages);
- }
- throw new \RuntimeException($exceptionMessage);
- }
- return $signature;
- }
- private function encode($policy)
- {
- return strtr(base64_encode($policy), '+=/', '-_~');
- }
- }
|