/** * Initialize from PEM. * * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { if ($pem->type() !== PEM::TYPE_ATTRIBUTE_CERTIFICATE) { throw new \UnexpectedValueException("Invalid PEM type."); } return self::fromDER($pem->data()); }
/** * Initialize from PEM. * * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { if ($pem->type() != PEM::TYPE_PRIVATE_KEY) { throw new \UnexpectedValueException("Invalid PEM type."); } return self::fromDER($pem->data()); }
/** * 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."); }
/** * * @see PublicKey::fromPEM() * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { if ($pem->type() == PEM::TYPE_RSA_PUBLIC_KEY) { return self::fromDER($pem->data()); } if ($pem->type() != PEM::TYPE_PUBLIC_KEY) { throw new \UnexpectedValueException("Invalid PEM type."); } $pki = PublicKeyInfo::fromDER($pem->data()); if ($pki->algorithmIdentifier()->oid() != AlgorithmIdentifier::OID_RSA_ENCRYPTION) { throw new \UnexpectedValueException("Not an RSA public key."); } return self::fromDER($pki->publicKeyData()); }
/** * Initialize private key from PEM. * * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { switch ($pem->type()) { case PEM::TYPE_RSA_PRIVATE_KEY: return RSAPrivateKey::fromDER($pem->data()); case PEM::TYPE_EC_PRIVATE_KEY: return ECPrivateKey::fromDER($pem->data()); case PEM::TYPE_PRIVATE_KEY: return PrivateKeyInfo::fromDER($pem->data())->privateKey(); } throw new \UnexpectedValueException("PEM type " . $pem->type() . " is not a valid private key."); }
/** * * @see PrivateKey::fromPEM() * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { if ($pem->type() == PEM::TYPE_EC_PRIVATE_KEY) { return self::fromDER($pem->data()); } if ($pem->type() != PEM::TYPE_PRIVATE_KEY) { throw new \UnexpectedValueException("Not a private key."); } $pki = PrivateKeyInfo::fromDER($pem->data()); $algo = $pki->algorithmIdentifier(); if ($algo->oid() != AlgorithmIdentifier::OID_EC_PUBLIC_KEY) { throw new \UnexpectedValueException("Not an elliptic curve key."); } $obj = self::fromDER($pki->privateKeyData()); if (!isset($obj->_namedCurve)) { $obj->_namedCurve = $algo->namedCurve(); } return $obj; }
/** * * @see PublicKey::fromPEM() * @param PEM $pem * @throws \UnexpectedValueException * @return self */ public static function fromPEM(PEM $pem) { if ($pem->type() != PEM::TYPE_PUBLIC_KEY) { throw new \UnexpectedValueException("Not a public key."); } $pki = PublicKeyInfo::fromDER($pem->data()); $algo = $pki->algorithmIdentifier(); if ($algo->oid() != AlgorithmIdentifier::OID_EC_PUBLIC_KEY) { throw new \UnexpectedValueException("Not an elliptic curve key."); } // ECPoint is directly mapped into public key data return new self($pki->publicKeyData(), $algo->namedCurve()); }