def __init__(self, username, password, port): ''' __init__(None) : Input : None Output : None Purpose : Constructor which initializes the Connection object 1) Reads The CLIENT.conf file and sets up essential variables 2) Reads the public_key.pem file and obtains the servers public key 3) Creates socket to talk to server ''' self.__readConfigFile() self.__username = username self.__destHostKey = {} # {Address,Key} self.__convertPasswordToSecret(password) self.__diffi = DH.DiffieHellman() self.__serverNonceHistory = [] self.__addressUserNameMap = {} self.__pubKey = self.__diffi.gen_public_key() global serverPort serverPort = int(port) try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) except Exception as e: print "Error while creating socket", e sys.exit(0) with open("public_key.pem", "rb") as key_file: try: self.serverPublicKey = serialization.load_pem_public_key( key_file.read(), backend=default_backend()) except Exception as e: print "Error while loading key ", e sys.exit(0)
def __init__(self): ''' __init__(None): Input : None Output : None Purpose : 1) Initialise objects to maintain connection state 2) Read server private key for future use ''' self.__diffiObj = DH.DiffieHellman() self.__authDict = {} self.__sessionKeyDict = {} # username : [key,address] self.__userNonceHistor = {} # nonce : bool self.__connectedClients = {} # address : username with open("private_key.pem", "rb") as key_file: try: self.__privateKey = serialization.load_pem_private_key( key_file.read(), password=None, backend=default_backend()) except: print "Error while Loading key " + file sys.exit(0)