Ejemplo n.º 1
0
 /**
  * @param resource $res
  *
  * @throws \Exception
  *
  * @return array
  */
 public static function loadKeyFromResource($res)
 {
     $details = openssl_pkey_get_details($res);
     if (!is_array($details)) {
         throw new \Exception('Unable to get details of the key');
     }
     if (array_key_exists('ec', $details)) {
         $pem = $details['key'];
         try {
             openssl_pkey_export($res, $pem);
         } catch (\Exception $e) {
             // Public keys cannot be exported with openssl_pkey_export
         }
         $ec_key = new ECKey($pem);
         return $ec_key->toArray();
     } elseif (array_key_exists('rsa', $details)) {
         return self::loadRSAKey($details['rsa']);
     }
     throw new \Exception('Unsupported key type');
 }
Ejemplo n.º 2
0
 /**
  * @param string $kid               The key ID
  * @param array  $values            Array of values that represent a key
  * @param array  $additional_values Add additional parameters to your key ('alg'=>'RS256'...)
  *
  * @return $this
  */
 public function loadKeyFromValues($kid, array $values, array $additional_values = [])
 {
     if (!array_key_exists('kty', $values)) {
         throw new \InvalidArgumentException('Unable to determine the key type');
     }
     /*
      * @var \Jose\JWKInterface[]
      */
     $keys = [];
     switch ($values['kty']) {
         case 'RSA':
             $rsa = new RSAKey($values);
             if ($rsa->isPrivate()) {
                 $keys['private'] = $this->getJWKManager()->createJWK(array_merge($rsa->toArray(), $additional_values));
             }
             $keys['public'] = $this->getJWKManager()->createJWK(array_merge(RSAKey::toPublic($rsa)->toArray(), $additional_values));
             break;
         case 'EC':
             $rsa = new ECKey($values);
             if ($rsa->isPrivate()) {
                 $keys['private'] = $this->getJWKManager()->createJWK(array_merge($rsa->toArray(), $additional_values));
             }
             $keys['public'] = $this->getJWKManager()->createJWK(array_merge(ECKey::toPublic($rsa)->toArray(), $additional_values));
             break;
         case 'oct':
             $keys['asymmetric'] = $this->getJWKManager()->createJWK(array_merge($values, $additional_values));
             break;
         case 'dir':
             $keys['direct'] = $this->getJWKManager()->createJWK(array_merge($values, $additional_values));
             break;
         case 'none':
             break;
         default:
             throw new \InvalidArgumentException('Unsupported key type');
     }
     foreach ($keys as $name => $jwk) {
         if (is_null($jwk->getKeyID())) {
             $jwk->setValue('kid', $kid);
         }
         $this->addKeyInKeySet($name, $jwk);
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * @param \SpomkyLabs\Jose\KeyConverter\ECKey $private
  *
  * @return \SpomkyLabs\Jose\KeyConverter\ECKey
  */
 public static function toPublic(ECKey $private)
 {
     $data = $private->toArray();
     if (array_key_exists('d', $data)) {
         unset($data['d']);
     }
     return new self($data);
 }