Пример #1
0
 def callback(self, conn, addr):
     print("[Bob]" + str(addr) + " is now connected to Bob")
     cipher_rsa = PKCS1_OAEP.new(self.key_pair)
     # Bob wants only to talk with Alice or Cert
     who = unpad(conn.recv(2000))
     if (who == b'Cert'):
         try:
             ## Recive AB Key and check everything
             self.ab_key = cipher_rsa.decrypt(conn.recv(256))
             self.a_timestamp = parser.parse(
                 cipher_rsa.decrypt(conn.recv(256)).decode())
             alice_port = int(cipher_rsa.decrypt(conn.recv(256)).decode())
             # One minute tollerance
             assert (datetime.now().strftime("%H:%M") ==
                     self.a_timestamp.strftime("%H:%M"))
             print("[Bob] Got AB_key ")
             print("[Bob] Got Alice port")
             ## To Alice
             self.connect_to_alice(alice_port)
         except:
             print("[Bob] Never saw you around")
     elif (who == b'Alice'):
         print("[Bob] wait for me Alice")
     else:
         print("[Bob] i dont know u, see ya")
         return
     return
Пример #2
0
    def connect_to_cert(self):
        cert_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        cert_socket.connect(('127.0.0.1', 9000))
        print("[Alice] Now connected to Cert Authority")
        cert_socket.send(pad(b'Alice'))
        b = cert_socket.recv(2000)
        b = unpad(b)
        if(b == b'send'):
            cert_socket.send(pad(self.key_pair.publickey().export_key()))
            b=unpad(cert_socket.recv(2000))
        if(b == b'nonce,ts'):
            nonce = random.randint(1,20000)
            timestamp = datetime.now()
            self.timestamp = timestamp
            cert_socket.send(pad(bytes(str(nonce)+","+format(timestamp),"utf-8")))
        try:
            cipher_rsa = PKCS1_OAEP.new(self.key_pair)
        
            ## my nonce
            nonce_c        = cert_socket.recv(256)
            ## my timestamp
            timestamp_c    = cert_socket.recv(256)
            ## c pub key
            self.c_pub_key = RSA.import_key(unpad(cert_socket.recv(2000)))
            ## checksum
            checksum       = cert_socket.recv(256)
            
            # check integrity
            nonce_d        = cipher_rsa.decrypt(nonce_c)
            timestamp_d    = parser.parse(cipher_rsa.decrypt(timestamp_c).decode())
            hash_object    = SHA256.new(data=nonce_c)
            hash_object.update(data=timestamp_c)
            
            assert(int(nonce_d)==nonce)
            assert(hash_object.digest() == checksum)
            assert(timestamp_d.strftime("%H:%M:%S")==timestamp.strftime("%H:%M:%S"))

            ## Send Symmetric-Key to C and wait for Bob
            cipher_rsa_c = PKCS1_OAEP.new(self.c_pub_key)
            cert_socket.send(cipher_rsa_c.encrypt(self.ab_key))
            cert_socket.send(cipher_rsa_c.encrypt(str(self.PORT).encode()))
        except:
            print("[Alice] See Ya")
            return
        print("[Alice] wait for Bob")
        return
Пример #3
0
 def callback(self,conn,addr):
     print("[Alice]"+str(addr) + " is now connected to Alice")
     data = unpad(conn.recv(2000))
     if(data == b'Bob'):
         cipher     = AES.new(self.ab_key, AES.MODE_ECB)
         timestamp_c = conn.recv(16)
         timestamp_d = parser.parse(Padding.unpad(cipher.decrypt(timestamp_c),AES.block_size).decode())
         assert(timestamp_d.strftime("%H:%M:%S")==self.timestamp.strftime("%H:%M:%S"))
         print("[Alice] You are Bob 100% sure")
     return
Пример #4
0
 def connect_to_cert(self):
     cert_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     cert_socket.connect(('127.0.0.1', 9000))
     print("[Bob] Now connected to Cert Authority")
     cert_socket.send(pad(b'Bob'))
     mess = cert_socket.recv(2000)
     mess = unpad(mess)
     if (mess == b'send'):
         cert_socket.send(pad(self.key_pair.publickey().export_key()))
     return
