Skip to content
This repository
Browse code

adding enableCrypto() method to CakeSocket class

  • Loading branch information...
commit 27a895d7d8789334def82f32402c6bb282ed632a 1 parent a7865b5
Jorge M. González Martín authored July 21, 2012
53  lib/Cake/Network/CakeSocket.php
@@ -77,6 +77,27 @@ class CakeSocket {
77 77
 	public $lastError = array();
78 78
 
79 79
 /**
  80
+ * True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
  81
+ * @var type 
  82
+ */	
  83
+	public $encrypted = false;
  84
+	
  85
+/**
  86
+ * Contains all the encryption methods available
  87
+ * @var array 
  88
+ */
  89
+	protected $_encryptMethods = array(
  90
+		'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
  91
+		'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  92
+		'sslv23_client' =>STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  93
+		'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
  94
+		'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER,
  95
+		'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER,
  96
+		'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER,
  97
+		'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER
  98
+	);
  99
+
  100
+/**
80 101
  * Constructor.
81 102
  *
82 103
  * @param array $config Socket configuration, which will be merged with the base configuration
@@ -277,4 +298,34 @@ public function reset($state = null) {
277 298
 		return true;
278 299
 	}
279 300
 
280  
-}
  301
+/**
  302
+ * Encrypts current stream socket, using one of the defined encryption methods
  303
+ * 
  304
+ * @param string $type can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'
  305
+ * @param string $clientOrServer can be one of 'client', 'server'. Default is 'client'
  306
+ * @param boolean $enable enable or disable encryption. Default is true (enable)
  307
+ * @return boolean True on success
  308
+ * @throws SocketException
  309
+ * @see stream_socket_enable_crypto  
  310
+ */
  311
+	public function enableCrypto($type, $clientOrServer = 'client', $enable = true) {
  312
+		if (!array_key_exists($type . '_' . $clientOrServer, $this->_encryptMethods)) {
  313
+			throw new InvalidArgumentException();
  314
+		}
  315
+		$enableCryptoResult = false;
  316
+		try {
  317
+			$enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable, $this->_encryptMethods[$type . '_' . $clientOrServer]);
  318
+		} catch (Exception $e) {
  319
+			$this->setLastError(null, $e->getMessage());
  320
+			throw new SocketException($e->getMessage());
  321
+		}
  322
+		if ($enableCryptoResult === true) {
  323
+			$this->encrypted = $enable;
  324
+			return true;
  325
+		} else {
  326
+			$errorMessage = __('Unable to perform enableCrypto operation on CakeSocket');
  327
+			$this->setLastError(null, $errorMessage);
  328
+			throw new SocketException($errorMessage);
  329
+		}
  330
+	}	
  331
+}
109  lib/Cake/Test/Case/Network/CakeSocketTest.php
@@ -214,4 +214,113 @@ public function testReset() {
214 214
 		$anotherSocket->reset();
215 215
 		$this->assertEquals(array(), $anotherSocket->config);
216 216
 	}
  217
+
  218
+/**
  219
+ * testEncrypt
  220
+ * 
  221
+ * @return void
  222
+ */	
  223
+	public function testEnableCryptoSocketExceptionNoSsl() {
  224
+		$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  225
+
  226
+		// testing exception on no ssl socket server for ssl and tls methods
  227
+		$this->Socket = new CakeSocket($configNoSslOrTls);
  228
+		$this->Socket->connect();
  229
+		$this->setExpectedException('SocketException');
  230
+		$this->Socket->enableCrypto('sslv3', 'client');
  231
+	}
  232
+
  233
+/**
  234
+ * testEnableCryptoSocketExceptionNoTls
  235
+ * 
  236
+ * @return void
  237
+ */	
  238
+	public function testEnableCryptoSocketExceptionNoTls() {
  239
+		$configNoSslOrTls = array('host' => 'localhost', 'port' => 80, 'timeout' => 0.1);
  240
+
  241
+		// testing exception on no ssl socket server for ssl and tls methods
  242
+		$this->Socket = new CakeSocket($configNoSslOrTls);
  243
+		$this->Socket->connect();
  244
+		$this->setExpectedException('SocketException');
  245
+		$this->Socket->enableCrypto('tls', 'client');
  246
+	}
  247
+
  248
+/**
  249
+ * _connectSocketToSslTls
  250
+ * 
  251
+ * @return void
  252
+ */	
  253
+	protected function _connectSocketToSslTls() {
  254
+		$configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
  255
+		$this->Socket = new CakeSocket($configSslTls);
  256
+		$this->Socket->connect();
  257
+	}
  258
+
  259
+/**
  260
+ * testEnableCryptoBadMode
  261
+ * 
  262
+ * @return void
  263
+ */	
  264
+	public function testEnableCryptoBadMode() {
  265
+		// testing wrong encryption mode
  266
+		$this->_connectSocketToSslTls();
  267
+		$this->setExpectedException('InvalidArgumentException');
  268
+		$this->Socket->enableCrypto('doesntExistMode', 'server');
  269
+		$this->Socket->disconnect();
  270
+	}
  271
+	
  272
+/**
  273
+ * testEnableCrypto
  274
+ * 
  275
+ * @return void
  276
+ */	
  277
+	public function testEnableCrypto() {
  278
+		// testing on ssl server
  279
+		$this->_connectSocketToSslTls();
  280
+		$this->assertTrue($this->Socket->enableCrypto('sslv3', 'client'));
  281
+		$this->Socket->disconnect();
  282
+
  283
+		// testing on tls server
  284
+		$this->_connectSocketToSslTls();
  285
+		$this->assertTrue($this->Socket->enableCrypto('tls', 'client'));
  286
+		$this->Socket->disconnect();
  287
+	}
  288
+	
  289
+/**
  290
+ * testEnableCryptoExceptionEnableTwice
  291
+ * 
  292
+ * @return void
  293
+ */	
  294
+	public function testEnableCryptoExceptionEnableTwice() {
  295
+		// testing on tls server
  296
+		$this->_connectSocketToSslTls();
  297
+		$this->Socket->enableCrypto('tls', 'client');
  298
+		$this->setExpectedException('SocketException');
  299
+		$this->Socket->enableCrypto('tls', 'client');
  300
+	}
  301
+
  302
+/**
  303
+ * testEnableCryptoExceptionDisableTwice
  304
+ * 
  305
+ * @return void
  306
+ */	
  307
+	public function testEnableCryptoExceptionDisableTwice() {
  308
+		// testing on tls server
  309
+		$this->_connectSocketToSslTls();
  310
+		$this->setExpectedException('SocketException');
  311
+		$this->Socket->enableCrypto('tls', 'client', false);
  312
+	}	
  313
+
  314
+/**
  315
+ * testEnableCryptoEnableStatus
  316
+ * 
  317
+ * @return void
  318
+ */	
  319
+	public function testEnableCryptoEnableStatus() {
  320
+		// testing on tls server
  321
+		$this->_connectSocketToSslTls();
  322
+		$this->assertFalse($this->Socket->encrypted);
  323
+		$this->Socket->enableCrypto('tls', 'client', true);
  324
+		$this->assertTrue($this->Socket->encrypted);
  325
+	}	
217 326
 }

0 notes on commit 27a895d

Please sign in to comment.
Something went wrong with that request. Please try again.