Beispiel #1
0
 /**
  * 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.");
 }
Beispiel #2
0
 /**
  *
  * @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());
 }
Beispiel #3
0
 /**
  * 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());
 }
Beispiel #4
0
 /**
  * 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.");
 }
Beispiel #5
0
 /**
  *
  * @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;
 }
Beispiel #6
0
 /**
  * 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());
 }
Beispiel #7
0
 /**
  *
  * @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());
 }