Пример #5
0
    def decrypt(self, ct):
        self.buffer = bytearray()  #리턴할 buffer
        self.ct = ct
        cnt = int(len(self.ct) / self.block_size)  #count
        '''
        복호화 진행
        '''
        for i in range(cnt):
            buf = self.ct[(self.block_size * i):(self.block_size * i) +
                          self.block_size]  #block 자르기
            dec = self.crypto.decrypt(buf)
            self.buffer += dec

        self.buffer = unpad(self.buffer)  #unpadding
        return self.buffer
Пример #6
0
    def callback(self, conn, addr):
        print("[Auth]" + str(addr) + " is now connected to Cert Authority")
        data = conn.recv(2000)
        data = unpad(data)
        decoded = data.decode("utf-8")
        ## If ALICE
        if (decoded == 'Alice'):

            ## Handshake
            #self.alice_port   = int(decoded.split("on")[1])
            self.alice_socket = conn
            self.alice_socket.send(pad(b'send'))
            self.alice_pub_key = RSA.import_key(
                unpad(self.alice_socket.recv(2000)))
            print("[Auth] got alice key")

            ## Recive Nonce and Timestamp
            time_now = datetime.now()
            self.alice_socket.send(pad(b'nonce,ts'))

            mess = self.alice_socket.recv(2000)

            try:
                nonce = mess.decode("utf-8").split(",")[0]
                timestamp = mess.decode("utf-8").split(",")[1]
                date_time_obj = parser.parse(timestamp)
                hms = date_time_obj.strftime("%H:%M:%S")
            except:
                print("[Auth] Timeout Expired")
                return

            ## Send back Encrypted Nonce and Timestamp and my pub_key
            cipher_rsa_c = PKCS1_OAEP.new(self.key_pair)
            cipher_rsa_ac = PKCS1_OAEP.new(self.alice_pub_key)
            cipher_rsa_bc = PKCS1_OAEP.new(self.bob_pub_key)
            if ((date_time_obj - time_now).seconds < 10):

                nonce_c = cipher_rsa_ac.encrypt(bytes(nonce, "utf-8"))
                a_timestamp = cipher_rsa_ac.encrypt(format(hms).encode())
                c_pub_key = self.key_pair.publickey().export_key()

                hash_object = SHA256.new(data=nonce_c)
                hash_object.update(data=a_timestamp)

                self.alice_socket.send(nonce_c)
                self.alice_socket.send(a_timestamp)
                self.alice_socket.send(pad(c_pub_key))
                self.alice_socket.send(hash_object.digest())

                ## Reciva AB key from A
                ab_key_enc = self.alice_socket.recv(256)
                ab_key = cipher_rsa_c.decrypt(ab_key_enc)
                self.alice_port = int(
                    cipher_rsa_c.decrypt(self.alice_socket.recv(256)).decode())
                print("[Auth] recvd key and port from Alice")

                ## Send AB Key and TS To bob
                self.bob_socket = socket.socket(socket.AF_INET,
                                                socket.SOCK_STREAM)
                self.bob_socket.connect(('127.0.0.1', 8000))
                self.bob_socket.send(pad(b'Cert'))
                self.bob_socket.send(cipher_rsa_bc.encrypt(ab_key))
                self.bob_socket.send(cipher_rsa_bc.encrypt(hms.encode()))
                self.bob_socket.send(
                    cipher_rsa_bc.encrypt(str(self.alice_port).encode()))

            else:
                print("[Auth] Timeout Expired")
                return
            return
        elif (decoded == 'Bob'):
            self.bob_socket = conn
            self.bob_socket.send(pad(b'send'))
            self.bob_pub_key = RSA.import_key(unpad(
                self.bob_socket.recv(2000)))
            print("[Auth] got bob key")
            return
        else:
            print("[Auth] Not identified! closing connection")
            return
        return