root 4 vuotta sitten
vanhempi
commit
5f113a6ea5
9 muutettua tiedostoa jossa 74 lisäystä ja 43 poistoa
  1. 13 12
      Utils.py
  2. 1 0
      command.py
  3. 1 2
      connection/broadcast.py
  4. 18 7
      connection/socket.py
  5. 3 2
      main.py
  6. 21 3
      oracle/worker/connector.py
  7. 7 8
      tls/__init__.py
  8. 8 7
      tls/key_schedule.py
  9. 2 2
      user/userParser.py

+ 13 - 12
Utils.py

@@ -26,47 +26,48 @@ CWHITE2 = "\33[97m"
 
 
 def simplePrint(a, b):
-    print("\n{CWHITE2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CWHITE2, "\t", a, " -> ", b, CEND, "\n")
 
 
 def yBackPrint(a, b):
-    print("\n{CYELLOWBG}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CYELLOWBG, "\t", a, " -> ", b, CEND, "\n")
 
 
 def gBackPrint(a, b):
-    print("\n{CGREENBG}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CGREENBG, "\t", a, " -> ", b, CEND, "\n")
 
 
 def rBackPrint(a, b):
-    print("\n{CREDBG}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CREDBG, "\t", a, " -> ", b, CEND, "\n")
 
 
 def bBackPrint(a, b):
-    print("\n{CBLUEBG}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CBLUEBG, "\t", a, " -> ", b, CEND, "\n")
 
 
 def vBackPrint(a, b):
-    print("\n{CVIOLETBG}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CVIOLETBG, "\t", a, " -> ", b, CEND, "\n")
 
 
 def yPrint(a, b):
-    print("\n{CYELLOW2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CYELLOW2, "\t", a, " -> ", b, CEND, "\n")
 
 
 def gPrint(a, b):
-    print("\n{CGREEN2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CGREEN2, "\t", a, " -> ", b, CEND, "\n")
 
 
 def rPrint(a, b):
-    print("\n{CRED2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CRED2, "\t", a, " -> ", b, CEND, "\n")
 
 
 def bPrint(a, b):
-    print("\n{CBLUE2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CBLUE2, "\t", a, " -> ", b, CEND, "\n")
 
 
 def vPrint(a, b):
-    print("\n{CVIOLET2}\t  {a} ->  {b} {CEND}\n")
+    print("\n", CVIOLET2, "\t", a, " -> ", b, CEND, "\n")
+
 
 def spacePrint():
-    print("\n\t*************************\n")
+    print("\n\t*************************\n")

+ 1 - 0
command.py

@@ -18,6 +18,7 @@ def install():
     os.system("echo 1 2 3 4 > ./Player-Data/Input-P0-0")
     os.system("echo 1 2 3 4 > ./Player-Data/Input-P1-0")
     os.system("./MP-SPDZ/Scripts/mascot.sh tutorial")
+    comp("xor")
 
 
 def comp(a):

+ 1 - 2
connection/broadcast.py

@@ -20,7 +20,7 @@ class Broadcast:
         while True:
             data, addr = self.socket.recvfrom(1024)
             d = self.parser.toJSON(data.decode("utf-8")[LenFunc:])
-            vPrint("receive", d, " \n\tfrom", addr, "\n\033[0m")
+            vPrint("receive", (d, addr))
             self.parser.parser(d, addr, self.child)
 
     def send(self, message, addr):
@@ -30,7 +30,6 @@ class Broadcast:
 
 class BroadcastServer(Broadcast):
     def __init__(self, broadcastPort, parser, id):
-
         """ ,broadcast """  # TODO Broadcast
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

+ 18 - 7
connection/socket.py

@@ -18,10 +18,18 @@ class SocketHTTP(threading.Thread):
             self.socket.connect((HOST, PORT))
             t = threading.Thread(target=self.receive)
             t.start()
-            gBackPrint("socket is created and Listener is created", (HOST, PORT))
+            gBackPrint(
+                "socket is created and Listener is created", (HOST, PORT))
         except Exception as e:
             rBackPrint("An exception occurred SocketHTTP __init__", e)
 
+    def close(self):
+        try:
+            self.socket.close()
+            vPrint("SocketHTTP", " CLOSED")
+        except Exception as e:
+            rBackPrint("An exception occurred SocketHTTP close", e)
+
     def send(self, M):
         try:
             self.socket.sendall(M)
@@ -35,8 +43,9 @@ class SocketHTTP(threading.Thread):
             while True:
                 data = self.socket.recv(65565)
                 bPrint("receive", data)
-                if bytes.hex(data) == "":
-                    continue
+                if bytes.hex(data) == b"":
+                    self.close
+                    break
                 self.parent.recv(bytes.hex(data))
         except Exception as e:
             rBackPrint("An exception occurred SocketHTTP receive", e)
@@ -55,15 +64,16 @@ class SocketClient(threading.Thread):
             self.socket.connect((HOST, PORT))
             t = threading.Thread(target=self.receive)
             t.start()
-            gBackPrint("socket is created and Listener is created\n")
+            gPrint("socket is created", " Listener is created")
         except Exception as e:
             rBackPrint("An exception occurred SocketClient __init__", e)
 
     def send(self, M):
         try:
             Message = bytes(M, "utf-8")
+            vPrint("Befor sendSocketHTTP Send", Message)
             self.socket.sendall(Message)
-            self.history.append(("send", Message))
+            # self.history.append(("send", Message))
             vPrint("SocketHTTP Send", M)
         except Exception as e:
             rBackPrint("An exception occurred SocketClient send", e)
@@ -92,7 +102,7 @@ class SocketServer:
             self.id = id
             t = threading.Thread(target=self.connection)
             t.start()
-            gBackPrint("socket is created and Listener is created", socketPort)
+            gPrint("socket is created and Listener is created", socketPort)
         except Exception as e:
             rBackPrint("An exception occurred SocketServer __init__", e)
 
@@ -105,7 +115,8 @@ class SocketServer:
                 while True:
                     conn, addr = self.socket.accept()
                     print("user connected to ", conn, "-> ", addr)
-                    u = Connection(conn, addr, self.socketPort + 1, self.parser)
+                    u = Connection(
+                        conn, addr, self.socketPort + 1, self.parser)
                     self.connected.append(u)
         except Exception as e:
             rBackPrint("An exception occurred SocketServer connection", e)

+ 3 - 2
main.py

@@ -1,7 +1,7 @@
 from optparse import OptionParser
 from oracle.oracle import Oracle
 from user.user import User
-from command import install, comp, compCrypto
+from command import install, comp
 import os
 from Utils import *
 
@@ -92,7 +92,8 @@ def main():
         install()
 
     elif options.oracle:
-        Oracle(broadcast=options.broadcast, broadcastPort=options.broadcastPort)
+        Oracle(broadcast=options.broadcast,
+               broadcastPort=options.broadcastPort)
 
     elif options.user:
         User(

+ 21 - 3
oracle/worker/connector.py

@@ -31,16 +31,30 @@ class Connector:
         self.auth_key = None
         self.last_key = None
         self.GhashCon = None
+        self.key = None
         self.cipher = b""
         stream = os.popen("pwd")
         output = stream.read()
         self.path = output[:-1]
         self.t = threading.Thread(target=self.toCryptor)
+        self.done = threading.Thread(target=self.done)
+        self.doneTime = 5
         self.t.start()
 
+    def done(self):
+        try:
+            while(self.doneTime > 0):
+                time.sleep(1)
+                self.doneTime = self.doneTime - 1
+            self.server.close()
+        except Exception as e:
+            rBackPrint("An exception occurred done", e)
+
     def recv(self, data):
         try:
             self.buffer.append(data)
+            if(self.h):
+                self.doneTime = self.doneTime + 1
         except Exception as e:
             rBackPrint("An exception occurred recv", e)
 
@@ -85,12 +99,14 @@ class Connector:
                 models.ClientExtension.server_names(self.serverDomain),
                 models.ClientExtension.supported_versions([b"\x03\x04"]),
                 models.ClientExtension.key_share(
-                    [models.KeyShareEntry(models.NamedGroup.x25519, self.key_exchange)]
+                    [models.KeyShareEntry(
+                        models.NamedGroup.x25519, self.key_exchange)]
                 ),
                 models.ClientExtension.signature_algorithms(
                     list(models.SignatureScheme)
                 ),
-                models.ClientExtension.supported_groups(list(models.NamedGroup)),
+                models.ClientExtension.supported_groups(
+                    list(models.NamedGroup)),
                 models.ClientExtension.psk_key_exchange_modes(
                     list(models.PskKeyExchangeMode)
                 ),
@@ -116,7 +132,8 @@ class Connector:
                 "fromContoCryHelloCLient",
             )
 
-            data = models.ContentType.handshake.tls_plaintext(self.client_hello_data)
+            data = models.ContentType.handshake.tls_plaintext(
+                self.client_hello_data)
             return bytes.hex(data)
         except Exception as e:
             rBackPrint("An exception occurred client_hello", e)
@@ -201,6 +218,7 @@ class Connector:
             gPrint("last_key ", self.last_key)
             self.GhashCon = GhashCon(self.auth_key, self.last_key)
             m = self.MPC()
+            self.done.start()
             self.server.send(bytes.fromhex(data["change"]))
             self.server.send(bytes.fromhex(data["finish"]))
             time.sleep(1)

+ 7 - 8
tls/__init__.py

@@ -14,7 +14,7 @@ from types import SimpleNamespace
 from nacl.public import PrivateKey
 from nacl.bindings import crypto_scalarmult
 import hashlib
-from tls.Crypto.PublicKey import RSA
+from Crypto.PublicKey import RSA
 
 # from Cryptography import x509
 # from OpenSSL.crypto import load_certificate, FILETYPE_ASN1
@@ -575,7 +575,7 @@ class TLSClientSession:
         self.client = None
         self.server = None
         self.session_tickets = []
-        gBackPrint("tls"," started")
+        gBackPrint("tls", " started")
 
     def unpack_server_hello(self, mv: memoryview):
         assert mv[:2] == b"\x03\x03", "version must be 0x0303"
@@ -662,7 +662,6 @@ class TLSClientSession:
                 raise Exception(f"unknown handshake type {handshake_type}")
             mv = mv[4+length:]
 
-
     def tls_response(self, mv: memoryview):
         head = memoryview(mv[:5])
         assert head[1:
@@ -705,7 +704,7 @@ class TLSClientSession:
                 b'' + bytes.fromhex(self.handshake_context.decode())
             )
             simplePrint("s_hand_traf_s",
-                  secret.hex())
+                        secret.hex())
 
             self.peer_cipher = TLSCipher(secret)
             simplePrint("peer_cipher", self.peer_cipher)
@@ -717,11 +716,11 @@ class TLSClientSession:
             )
             self.client_handshake_traffic_secret = client_handshake_traffic_secret
             simplePrint("c_hand_traf_s",
-                  client_handshake_traffic_secret.hex())
+                        client_handshake_traffic_secret.hex())
 
         elif head[0] == ContentType.application_data:
             plaintext = self.peer_cipher.decrypt(content, head).rstrip(b"\x00")
-            simplePrint("plaintext", plaintext.hex(), type(plaintext))
+            simplePrint("plaintext", (plaintext.hex(), type(plaintext)))
 
             content_type = ContentType.from_value(plaintext[-1])
             simplePrint(" content_type", content_type)
@@ -737,7 +736,8 @@ class TLSClientSession:
                     client_finished_data = HandshakeType.finished.pack_data(
                         client_finished
                     )
-                    simplePrint("client_finished_data", client_finished_data.hex())
+                    simplePrint("client_finished_data",
+                                client_finished_data.hex())
 
                     inner_plaintext = client_finished_data + b'\x16'
                     simplePrint("inner_plaintext", inner_plaintext.hex())
@@ -818,4 +818,3 @@ class TLSClientSession:
 
     def pack_canceled(self) -> bytes:
         return self.pack_warning(AlertDescription.user_canceled)
-

+ 8 - 7
tls/key_schedule.py

@@ -3,6 +3,7 @@ import hkdf
 import hashlib
 from Utils import *
 
+
 class TlsHash:
     def __init__(self, hashmod=hashlib.sha256):
         self.hashmod = hashmod
@@ -32,8 +33,8 @@ class TlsHash:
     def derive_secret(self, secret: bytes, label: bytes, messages) -> bytes:
         if type(messages) == list:
             messages = b"".join(messages)
-        simplePrint("\n\tHash", label, "\t", bytes(
-            self.hashmod(messages).digest()).hex())
+        simplePrint("Hash", (label, "", bytes(
+            self.hashmod(messages).digest()).hex()))
         return self.hkdf_expand_label(
             secret, label, self.hashmod(messages).digest(), self.hash_len
         )
@@ -102,21 +103,21 @@ class KeyScheduler:
         self.ecdhe = ecdhe
 
         self.early_secret = self.tls_hash.hkdf_extract(None, psk)
-        simplePrint("\n\tearly_secret\t", self.early_secret.hex())
+        simplePrint("early_secret", self.early_secret.hex())
 
         self.first_salt = self.tls_hash.derive_secret(
             self.early_secret, b"derived", b""
         )
-        simplePrint("\n\tderived_secret\t", self.first_salt.hex())
+        simplePrint("derived_secret", self.first_salt.hex())
         self.handshake_secret = self.tls_hash.hkdf_extract(
             self.first_salt, self.ecdhe)
-        simplePrint("\n\thandshake_sec\t", self.handshake_secret.hex())
+        simplePrint("handshake_sec", self.handshake_secret.hex())
         self.second_salt = self.tls_hash.derive_secret(
             self.handshake_secret, b"derived", b""
         )
-        simplePrint("\n\tsecond_salt\t", self.second_salt.hex())
+        simplePrint("second_salt", self.second_salt.hex())
         self.master_secret = self.tls_hash.hkdf_extract(self.second_salt, None)
-        simplePrint("\n\tmaster_secret\t", self.master_secret.hex())
+        simplePrint("master_secret", self.master_secret.hex())
 
     def client_handshake_traffic_secret(self, messages) -> bytes:
         return self.tls_hash.derive_secret(

+ 2 - 2
user/userParser.py

@@ -63,12 +63,12 @@ class UserParser(Parser):
         except Exception as e:
             rBackPrint("An exception occurred oracleInitialCryptor", e)
 
-    def oracleInitialBearer(self, connector, data):
+    def oracleInitialBearer(self, connector, d):
         try:
             data = {}
             self.add(data, "type", "oracleInitial")
             self.add(data, "connector", connector)
-            self.add(data, "data", data)
+            self.add(data, "data", d)
             return self.toSEND(data)
         except Exception as e:
             rBackPrint("An exception occurred oracleInitialBearer", e)