Example #1
0
 /**
  * @expectedException RuntimeException
  * @expectedExceptionCode 203
  */
 public function testDecryptionRuntimeException203()
 {
     $msg = new Msg();
     $msg->setVersion(1);
     $msg->setId('cafed00d-2131-4159-8e11-0b4dbadb1738');
     $msg->setSrcNodeId('cafed00d-2331-4159-8e11-0b4dbadb1738');
     $msg->setSrcSslKeyPub(static::SRC1_SSL_KEY_PUB);
     $msg->setSrcUserNickname('username1');
     $msg->setDstNodeId('cafed00d-2431-4159-8e11-0b4dbadb1738');
     $msg->setDstSslPubKey(static::DST1_SSL_KEY_PUB);
     $msg->setSubject('subj1');
     $msg->setText('text1');
     $msg->setSslKeyPrv(static::SRC1_SSL_KEY_PRV, static::SSL_KEY_PRV_PASS);
     $msg->setIgnore(false);
     $this->assertTrue($msg->encrypt());
     $body = $msg->getBody();
     $timeCreated = $msg->getTimeCreated();
     $password = $msg->getPassword();
     $checksum = $msg->getChecksum();
     $body = base64_decode($body);
     $body = gzdecode($body);
     $body = json_decode($body, true);
     $body['data'] = 'invalid';
     $body = json_encode($body);
     $body = gzencode($body, 9);
     $body = base64_encode($body);
     $msg = new Msg();
     $msg->setVersion(1);
     $msg->setId('cafed00d-2131-4159-8e11-0b4dbadb1738');
     $msg->setSrcNodeId('cafed00d-2331-4159-8e11-0b4dbadb1738');
     $msg->setBody($body);
     $msg->setSrcSslKeyPub(static::SRC1_SSL_KEY_PUB);
     $msg->setDstSslPubKey(static::DST1_SSL_KEY_PUB);
     $msg->setSslKeyPrv(static::DST1_SSL_KEY_PRV, static::SSL_KEY_PRV_PASS);
     $msg->setDstNodeId('cafed00d-2431-4159-8e11-0b4dbadb1738');
     $msg->setTimeCreated($timeCreated);
     $msg->setPassword($password);
     $msg->setChecksum($checksum);
     $msg->decrypt();
 }
