Continued:
[core.git] / framework / main / classes / streams / crypto / class_OpenSslStream.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stream\Crypto;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
7 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
8 use Org\Mxchange\CoreFramework\Stream\BaseStream;
9
10 /**
11  * An OpenSSL-based encryption stream
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class OpenSslStream extends BaseStream implements EncryptableStream {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this node class
45          *
46          * @param       $rngInstance            An RNG instance
47          * @return      $streamInstance         An instance of this node class
48          */
49         public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
50                 // Get a new instance
51                 $streamInstance = new OpenSslStream();
52
53                 // Set the RNG instance
54                 $streamInstance->setRngInstance($rngInstance);
55
56                 // Return the instance
57                 return $streamInstance;
58         }
59
60         /**
61          * Encrypt the string with fixed salt
62          *
63          * @param       $str            The unencrypted string
64          * @param       $key            Optional key, if none provided, a random key will be generated
65          * @return      $encrypted      Encrypted string
66          */
67         public function encryptStream ($str, $key = NULL) {
68                 // @TODO unfinished
69                 return $str;
70
71                 // Debug message
72                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: key[' . gettype($key) . ']=' . $key);
73
74                 // Init crypto module
75                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
76                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
77
78                 // Generate key, if none provided
79                 if (is_null($key)) {
80                         // None provided
81                         $key = $this->getRngInstance()->generateKey();
82                 } // END - if
83
84                 // Add some "payload" to the string
85                 switch ($this->getRngInstance()->randomNumber(0, 8)) {
86                         case 0:
87                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
88                                 break;
89
90                         case 1:
91                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
92                                 break;
93
94                         case 2:
95                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
96                                 break;
97
98                         case 3:
99                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
100                                 break;
101
102                         case 4:
103                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
104                                 break;
105
106                         case 5:
107                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
108                                 break;
109
110                         case 6:
111                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
112                                 break;
113
114                         case 7:
115                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
116                                 break;
117
118                         case 8:
119                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
120                                 break;
121                 }
122
123                 // Encrypt the string
124                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
125
126                 // Return the string
127                 return $encrypted;
128         }
129
130         /**
131          * Decrypt the string with fixed salt
132          *
133          * @param       $encrypted      Encrypted string
134          * @param       $key            Optional key, if none provided, a random key will be generated
135          * @return      $str            The unencrypted string
136          */
137         public function decryptStream ($encrypted, $key = NULL) {
138                 // @TODO unfinished
139                 return $encrypted;
140
141                 // Init crypto module
142                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
143                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
144
145                 // Shall we use a default key or custom?
146                 if (is_null($key)) {
147                         // Generate (default) key
148                         $key = $this->getRngInstance()->generateKey();
149                 } // END - if
150
151                 // Decrypt the string
152                 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
153
154                 // Get the real string out
155                 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
156
157                 // Does the element count match?
158                 assert(count($strArray) == 3);
159
160                 // Decode the string
161                 $str = base64_decode($strArray[1]);
162
163                 // Trim trailing nulls away
164                 $str = rtrim($str, "\0");
165
166                 // Return the string
167                 return $str;
168         }
169
170         /**
171          * Streams the data and maybe does something to it
172          *
173          * @param       $data   The data (string mostly) to "stream"
174          * @return      $data   The data (string mostly) to "stream"
175          * @throws      UnsupportedOperationException   If this method is called (which is a mistake)
176          */
177         public function streamData ($data) {
178                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
179                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
180         }
181
182 }