/** * Convert JWK to PEM. * * @return PEM PUBLIC KEY */ public function toPEM() { $n = $this->modulusParameter()->number()->base10(); $e = $this->exponentParameter()->number()->base10(); $pk = new RSAPublicKey($n, $e); $pki = new PublicKeyInfo(new RSAEncryptionAlgorithmIdentifier(), $pk->toDER()); return $pki->toPEM(); }
/** * Initialize public key from PEM. * * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { switch ($pem->type()) { case PEM::TYPE_RSA_PUBLIC_KEY: return RSAPublicKey::fromDER($pem->data()); case PEM::TYPE_PUBLIC_KEY: return PublicKeyInfo::fromPEM($pem)->publicKey(); } throw new \UnexpectedValueException("PEM type " . $pem->type() . " is not a valid public key."); }
/** * Get public key. * * @throws \RuntimeException * @return PublicKey */ public function publicKey() { $algo = $this->algorithmIdentifier(); switch ($algo->oid()) { // RSA case AlgorithmIdentifier::OID_RSA_ENCRYPTION: return RSAPublicKey::fromDER($this->_publicKeyData); // elliptic curve // elliptic curve case AlgorithmIdentifier::OID_EC_PUBLIC_KEY: if (!$algo instanceof ECPublicKeyAlgorithmIdentifier) { throw new \UnexpectedValueException("Not an EC algorithm."); } // ECPoint is directly mapped into public key data return new ECPublicKey($this->_publicKeyData, $algo->namedCurve()); } throw new \RuntimeException("Public key " . $algo->oid() . " not supported."); }