Explorar o código

Handle situation when openss_pkey_new() cannot generate a private key by throwing and exception. Tests now skip in this scenario

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24707 44c647ce-9c0f-0410-b52a-842ac1e357ba
rob %!s(int64=13) %!d(string=hai) anos
pai
achega
38b75aa6f4
Modificáronse 2 ficheiros con 45 adicións e 1 borrados
  1. 14 0
      library/Zend/Crypt/Rsa.php
  2. 31 1
      tests/Zend/Crypt/Rsa/RsaTest.php

+ 14 - 0
library/Zend/Crypt/Rsa.php

@@ -201,6 +201,13 @@ class Zend_Crypt_Rsa
         return $decrypted;
     }
 
+    /**
+     * @param  array $configargs
+     * 
+     * @throws Zend_Crypt_Rsa_Exception
+     * 
+     * @return ArrayObject
+     */
     public function generateKeys(array $configargs = null)
     {
         $config = null;
@@ -215,6 +222,10 @@ class Zend_Crypt_Rsa
         $privateKey = null;
         $publicKey = null;
         $resource = openssl_pkey_new($config);
+        if (!$resource) {
+            require_once 'Zend/Crypt/Rsa/Exception.php';
+            throw new Zend_Crypt_Rsa_Exception('Failed to generate a new private key');
+        }
         // above fails on PHP 5.3
         openssl_pkey_export($resource, $private, $passPhrase);
         $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
@@ -312,6 +323,9 @@ class Zend_Crypt_Rsa
     protected function _parseConfigArgs(array $config = null)
     {
         $configs = array();
+        if (isset($config['private_key_bits'])) {
+            $configs['private_key_bits'] = $config['private_key_bits'];
+        }
         if (isset($config['privateKeyBits'])) {
             $configs['private_key_bits'] = $config['privateKeyBits'];
         }

+ 31 - 1
tests/Zend/Crypt/Rsa/RsaTest.php

@@ -43,7 +43,7 @@ class Zend_Crypt_RsaTest extends PHPUnit_Framework_TestCase
         try {
             $rsaObject = new Zend_Crypt_Rsa();
         } catch (Zend_Crypt_Rsa_Exception $e) {
-            if (strpos($e->getMessage(), 'requires openssl extention') !== false) {
+            if (strpos($e->getMessage(), 'requires openssl extension') !== false) {
                 $this->markTestSkipped($e->getMessage());
             } else {
                 throw $e;
@@ -273,6 +273,12 @@ CERT;
     public function testKeyGenerationCreatesArrayObjectResult()
     {
         $rsa = new Zend_Crypt_Rsa;
+        // check to see if openssl.cnf can be found by trying to generate a key
+        $test = openssl_pkey_new();
+        if (!$test) {
+            $this->markTestSkipped('Cannot generate a private key with openssl_pkey_new()');
+        }
+
         $keys = $rsa->generateKeys(array('private_key_bits'=>512));
         $this->assertType('ArrayObject', $keys);
     }
@@ -280,6 +286,12 @@ CERT;
     public function testKeyGenerationCreatesPrivateKeyInArrayObject()
     {
         $rsa = new Zend_Crypt_Rsa;
+        // check to see if openssl.cnf can be found by trying to generate a key
+        $test = openssl_pkey_new();
+        if (!$test) {
+            $this->markTestSkipped('Cannot generate a private key with openssl_pkey_new()');
+        }
+
         $keys = $rsa->generateKeys(array('private_key_bits'=>512));
         $this->assertType('Zend_Crypt_Rsa_Key_Private', $keys->privateKey);
     }
@@ -287,6 +299,12 @@ CERT;
     public function testKeyGenerationCreatesPublicKeyInArrayObject()
     {
         $rsa = new Zend_Crypt_Rsa;
+        // check to see if openssl.cnf can be found by trying to generate a key
+        $test = openssl_pkey_new();
+        if (!$test) {
+            $this->markTestSkipped('Cannot generate a private key with openssl_pkey_new()');
+        }
+
         $keys = $rsa->generateKeys(array('privateKeyBits'=>512));
         $this->assertType('Zend_Crypt_Rsa_Key_Public', $keys->publicKey);
     }
@@ -294,6 +312,12 @@ CERT;
     public function testKeyGenerationCreatesPassphrasedPrivateKey()
     {
         $rsa = new Zend_Crypt_Rsa;
+        // check to see if openssl.cnf can be found by trying to generate a key
+        $test = openssl_pkey_new();
+        if (!$test) {
+            $this->markTestSkipped('Cannot generate a private key with openssl_pkey_new()');
+        }
+
         $config = array(
             'privateKeyBits' => 512,
             'passPhrase' => '0987654321'
@@ -312,6 +336,12 @@ CERT;
     public function testConstructorLoadsPassphrasedKeys()
     {
         $rsa = new Zend_Crypt_Rsa;
+        // check to see if openssl.cnf can be found by trying to generate a key
+        $test = openssl_pkey_new();
+        if (!$test) {
+            $this->markTestSkipped('Cannot generate a private key with openssl_pkey_new()');
+        }
+        
         $config = array(
             'privateKeyBits' => 512,
             'passPhrase' => '0987654321'