Example #2
0
 private function handleCommandMsg($line)
 {
     #print __CLASS__.'->'.__FUNCTION__.': "'.$line.'"'."\n";
     $line = substr($line, 3);
     #print __CLASS__.'->'.__FUNCTION__.': get table'."\n";
     $table = $this->ipcKernelConnection->execSync('getTable');
     #print __CLASS__.'->'.__FUNCTION__.': get msgDb'."\n";
     #$msgDb = $this->ipcKernelConnection->execSync('getMsgDb');
     $msgs = $this->ipcKernelConnection->execSync('msgDbMsgGetMsgsForDst');
     $msgsByIndex = array_keys($msgs);
     #ve($msgDb);
     #ve($msgs);
     #ve($msgsByIndex);
     /*
     		if(!$msgDb){
     			print __CLASS__.'->'.__FUNCTION__.': get msgDb failed'."\n";
     			return;
     		}*/
     if ($line) {
         $line = substr($line, 1);
         $args = preg_split('/ /', $line);
         #ve($args);
         #print __CLASS__.'->'.__FUNCTION__.': rest "'.$line.'"'."\n";
         #$this->printPs1(true, 'handleCommandMsg A');
         if ($args[0] == 'new' || $args[0] == 'n') {
             if (Uuid::isValid($args[1])) {
                 if ($args[1] != $table->getLocalNode()->getIdHexStr()) {
                     $this->sttyExitIcanonMode();
                     $this->sttyEchoOn();
                     stream_set_blocking(STDIN, 1);
                     print PHP_EOL . 'Subject: ';
                     $subject = strtolower(substr(fgets(STDIN, 100), 0, -1));
                     if (!$subject) {
                         $subject = 'No Subject';
                     }
                     #print "Subject: '".$subject."'\n\n";
                     print PHP_EOL . 'Enter the text to send.' . PHP_EOL;
                     print 'NOTE: end text with  <RETURN>.<RETURN>' . PHP_EOL;
                     $text = '';
                     while (!$this->getExit()) {
                         $line = fgets(STDIN, 1024);
                         #print "line: '".substr($line, 0, -1)."'\n";
                         if (substr($line, 0, -1) == '.') {
                             break;
                         }
                         $text .= $line;
                         #sleep(1);
                     }
                     if (!$this->getExit()) {
                         $text = substr($text, 0, -1);
                         print 'Send msg? [Y/n] ';
                         $answer = strtolower(substr(fgets(STDIN, 100), 0, -1));
                         if (!$answer) {
                             $answer = 'y';
                         }
                         print "Answer: '" . $answer . "'" . PHP_EOL;
                         #print "Text: '".$text."'\n";
                         stream_set_blocking(STDIN, 0);
                         $this->sttyEnterIcanonMode();
                         $this->sttyEchoOff();
                         if ($answer == 'y') {
                             $dstNodeId = $args[1];
                             #$dstNodeId = '42785b21-011b-4093-b61d-000000000001';
                             #$text = 'this is  a test. '.date('Y-m-d H:i:s');
                             $table = $this->ipcKernelConnection->execSync('getTable');
                             $msg = new Msg();
                             $msg->setSrcNodeId($this->settings->data['node']['id']);
                             $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub());
                             $msg->setSrcUserNickname($this->userNickname);
                             $dstNode = new Node();
                             $dstNode->setIdHexStr($dstNodeId);
                             $msg->setDstNodeId($dstNode->getIdHexStr());
                             if ($oDstNode = $table->nodeFind($dstNode)) {
                                 #print 'found node in table'.PHP_EOL;
                                 $msg->setDstSslPubKey($oDstNode->getSslKeyPub());
                             }
                             #else{ print 'node not found'.PHP_EOL; }
                             $msg->setSubject($subject);
                             $msg->setText($text);
                             $msg->setSslKeyPrvPath($this->settings->data['node']['sslKeyPrvPath'], $this->settings->data['node']['sslKeyPrvPass']);
                             $msg->setStatus('O');
                             $encrypted = false;
                             #print 'DstSslPubKey: '.strlen($msg->getDstSslPubKey()).PHP_EOL;
                             if ($msg->getDstSslPubKey()) {
                                 #print 'use dst key'.PHP_EOL;
                                 $msg->setEncryptionMode('D');
                             } else {
                                 // Encrypt with own public key
                                 // while destination public key is not available.
                                 #print 'use local key'.PHP_EOL;
                                 $msg->setEncryptionMode('S');
                                 $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
                             }
                             try {
                                 $encrypted = $msg->encrypt();
                                 if ($encrypted) {
                                     $this->ipcKernelConnection->execAsync('msgDbMsgAdd', array($msg));
                                     $this->msgAdd('OK: msg created ' . $msg->getId(), true, true);
                                 } else {
                                     $this->msgAdd('ERROR: could not encrypt message.', true, true);
                                 }
                             } catch (Exception $e) {
                                 $this->msgAdd('ERROR: ' . $e->getMessage(), true, true);
                             }
                             #$this->printPs1(true, 'handleCommandMsg B');
                         } else {
                             print 'Nothing created, nothing sent.' . PHP_EOL;
                             $this->printPs1(true, 'handleCommandMsg C');
                         }
                     }
                 } else {
                     $this->msgAdd();
                     $this->msgAdd('Send a message to yourself?', false, true);
                 }
             } else {
                 $this->msgAdd();
                 $this->msgAdd('ERROR: "' . $args[1] . '" is not a UUID.', false, true);
             }
         } elseif ($args[0] == 'read' || $args[0] == 'r') {
             if (isset($args[1])) {
                 $msg = null;
                 if (Uuid::isValid($args[1])) {
                     if (isset($msgs[$args[1]])) {
                         $msg = $msgs[$args[1]];
                     }
                 } else {
                     $no = (int) $args[1] - 1;
                     if (isset($msgsByIndex[$no])) {
                         $msg = $msgs[$msgsByIndex[$no]];
                     }
                 }
                 if ($msg) {
                     $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
                     $sslKeyPrvPath = $this->settings->data['node']['sslKeyPrvPath'];
                     $sslKeyPrvPass = $this->settings->data['node']['sslKeyPrvPass'];
                     $msg->setSslKeyPrvPath($sslKeyPrvPath, $sslKeyPrvPass);
                     #ve($msg);
                     $text = null;
                     try {
                         $text = $msg->decrypt();
                     } catch (Exception $e) {
                         $text = null;
                         #print 'ERROR: decrypt: '.$e->getMessage().PHP_EOL;
                     }
                     $dateCreated = new DateTime();
                     $dateCreated->setTimestamp($msg->getTimeCreated());
                     $dateReceived = new DateTime();
                     $dateReceived->setTimestamp($msg->getTimeReceived());
                     $fromLine = '';
                     if ($msg->getSrcUserNickname()) {
                         $fromLine .= $msg->getSrcUserNickname() . ' ';
                     }
                     $fromLine .= '<' . $msg->getSrcNodeId() . '>';
                     $toLine = '';
                     if ($table->getLocalNode()->getIdHexStr() == $msg->getDstNodeId()) {
                         $toLine .= 'Me ';
                     }
                     $toLine .= '<' . $msg->getDstNodeId() . '>';
                     $this->msgAdd();
                     if (!$text) {
                         $this->msgAdd('WARNING: could not decrypt text. Only meta data available.', false, false);
                     }
                     $this->msgAdd('----- MESSAGE BEGIN -----');
                     $this->msgAdd('Subject: ' . $msg->getSubject(), false, false);
                     $this->msgAdd('From: ' . $fromLine, false, false);
                     $this->msgAdd('To: ' . $toLine, false, false);
                     $this->msgAdd('Msg ID: ' . $msg->getId(), false, false);
                     $this->msgAdd('Status: ' . $msg->getStatusText(), false, false);
                     $this->msgAdd('Created:  ' . $dateCreated->format('Y-m-d H:i:s'), false, false);
                     $this->msgAdd('Received: ' . $dateReceived->format('Y-m-d H:i:s'), false, false);
                     if ($text) {
                         $this->msgAdd();
                         $this->msgAdd($text, false, false);
                         $msg->setStatus('R');
                         $this->ipcKernelConnection->execAsync('msgDbMsgUpdate', array($msg));
                     }
                     $this->msgAdd('----- MESSAGE END -----', false, true);
                 } else {
                     $this->msgAdd();
                     $this->msgAdd('ERROR: could not read msg "' . $args[1] . '".', false, true);
                 }
             } else {
                 $this->msgAdd();
                 $this->msgAdd('ERROR: you must specify a msg number or ID.', false, true);
             }
         }
     } else {
         $format = '%3d %1s %36s %s %s';
         $this->msgAdd();
         $this->msgAdd('  # N FROM                                 CRATED              RECEIVED', false, true);
         $no = 0;
         foreach ($msgs as $msgId => $msg) {
             $no++;
             $dateCreated = new DateTime();
             $dateCreated->setTimestamp($msg->getTimeCreated());
             $dateReceived = new DateTime();
             $dateReceived->setTimestamp($msg->getTimeReceived());
             $line = sprintf($format, $no, $msg->getStatus() == 'U' ? '*' : ' ', $msg->getSrcNodeId(), $dateCreated->format('Y-m-d H:i:s'), $dateReceived->format('Y-m-d H:i:s'));
             $this->msgAdd($line, false, false);
         }
         $this->msgAdd('END OF LIST', false, true);
     }
 }
