some basic encryption beginnings.
[pengine.git] / lib / interComms.php
1 <?php
2 define("NETCOM_PORT", 14001);
3 define("NETCOM_PORT_SSL", 14002);
4
5
6 // comms on this thing are very serial, the server are only capable of processing
7 // one thing at a time... for now
8
9 // the encryption we use here yes, it will be pub/priv but not ssl per se.
10 // how encryption works:
11 // initiate connection:
12 // server -> PEN:b64enc(pubkey):INE
13 // client -> PEN:ACK:INE
14 // client -> PEN:encrypt(pubkey, session key):INE
15 // server -> PEN:ACK:INE
16 // client -> PEN:encrypt(data, session key):INE <-- data transmission starts here
17 // server -> PEN:ACK:INE
18
19 class netCom {
20         
21         function __construct($am_i_a_server = false, $server_addr = "127.0.0.1")
22         {
23                 global $storeLocation;
24                 
25                 // i have to set it to something, right?
26                 $this->semKey = ftok(__FILE__, "p");
27                 $this->encrypt = false;
28                 
29                 $this->amserver = $am_i_a_server;
30                 $this->server = $server_addr;
31                 
32                 if($this->amserver) if(is_file("$storeLocation/mykey.priv")) {
33                         echo "loading key\n";
34                         $kh = fopen("$storeLocation/mykey.priv", "r");
35                         $kdp = fread($kh, filesize("$storeLocation/mykey.priv"));
36                         
37                         $key = openssl_pkey_get_private($kdp);
38                         $output = "";
39                         $km = openssl_pkey_export($key, $output);
40                         echo "key is $output\n";
41                         $this->key_priv = $output;
42                         
43                         $ar_pubkey = openssl_pkey_get_details($key);
44                         $this->key_pub = $ar_pubkey["key"];
45                         
46                         
47                 } else {
48                         echo "generateing key\n";
49                         $key = openssl_pkey_new();
50                         echo "key generated $key\n";
51                         $output = "";
52                         $km = openssl_pkey_export($key, $output);
53                         echo "key is $output\n";
54                         $ar_pubkey = openssl_pkey_get_details($key);
55                         $pubkey = $ar_pubkey["key"];
56                         echo "array is $pubkey\n";
57                         // now lets write some shit
58                         $priv_f = fopen("$storeLocation/mykey.priv", "w");
59                         fwrite($priv_f, $output);
60                         $this->key_priv = $output;
61                         fclose($priv_f);
62                         
63                         $pub_f = fopen("$storeLocation/mykey.pub", "w");
64                         fwrite($pub_f, $pubkey);
65                         $this->key_pub = $pubkey;
66                         fclose($pub_f);
67                         
68                         
69                 }
70         }
71         
72         // initiates a bind if its a server, a connect if its a client
73         function go()
74         {
75                 if($this->amserver) {
76                         echo "i am a server, bind!\n";
77                         $this->listen_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
78                         $this->listen_socket_ssl = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
79                         socket_bind($this->listen_socket, "127.0.0.1", NETCOM_PORT);
80                         socket_listen($this->listen_socket);
81                         socket_bind($this->listen_socket_ssl, "127.0.0.1", NETCOM_PORT_SSL);
82                         socket_listen($this->listen_socket_ssl);
83                         echo "bound\n";
84                 } else {
85                         echo "I am a client, connect!\n";
86                         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
87                         $res = socket_connect($this->socket, "127.0.0.1", NETCOM_PORT_SSL);
88                         $this->secure_socket = true;
89                         if(!$res) {
90                                 echo "fail on connect\n";
91                                 socket_close($this->socket);
92                                 return false;
93                         }
94                         echo "connected\n";
95                         
96                         // now handle secure handshake;
97                         if($this->secure_socket) {
98                                 echo "begin handshake\n";
99                                 $msg = $this->receiveMessage();
100                                 echo "got msg:\n";
101                                 print_r($msg);
102                                 echo "\n";
103                                 
104                         }
105                 }
106         }
107         
108         function waitForConnection()
109         {
110                 echo "in wait for connection\n";
111                 socket_listen($this->listen_socket);
112                 $this->socket = socket_accept($this->listen_socket);
113                 $this->secure_socket = false;
114                 echo "exit wait for connection\n";
115         }
116
117         function waitForSecureConnection()
118         {
119                 echo "in wait for secure connection\n";
120                 socket_listen($this->listen_socket_ssl);
121                 $this->socket = socket_accept($this->listen_socket_ssl);
122                 $this->secure_socket = true;
123                 
124                 // now do negotiate
125                 if($this->secure_socket) {
126                         $arg[0] = $this->key_pub;
127                         $this->sendMessage($arg);
128                 }
129                 
130                 echo "exit wait for connection\n";
131         }
132         
133         function sendMessage($message_array)
134         {               
135                 echo "begin send message\n";
136                 $datacomp = base64_encode(serialize($message_array));
137                 $tosend = "PEN:$datacomp:INE";
138                 
139                 socket_send($this->socket, $tosend, strlen($tosend), 0);
140                 echo "end send message\n";
141                 // get up to one meg of data - this is bad... i can feel this function
142                 // hurting alot
143                 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
144                 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
145                 // timeouts now.
146                 // we wait for an ack
147                 $size = socket_recv($this->socket, $recv, 1024, 0);
148                 if($recv != "PEN:ACK:INE") {
149                         echo "invalid response?\n$recv\n";
150                 } else {
151                         echo "got ack\n";
152                 }
153         }
154         
155         function receiveMessage()
156         {
157                 echo "begin recieve message\n";
158                 $recvd = "";
159                 $continue = true;
160                 while($continue) {
161                         $size = socket_recv($this->socket, $recvd_a, 1024, 0);
162                         
163                         $recvd .= $recvd_a;
164                         echo "got $recvd_a so far for $size\n";
165                         if($size == 0) return false;
166                         if(preg_match("/.*\:INE$/", $recvd)) {
167                                 // we have a full string... break out
168                                 $continue = false;
169                                 break;
170                         }
171                 }
172                 
173                 
174                 echo "rec msg next\n";
175                 // first check we got something that makes sense
176                 if(preg_match("/^PEN:.*:INE$/", $recvd) < 1) {
177                         socket_close($this->socket);
178                         echo "Returned data is not in right format\n";
179                         // we have a problem jim
180                         return false;
181                 }
182                 $msg = "PEN:ACK:INE";
183                 socket_send($this->socket, $msg, strlen($msg), 0);
184                 
185                 echo "got a data packet\n";
186                 $xps = explode(":", $recvd);
187                 
188                 $component =  unserialize(base64_decode($xps[1]));
189
190                 return $component;
191         }
192          
193         
194         
195         private $server;
196         private $encrypt;
197         private $semKey;
198         private $amserver;
199         private $socket;
200         private $socket_ssl;
201         private $listen_socket;
202         private $listen_socket_ssl;
203         private $key_priv;
204         private $key_pub;
205         private $secure_socket;
206         
207 }
208
209 /* packets look like
210  * PEN:base64_enoded data:INE
211  * 
212  * 
213  */
214
215 ?>