From 2a221f180ebf6f86e3709401804108fcda65184f Mon Sep 17 00:00:00 2001
From: marvin-borner@live.com
Date: Wed, 11 Apr 2018 18:36:44 +0200
Subject: Started implementing chat encryption
---
assets/js/chat.js | 34 ++++++-
assets/js/main.js | 8 +-
assets/php/Chatserver/bin/WebChatServer.php | 18 ++++
assets/php/Chatserver/bin/server.csr | 18 ++++
assets/php/Chatserver/src/ChatProcessor.php | 118 +++++++++++++++++++++++++
assets/php/SavePublicKey.php | 17 ++++
assets/php/bin/WebChatServer.php | 18 ----
assets/php/bin/server.csr | 18 ----
assets/php/composer.json | 2 +-
assets/php/src/ChatProcessor.php | 118 -------------------------
assets/php/vendor/composer/autoload_psr4.php | 2 +-
assets/php/vendor/composer/autoload_static.php | 2 +-
12 files changed, 209 insertions(+), 164 deletions(-)
create mode 100644 assets/php/Chatserver/bin/WebChatServer.php
create mode 100644 assets/php/Chatserver/bin/server.csr
create mode 100644 assets/php/Chatserver/src/ChatProcessor.php
create mode 100644 assets/php/SavePublicKey.php
delete mode 100644 assets/php/bin/WebChatServer.php
delete mode 100644 assets/php/bin/server.csr
delete mode 100644 assets/php/src/ChatProcessor.php
(limited to 'assets')
diff --git a/assets/js/chat.js b/assets/js/chat.js
index 9543f87..9acff1d 100644
--- a/assets/js/chat.js
+++ b/assets/js/chat.js
@@ -1,10 +1,42 @@
+/************
+ GENERATE KEYS
+ ************/
+if (localStorage.getItem('KeysGenerated') === null || localStorage.getItem('KeysGenerated') !== "true") {
+ // GENERATE
+ var EncryptionPhrase = "PASSWORD 123"; // THE USERS PASSWORD
+ var RSABitLength = 1024;
+ var PrivateKeyString = cryptico.generateRSAKey(EncryptionPhrase, RSABitLength);
+ var PublicKeyString = cryptico.publicKeyString(PrivateKeyString);
+ // SAVE TO DATABASE
+ $.ajax({
+ type: "POST",
+ url: "assets/php/SavePublicKey.php",
+ data: {
+ UserID: "1", // TEMPORARY
+ PublicKeyString: PublicKeyString
+ },
+ async: true,
+ error: function () {
+ console.error("Error while saving public key to database!");
+ },
+ success: function () {
+ localStorage.setItem('KeysGenerated', "true");
+ }
+ });
+}
+
+
+/******
+ GENERAL
+ ******/
+
var ChatTextInput = $("#ChatTextInput");
var SubscribeTextInput = $("#SubscribeTextInput");
var ChatResponses = $("#ChatResponses");
var WebSocket = new WebSocket('wss://marvinborner.ddnss.de:1337');
WebSocket.onopen = function () {
- //console.log("Chat connection established!");
+ console.log("Chat connection established!");
};
WebSocket.onmessage = function (e) {
var MessageObject = JSON.parse(e.data);
diff --git a/assets/js/main.js b/assets/js/main.js
index b9cfe34..aa0f76b 100644
--- a/assets/js/main.js
+++ b/assets/js/main.js
@@ -1,10 +1,6 @@
-/******
- GENERAL
- ******/
-
/*****
- NAVBAR
- *****/
+NAVBAR
+*****/
var $el, leftPos, newWidth,
$mainNav = $(".Navbar");
$mainNav.append("");
diff --git a/assets/php/Chatserver/bin/WebChatServer.php b/assets/php/Chatserver/bin/WebChatServer.php
new file mode 100644
index 0000000..15f573b
--- /dev/null
+++ b/assets/php/Chatserver/bin/WebChatServer.php
@@ -0,0 +1,18 @@
+run();
\ No newline at end of file
diff --git a/assets/php/Chatserver/bin/server.csr b/assets/php/Chatserver/bin/server.csr
new file mode 100644
index 0000000..8523b82
--- /dev/null
+++ b/assets/php/Chatserver/bin/server.csr
@@ -0,0 +1,18 @@
+-----BEGIN CERTIFICATE REQUEST-----
+MIICzTCCAbUCAQAwgYcxCzAJBgNVBAYTAkRFMRMwEQYDVQQIDApTb21lLVN0YXRl
+MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxHjAcBgNVBAMMFW1h
+cnZpbmJvcm5lci5kZG5zcy5kZTEgMB4GCSqGSIb3DQEJARYRbWFydmluQGJvcm5l
+cnMuZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDjNPmJb5tz9xp+
+h9Jt7Go+VQ31oEu/FINxxW9jamPvvAzcBs82YKoLuOb1NHJX0wWLDAoCwXRn/vpP
+pp2OPicl4ACFKOUoKLUj4XXpsFMNsfV5qT4z0rte8P3RYcwbVWlscNekVMbgA6DF
+PXvOqZWHgJEZe8UoVP0Pj9J7/xcjY+VpfBhHAEimOipBVxijFAtnOzJ2o1M58xjZ
+3XH2BmeSVbjmhSgIbB50zcATKcVudSvaj3hnGmnfwhPRpO2UOvBrrQubwRoTGyvV
+TsW6dGGVY/qOJIyBcddayaZJraozBjDwp43c9S44DSotEgeLtvAyceQ27WDl37bl
+eEEyMJpvAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEATQ8QnGSJmoudHnYuG8t2
+SHQOxp4R3RcsgXRtXSm3tFlIAvvvixqiqVBfppxK6f6cf4B8tagmXNdblCK8dbWn
+9OIBxEh/hrooCtzydXcfvaJaFFaqPQ5YMyNNTtwLHd14w2hSSixNcON+YjfVY2ZA
+CdlR/pEDv/xSCABaTNecvSUYBHWsbmo7pMSKBZ/wI0UcgJHbB4PhCjuLTdB79RTp
+RJsMzA9ISZd6dLFxkmnOgmq2+sX3xTRK5XSrNfkE9Veou26BHeCwSBA25JVkatqC
+DaSuNw3a9SY28Tk1GPjhrKaX8lI40aViWtnuwW36jD3G2GMyXbo4K8hST0wI6gHi
+PQ==
+-----END CERTIFICATE REQUEST-----
diff --git a/assets/php/Chatserver/src/ChatProcessor.php b/assets/php/Chatserver/src/ChatProcessor.php
new file mode 100644
index 0000000..da78c9b
--- /dev/null
+++ b/assets/php/Chatserver/src/ChatProcessor.php
@@ -0,0 +1,118 @@
+clients = new \SplObjectStorage;
+ $this->subscriptions = [];
+ $this->users = [];
+ $this->connectedUsersNames = [];
+ }
+
+ public function onOpen(ConnectionInterface $conn) {
+ $generator = new Alliteration();
+ $this->clients->attach($conn);
+ $this->users[$conn->resourceId] = $conn;
+ $this->connectedUsersNames[$conn->resourceId] = $generator->getName();
+ }
+
+ /*public function onMessage(ConnectionInterface $from, $msg) {
+ $numRecv = count($this->clients) - 1;
+ echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
+ , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
+
+ foreach ($this->clients as $client) {
+ if ($from === $client) {
+ $client->send("You - " . $msg);
+ } else {
+ $client->send("" . $from->resourceId . " - " . $msg);
+ }
+ }
+ }
+ */
+
+ public function onMessage(ConnectionInterface $conn, MessageInterface $msg) {
+ $data = json_decode($msg);
+ switch ($data->ClientMessageType) {
+ case "Subscribe":
+ $this->subscriptions[$conn->resourceId] = $data->Channel;
+ foreach ($this->subscriptions as $id => $channel) {
+ if ($this->subscriptions[$conn->resourceId] == $channel) {
+ $MessageObject = new \stdClass();
+ $MessageObject->ServerMessage = true;
+ $MessageObject->ServerMessageType = "GroupJoin";
+ $MessageObject->GroupName = $channel;
+ $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
+ if ($id === $conn->resourceId) {
+ $MessageObject->WasHimself = true;
+ } else {
+ $MessageObject->WasHimself = false;
+ }
+ $MessageJson = json_encode($MessageObject, true);
+ $this->users[$id]->send($MessageJson);
+ }
+ }
+ break;
+ case "Message":
+ if (isset($this->subscriptions[$conn->resourceId])) {
+ $target = $this->subscriptions[$conn->resourceId];
+ foreach ($this->subscriptions as $id => $channel) {
+ if ($channel == $target) {
+ $MessageObject = new \stdClass();
+ $MessageObject->ServerMessage = false;
+ $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
+ $MessageObject->Message = htmlspecialchars($data->Message);
+ if ($id === $conn->resourceId) {
+ $MessageObject->WasHimself = true;
+ } else {
+ $MessageObject->WasHimself = false;
+ }
+ $MessageJson = json_encode($MessageObject, true);
+ $this->users[$id]->send($MessageJson);
+ }
+ }
+ }
+ }
+ }
+
+ public function onClose(ConnectionInterface $conn) {
+ $this->clients->detach($conn);
+ foreach ($this->clients as $client) {
+ if (isset($this->subscriptions[$conn->resourceId])) {
+ $target = $this->subscriptions[$conn->resourceId];
+ foreach ($this->subscriptions as $id => $channel) {
+ if ($channel == $target) {
+ $MessageObject = new \stdClass();
+ $MessageObject->ServerMessage = true;
+ $MessageObject->ServerMessageType = "UserDisconnect";
+ $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
+ $MessageJson = json_encode($MessageObject, true);
+ $this->users[$id]->send($MessageJson);
+ }
+ }
+ }
+ }
+ unset($this->users[$conn->resourceId]);
+ unset($this->subscriptions[$conn->resourceId]);
+ unset($this->connectedUsersNames[$conn->resourceId]);
+ }
+
+ public function onError(ConnectionInterface $conn, \Exception $e) {
+ echo "An error has occurred: {$e->getMessage()}\n";
+
+ $conn->close();
+ }
+}
\ No newline at end of file
diff --git a/assets/php/SavePublicKey.php b/assets/php/SavePublicKey.php
new file mode 100644
index 0000000..725a005
--- /dev/null
+++ b/assets/php/SavePublicKey.php
@@ -0,0 +1,17 @@
+prepare("SELECT count(*) FROM `PublicKeys` WHERE UserID = :UserID");
+ $CheckIfAlreadySetStmt->bindValue(':UserID', $_POST['UserID']);
+ $CheckIfAlreadySetStmt->execute();
+ $CheckIfAlreadySetRes = $CheckIfAlreadySetStmt->fetchColumn();
+ if ($CheckIfAlreadySetRes == 1) {
+ $UpdatePublicKeyStmt = $conn->prepare("UPDATE `PublicKeys` SET PublicKeyString = :PublicKeyString WHERE UserID = :UserID");
+ $UpdatePublicKeyStmt->execute(array('PublicKeyString' => $_POST["PublicKeyString"], 'UserID' => $_POST["UserID"]));
+ } else if ($CheckIfAlreadySetRes == 0) {
+ $InsertPublicKeyStmt = $conn->prepare("INSERT INTO `PublicKeys` (UserID, PublicKeyString) VALUES (:UserID, :PublicKeyString)");
+ $InsertPublicKeyStmt->execute(array('PublicKeyString' => $_POST["PublicKeyString"], 'UserID' => $_POST["UserID"]));
+ }
+} else {
+ http_response_code(400);
+}
\ No newline at end of file
diff --git a/assets/php/bin/WebChatServer.php b/assets/php/bin/WebChatServer.php
deleted file mode 100644
index bebd7b9..0000000
--- a/assets/php/bin/WebChatServer.php
+++ /dev/null
@@ -1,18 +0,0 @@
-run();
diff --git a/assets/php/bin/server.csr b/assets/php/bin/server.csr
deleted file mode 100644
index 8523b82..0000000
--- a/assets/php/bin/server.csr
+++ /dev/null
@@ -1,18 +0,0 @@
------BEGIN CERTIFICATE REQUEST-----
-MIICzTCCAbUCAQAwgYcxCzAJBgNVBAYTAkRFMRMwEQYDVQQIDApTb21lLVN0YXRl
-MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxHjAcBgNVBAMMFW1h
-cnZpbmJvcm5lci5kZG5zcy5kZTEgMB4GCSqGSIb3DQEJARYRbWFydmluQGJvcm5l
-cnMuZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDjNPmJb5tz9xp+
-h9Jt7Go+VQ31oEu/FINxxW9jamPvvAzcBs82YKoLuOb1NHJX0wWLDAoCwXRn/vpP
-pp2OPicl4ACFKOUoKLUj4XXpsFMNsfV5qT4z0rte8P3RYcwbVWlscNekVMbgA6DF
-PXvOqZWHgJEZe8UoVP0Pj9J7/xcjY+VpfBhHAEimOipBVxijFAtnOzJ2o1M58xjZ
-3XH2BmeSVbjmhSgIbB50zcATKcVudSvaj3hnGmnfwhPRpO2UOvBrrQubwRoTGyvV
-TsW6dGGVY/qOJIyBcddayaZJraozBjDwp43c9S44DSotEgeLtvAyceQ27WDl37bl
-eEEyMJpvAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEATQ8QnGSJmoudHnYuG8t2
-SHQOxp4R3RcsgXRtXSm3tFlIAvvvixqiqVBfppxK6f6cf4B8tagmXNdblCK8dbWn
-9OIBxEh/hrooCtzydXcfvaJaFFaqPQ5YMyNNTtwLHd14w2hSSixNcON+YjfVY2ZA
-CdlR/pEDv/xSCABaTNecvSUYBHWsbmo7pMSKBZ/wI0UcgJHbB4PhCjuLTdB79RTp
-RJsMzA9ISZd6dLFxkmnOgmq2+sX3xTRK5XSrNfkE9Veou26BHeCwSBA25JVkatqC
-DaSuNw3a9SY28Tk1GPjhrKaX8lI40aViWtnuwW36jD3G2GMyXbo4K8hST0wI6gHi
-PQ==
------END CERTIFICATE REQUEST-----
diff --git a/assets/php/composer.json b/assets/php/composer.json
index 7c3318b..8406940 100644
--- a/assets/php/composer.json
+++ b/assets/php/composer.json
@@ -1,7 +1,7 @@
{
"autoload": {
"psr-4": {
- "Websocket\\": "src"
+ "Websocket\\": "Chatserver/src"
}
},
"require": {
diff --git a/assets/php/src/ChatProcessor.php b/assets/php/src/ChatProcessor.php
deleted file mode 100644
index da78c9b..0000000
--- a/assets/php/src/ChatProcessor.php
+++ /dev/null
@@ -1,118 +0,0 @@
-clients = new \SplObjectStorage;
- $this->subscriptions = [];
- $this->users = [];
- $this->connectedUsersNames = [];
- }
-
- public function onOpen(ConnectionInterface $conn) {
- $generator = new Alliteration();
- $this->clients->attach($conn);
- $this->users[$conn->resourceId] = $conn;
- $this->connectedUsersNames[$conn->resourceId] = $generator->getName();
- }
-
- /*public function onMessage(ConnectionInterface $from, $msg) {
- $numRecv = count($this->clients) - 1;
- echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
- , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
-
- foreach ($this->clients as $client) {
- if ($from === $client) {
- $client->send("You - " . $msg);
- } else {
- $client->send("" . $from->resourceId . " - " . $msg);
- }
- }
- }
- */
-
- public function onMessage(ConnectionInterface $conn, MessageInterface $msg) {
- $data = json_decode($msg);
- switch ($data->ClientMessageType) {
- case "Subscribe":
- $this->subscriptions[$conn->resourceId] = $data->Channel;
- foreach ($this->subscriptions as $id => $channel) {
- if ($this->subscriptions[$conn->resourceId] == $channel) {
- $MessageObject = new \stdClass();
- $MessageObject->ServerMessage = true;
- $MessageObject->ServerMessageType = "GroupJoin";
- $MessageObject->GroupName = $channel;
- $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
- if ($id === $conn->resourceId) {
- $MessageObject->WasHimself = true;
- } else {
- $MessageObject->WasHimself = false;
- }
- $MessageJson = json_encode($MessageObject, true);
- $this->users[$id]->send($MessageJson);
- }
- }
- break;
- case "Message":
- if (isset($this->subscriptions[$conn->resourceId])) {
- $target = $this->subscriptions[$conn->resourceId];
- foreach ($this->subscriptions as $id => $channel) {
- if ($channel == $target) {
- $MessageObject = new \stdClass();
- $MessageObject->ServerMessage = false;
- $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
- $MessageObject->Message = htmlspecialchars($data->Message);
- if ($id === $conn->resourceId) {
- $MessageObject->WasHimself = true;
- } else {
- $MessageObject->WasHimself = false;
- }
- $MessageJson = json_encode($MessageObject, true);
- $this->users[$id]->send($MessageJson);
- }
- }
- }
- }
- }
-
- public function onClose(ConnectionInterface $conn) {
- $this->clients->detach($conn);
- foreach ($this->clients as $client) {
- if (isset($this->subscriptions[$conn->resourceId])) {
- $target = $this->subscriptions[$conn->resourceId];
- foreach ($this->subscriptions as $id => $channel) {
- if ($channel == $target) {
- $MessageObject = new \stdClass();
- $MessageObject->ServerMessage = true;
- $MessageObject->ServerMessageType = "UserDisconnect";
- $MessageObject->Username = $this->connectedUsersNames[$conn->resourceId];
- $MessageJson = json_encode($MessageObject, true);
- $this->users[$id]->send($MessageJson);
- }
- }
- }
- }
- unset($this->users[$conn->resourceId]);
- unset($this->subscriptions[$conn->resourceId]);
- unset($this->connectedUsersNames[$conn->resourceId]);
- }
-
- public function onError(ConnectionInterface $conn, \Exception $e) {
- echo "An error has occurred: {$e->getMessage()}\n";
-
- $conn->close();
- }
-}
\ No newline at end of file
diff --git a/assets/php/vendor/composer/autoload_psr4.php b/assets/php/vendor/composer/autoload_psr4.php
index 7240c63..5c1a135 100644
--- a/assets/php/vendor/composer/autoload_psr4.php
+++ b/assets/php/vendor/composer/autoload_psr4.php
@@ -6,7 +6,7 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
- 'Websocket\\' => array($baseDir . '/src'),
+ 'Websocket\\' => array($baseDir . '/Chatserver/src'),
'Symfony\\Polyfill\\Php70\\' => array($vendorDir . '/symfony/polyfill-php70'),
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Symfony\\Component\\Routing\\' => array($vendorDir . '/symfony/routing'),
diff --git a/assets/php/vendor/composer/autoload_static.php b/assets/php/vendor/composer/autoload_static.php
index e598fbf..e3cbb9e 100644
--- a/assets/php/vendor/composer/autoload_static.php
+++ b/assets/php/vendor/composer/autoload_static.php
@@ -56,7 +56,7 @@ class ComposerStaticInit3c5661e077098f105cbab5a541fd4883
public static $prefixDirsPsr4 = array (
'Websocket\\' =>
array (
- 0 => __DIR__ . '/../..' . '/src',
+ 0 => __DIR__ . '/../..' . '/Chatserver/src',
),
'Symfony\\Polyfill\\Php70\\' =>
array (
--
cgit v1.2.3