Example #3
0
 public function testSendMsg()
 {
     list($client1, $client2) = $this->sendGenTestDataDefault();
     // Send Msg before ID should cause an error.
     $msg = new Msg();
     $msg->setVersion(1);
     $msg->setSrcNodeId($client1->getSettings()->data['node']['id']);
     $msg->setSrcSslKeyPub(static::NODE_LOCAL_SSL_KEY_PUB1);
     $msg->setSrcUserNickname('thefox');
     $msg->setDstNodeId($client2->getSettings()->data['node']['id']);
     $msg->setDstSslPubKey(static::NODE_LOCAL_SSL_KEY_PUB2);
     $msg->setSubject('my first subject');
     $msg->setText('hello world! this is a test');
     $msg->setSslKeyPrv(static::NODE_LOCAL_SSL_KEY_PRV1, 'my_password');
     $msg->encrypt();
     $raw = $client1->sendMsg($msg);
     #ve($raw);
     $raw = $client2->dataRecv($raw);
     #ve($raw);
     $json = $this->rawMsgToJson($raw);
     #ve($json);
     $this->assertEquals('error', $json[0]['name']);
     $this->assertEquals(1000, $json[0]['data']['code']);
     $this->sendClientsId($client1, $client2);
     // Send Msg
     $msg = new Msg();
     $msg->setVersion(1);
     #$msg->setId('200b9758-2d34-4152-8ada-fc09fc9c9da0');
     $msg->setSrcNodeId('cafed00d-2131-4159-8e11-0b4dbadb1738');
     $msg->setSrcSslKeyPub(static::NODE_LOCAL_SSL_KEY_PUB1);
     $msg->setSrcUserNickname('thefox');
     $msg->setDstNodeId('cafed00d-2131-4159-8e11-0b4dbadb1739');
     $msg->setDstSslPubKey(static::NODE_LOCAL_SSL_KEY_PUB2);
     $msg->setSubject('my first subject');
     $msg->setText('hello world! this is a test');
     $msg->setSslKeyPrv(static::NODE_LOCAL_SSL_KEY_PRV1, 'my_password');
     $msg->encrypt();
     $raw = $client1->sendMsg($msg);
     $json = $this->rawMsgToJson($raw);
     #ve($json);
     $rid = $json[0]['data']['rid'];
     $this->assertEquals('msg', $json[0]['name']);
     $this->assertEquals('cafed00d-2131-4159-8e11-0b4dbadb1738', $json[0]['data']['srcNodeId']);
     $this->assertEquals('cafed00d-2131-4159-8e11-0b4dbadb1739', $json[0]['data']['dstNodeId']);
     $raw = $client2->dataRecv($raw);
     $json = $this->rawMsgToJson($raw);
     #ve($json);
     $this->assertEquals('msg_response', $json[0]['name']);
     $this->assertEquals($rid, $json[0]['data']['rid']);
     $client1->getServer()->getKernel()->shutdown();
     $client2->getServer()->getKernel()->shutdown();
 }
