How to finish writing a DTLS Client/Server program pair

211 Views Asked by At

Keep in mind that I am very new to network programming, and am pretty much learning as I go. It has been as painful as it sounds.

  1. I have successfully created a UDP client/server "program pair" that can communicate with each other across public IPs, or a LAN setting.

  2. I want to move on to securing this "program pair" using DTLS.

  3. I need to use DTLS, because (to avoid getting into details) I need to use UDP for a project.

DTLS Server

import socket
import ssl
from dtls import do_patch
do_patch()

##### Define server IP and port #####
serverAddress = "192.168.1.158"
serverPort = 12345

##### Define UDP socket object #####
s = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))

##### Assign server IP and port to socket object #####
s.bind((serverAddress, serverPort))

#### Receive client data and IP address #####
data, addr = s.recvfrom(4096)

##### Send greeting and server IP address to client #####
serverMessage = bytes(f"Hello, I am the server at {serverAddress}".encode("utf-8"))
s.sendto(serverMessage, addr)

##### Display client IP address and port #####
print(f"Client Address: {addr}")

##### Maintain server in a continuous listening state #####
while True:

#### Receive client data and IP address #####
    data, addr = s.recvfrom(4096)

##### Decode and print client data #####
    #data = data.decode("utf-8")
    print(f"Client: {data}")

##### Display client IP address and port #####
    print(f"Client Address: {addr}")

##### If decoded data is !quit, encoded received data and return to client #####
    #if data == "!quit":
        #serverData = bytes(data.encode("utf-8"))
        #s.sendto(serverData, addr)
##### If decoded data is not !quit, then encode received data and return to client to avoid hanging client #####
   #else:
        #serverData = bytes(data.encode("utf-8"))
        #.sendto(serverData, addr)

The following is the UDP client using DTLS

import socket
import ssl
from dtls import do_patch

do_patch()

##### Define target server IP and port #####
serverAddress = "IP Address"
serverPort = 12345

##### Define UDP socket object #####
s = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
addr = (serverAddress, serverPort)

##### Assign target server IP and port to tuple object #####
addr = (serverAddress, serverPort)

##### Define and encode client message to send to target server #####
clientMessage = bytes("I am the UDP client".encode("utf-8"))

##### Send encoded client message to target server #####
s.sendto(clientMessage, addr)

##### Maintain client in continuous listening state to receive data from server #####

while True:

##### Receive any data transmitted from server, and server IP address #####
    data,addr = s.recvfrom(4096)

##### Decode any received data from server #####
    data = data.decode("utf-8")

##### Print decoded data and target server IP and port #####
    print(f"\n\nReceived server data: {data}\nTarget server IP address: {addr}")

##### If decoded data is !quit, close the socket and break out of the loop
    if data == "!quit":
        s.close()
        break

##### Receive user input to encode, transform to bytes, and send to the target server #####
    clientData = bytes(input("Enter data to transmit to server: ").encode("utf-8"))
    s.sendto(clientData, addr)

PROBLEM SCENARIO

The following is the problem I am experiencing.

  1. If the line s.connect() is included in the client code, Wireshark successfully picks up DTLS client hellos transmitting to the server. Additionally, the server continuously receives these messages from the client

Client: b"\x16\xfe\xfd\x00\x00\x00\x00\x00\x00\x00\x01\x00\xc6\x01\x00\x00\xba\x00\x00\x00\x00\x00\x00\x00\xba\xfe\xfd\x93\x1dX\t- \x00\x9f\xf4%\x04\xaf\xb3k\xdd\xd0\xbd\xa35\x02|!O\x9b\x08\x8b_\x9d\xaad^\x00\x00\x008\xc0,\xc00\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0+\xc0/\x00\x9e\xc0$\xc0(\x00k\xc0#\xc0'\x00g\xc0\n\xc0\x14\x009\xc0\t\xc0\x13\x003\x00\x9d\x00\x9c\x00=\x00<\x005\x00/\x00\xff\x01\x00\x00X\x00\x0b\x00\x04\x03\x00\x01\x02\x00\n\x00\x0c\x00\n\x00\x1d\x00\x17\x00\x1e\x00\x19\x00\x18\x00#\x00\x00\x00\x16\x00\x00\x00\x17\x00\x00\x00\r\x000\x00.\x04\x03\x05\x03\x06\x03\x08\x07\x08\x08\x08\t\x08\n\x08\x0b\x08\x04\x08\x05\x08\x06\x04\x01\x05\x01\x06\x01\x03\x03\x02\x03\x03\x01\x02\x01\x03\x02\x02\x02\x04\x02\x05\x02\x06\x02" Client Address: ('IP Address', 2328)

That is all that happens however. The client.py file never prompts me to input my own message to transmit to the server, and the client never receives the initial greeting from the server.

  1. If the line s.connect() is omitted from the client code, everything functions as normally as before, however it seems the data is unencrypted as I can see the transmitted data in plaintext in Wireshark.

I feel as though I am close to accomplishing what I want, which is a simple client/server socket communication program utilizing encryption over UDP with DTLS. I am not sure what I am doing wrong however, or what my next step should be.

0

There are 0 best solutions below