Skip to content
Snippets Groups Projects
Select Git revision
  • 0c009ca19c05eb17713514fdf4ee839639e290d7
  • master default protected
  • parallel_trapping
  • offload_trapping
  • script_devel
  • unify_iterations
  • containers-m10
  • magma_refinement
  • release9
  • enable_svd
  • parallel_angles_gmu
  • containers-m8
  • parallel_angles
  • profile_omp_leonardo
  • test_nvidia_profiler
  • containers
  • shaditest
  • test1
  • main
  • 3-error-in-run-the-program
  • experiment
  • NP_TMcode-M10a.03
  • NP_TMcode-M10a.02
  • NP_TMcode-M10a.01
  • NP_TMcode-M10a.00
  • NP_TMcode-M9.01
  • NP_TMcode-M9.00
  • NP_TMcode-M8.03
  • NP_TMcode-M8.02
  • NP_TMcode-M8.01
  • NP_TMcode-M8.00
  • NP_TMcode-M7.00
  • v0.0
33 results

cfrfme.cpp

Blame
  • JWKSHandler.php 2.01 KiB
    <?php
    
    namespace RAP;
    
    use phpseclib\Crypt\RSA;
    
    /**
     * Manages the JWT Key Sets (currently only RSA).
     */
    class JWKSHandler {
    
        private $locator;
    
        public function __construct(Locator $locator) {
            $this->locator = $locator;
        }
    
        public function generateKeyPair() {
    
            $rsa = new RSA();
    
            $rsa->setPrivateKeyFormat(RSA::PRIVATE_FORMAT_PKCS1);
            $rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_PKCS8);
            // Guacamole needs a key of at least 2048
            $result = $rsa->createKey(2048);
    
            $keyPair = new RSAKeyPair();
            $keyPair->alg = 'RS256';
            $keyPair->privateKey = $result['privatekey'];
            $keyPair->publicKey = $result['publickey'];
            $keyPair->keyId = bin2hex(random_bytes(8));
    
            $dao = $this->locator->getJWKSDAO();
            $dao->insertRSAKeyPair($keyPair);
    
            return $keyPair;
        }
    
        public function getJWKS() {
    
            $dao = $this->locator->getJWKSDAO();
    
            $keyPairs = $dao->getRSAKeyPairs();
    
            $keys = [];
            foreach ($keyPairs as $keyPair) {
    
                $rsa = new RSA();
                $rsa->loadKey($keyPair->publicKey);
                $rsa->setPublicKey();
                $publicKeyXML = $rsa->getPublicKey(RSA::PUBLIC_FORMAT_XML);
    
                $rsaModulus = $this->getTagContent($publicKeyXML, "Modulus");
                $rsaExponent = $this->getTagContent($publicKeyXML, "Exponent");
    
                $urisafeModulus = strtr($rsaModulus, '+/', '-_');
    
                $jwk = [];
                $jwk['kty'] = "RSA";
                $jwk['kid'] = $keyPair->keyId;
                $jwk['use'] = "sig";
                $jwk['n'] = $urisafeModulus;
                $jwk['e'] = $rsaExponent;
    
                array_push($keys, $jwk);
            }
    
            return [
                "keys" => $keys
            ];
        }
    
        private function getTagContent(string $publicKeyXML, string $tagname): string {
            $matches = [];
            $pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
            preg_match($pattern, $publicKeyXML, $matches);
            return $matches[1];
        }
    
    }