Example #4
0
 public function mailNew($event, $from, $rcpt, $mail)
 {
     #fwrite(STDOUT, 'mail new: /'.$from.'/ a/'.join('/ /', $rcpt).'/'."\n");
     #$this->log->debug('mailNew: '.$event->getTrigger().' /'.$from.'/');
     $settings = $this->getSettings();
     $table = $this->ipcKernelConnection->execSync('getTable');
     $text = $mail->getBody();
     foreach ($rcpt as $dstNodeId) {
         $dstNodeId = substr($dstNodeId, 0, strpos($dstNodeId, '@'));
         #fwrite(STDOUT, 'to: /'.$dstNodeId.'/'."\n");
         $msg = new Msg();
         $msg->setSrcNodeId($settings->data['node']['id']);
         $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub());
         $msg->setSrcUserNickname($settings->data['user']['nickname']);
         $dstNode = new Node();
         $dstNode->setIdHexStr($dstNodeId);
         $msg->setDstNodeId($dstNode->getIdHexStr());
         if ($oDstNode = $table->nodeFind($dstNode)) {
             #print 'found node in table'.PHP_EOL;
             $msg->setDstSslPubKey($oDstNode->getSslKeyPub());
         }
         #else{ print 'node not found'.PHP_EOL; }
         $msg->setSubject($mail->getSubject());
         $msg->setText($text);
         $msg->setSslKeyPrvPath($settings->data['node']['sslKeyPrvPath'], $settings->data['node']['sslKeyPrvPass']);
         $msg->setStatus('O');
         $encrypted = false;
         #print 'DstSslPubKey: '.strlen($msg->getDstSslPubKey()).PHP_EOL;
         if ($msg->getDstSslPubKey()) {
             #print 'use dst key'.PHP_EOL;
             $msg->setEncryptionMode('D');
         } else {
             // Encrypt with own public key
             // while destination public key is not available.
             #print 'use local key'.PHP_EOL;
             $msg->setEncryptionMode('S');
             $msg->setDstSslPubKey($table->getLocalNode()->getSslKeyPub());
         }
         try {
             $encrypted = $msg->encrypt();
             if ($encrypted) {
                 $this->ipcKernelConnection->execAsync('msgDbMsgAdd', array($msg));
                 $this->log->debug('OK: msg created ' . $msg->getId());
             } else {
                 $this->log->error('Could not encrypt message.');
             }
         } catch (Exception $e) {
             $this->log->error('ERROR: ' . $e->getMessage());
         }
     }
 }