Пример #1
0
 /**
  * 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();
 }
Пример #2
0
 /**
  *
  * @see \CryptoUtil\Crypto\Crypto::verify()
  */
 public function verify($data, Signature $signature, PublicKeyInfo $pubkey_info, SignatureAlgorithmIdentifier $algo)
 {
     $this->_checkSignatureAlgoAndKey($algo, $pubkey_info->algorithmIdentifier());
     $result = openssl_verify($data, $signature->octets(), $pubkey_info->toPEM(), $this->_algoToDigest($algo));
     if (-1 == $result) {
         throw new \RuntimeException("openssl_verify() failed: " . $this->_getLastError());
     }
     return 1 == $result ? true : false;
 }
Пример #3
0
 /**
  * Generate ASN.1 structure.
  *
  * @return Sequence
  */
 public function toASN1()
 {
     $elements = array(new Integer($this->_version), $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1());
     if (isset($this->_attributes)) {
         $elements[] = new ImplicitlyTaggedType(0, $this->_attributes->toASN1());
     }
     return new Sequence(...$elements);
 }
Пример #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.");
 }
Пример #5
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());
 }
Пример #6
0
 /**
  * Generate ASN.1 structure.
  *
  * @return Sequence
  */
 public function toASN1()
 {
     $elements = array();
     $version = $this->version();
     // if version is not default
     if ($version != self::VERSION_1) {
         $elements[] = new ExplicitlyTaggedType(0, new Integer($version));
     }
     $serial = $this->serialNumber();
     $signature = $this->signature();
     // add required elements
     array_push($elements, new Integer($serial), $signature->toASN1(), $this->_issuer->toASN1(), $this->_validity->toASN1(), $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1());
     if (isset($this->_issuerUniqueID)) {
         $elements[] = new ImplicitlyTaggedType(1, $this->_issuerUniqueID->toASN1());
     }
     if (isset($this->_subjectUniqueID)) {
         $elements[] = new ImplicitlyTaggedType(2, $this->_subjectUniqueID->toASN1());
     }
     if (count($this->_extensions)) {
         $elements[] = new ExplicitlyTaggedType(3, $this->_extensions->toASN1());
     }
     return new Sequence(...$elements);
 }
Пример #7
0
 /**
  * Initialize from a PublicKeyInfo object.
  *
  * @param PublicKeyInfo $pki Public key info
  * @return self
  */
 public static function fromPublicKeyInfo(PublicKeyInfo $pki)
 {
     return self::fromPublicKey($pki->publicKey());
 }
Пример #8
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());
 }