Ver Fonte

Enable RSA class to be setup with just a public key (this was triggering an Exception). Establishes valid behaviour for verifying signatures. Fixes ZF-8846

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22041 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic há 15 anos atrás
pai
commit
2fbc433b1a
2 ficheiros alterados com 24 adições e 4 exclusões
  1. 15 4
      library/Zend/Crypt/Rsa.php
  2. 9 0
      tests/Zend/Crypt/Rsa/RsaTest.php

+ 15 - 4
library/Zend/Crypt/Rsa.php

@@ -218,8 +218,13 @@ class Zend_Crypt_Rsa
     public function setPemString($value)
     {
         $this->_pemString = $value;
-        $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
-        $this->_publicKey = $this->_privateKey->getPublicKey();
+        try {
+            $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
+            $this->_publicKey = $this->_privateKey->getPublicKey();
+        } catch (Zend_Crypt_Exception $e) {
+            $this->_privateKey = null;
+            $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_pemString);
+        }
     }
 
     public function setPemPath($value)
@@ -242,7 +247,7 @@ class Zend_Crypt_Rsa
 
     public function setHashAlgorithm($name)
     {
-        switch ($name) {
+        switch (strtolower($name)) {
             case 'md2':
                 $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
                 break;
@@ -252,6 +257,12 @@ class Zend_Crypt_Rsa
             case 'md5':
                 $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
                 break;
+            case 'sha1':
+                $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
+                break;
+            case 'dss1':
+                $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
+                break;
         }
     }
 
@@ -295,4 +306,4 @@ class Zend_Crypt_Rsa
         return null;
     }
 
-}
+}

+ 9 - 0
tests/Zend/Crypt/Rsa/RsaTest.php

@@ -315,4 +315,13 @@ CERT;
         }
     }
 
+    /**
+     * @group ZF-8846
+     */
+    public function testLoadsPublicKeyFromPEMWithoutPrivateKeyAndThrowsNoException()
+    {
+        $rsa = new Zend_Crypt_Rsa;
+        $rsa->setPemString($this->_testPemStringPublic);
+    }
+
 }