-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorityServer.py
More file actions
73 lines (56 loc) · 2.42 KB
/
authorityServer.py
File metadata and controls
73 lines (56 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import socket
import ssl
import core.authorityRoles as authorityRoles
import yaml
import netifaces
try:
normal_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket successfully created")
except socket.error as err:
print ("socket creation failed with error %s" %(err))
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile='./certificates/ca_certificate.pem', keyfile="./certificates/ca_private_key.pem")
# create an instance of authority
auth = authorityRoles.Authority()
normal_socket.bind(("0.0.0.0",3002))
normal_socket.listen(5)
ssl_socket = ssl_context.wrap_socket(normal_socket, server_side=True)
with open('config.yaml', "r") as config:
data = yaml.safe_load(config)
interfaces = netifaces.interfaces()
for interface in interfaces:
addresses = netifaces.ifaddresses(interface)
if netifaces.AF_INET in addresses:
ip_addresses = [addr['addr'] for addr in addresses[netifaces.AF_INET]]
if(ip_addresses[0][:9]=="10.10.100"):
AUTH_IP_ADDR = ip_addresses[0]
# Define IP Addresses and PORTS
# AUTH_IP_ADDR = socket.gethostbyname(socket.gethostname())
AUTH_PORT = 3002
data['pkg']['ip'] = AUTH_IP_ADDR
data['pkg']['hostname'] = socket.gethostname()
with open('config.yaml', "w") as config:
yaml.safe_dump(data, config)
# now the socket will listen to calls from the network
print("Authority is listening securely at IP {} PORT {}".format(AUTH_IP_ADDR, AUTH_PORT))
while True:
conn, addr = ssl_socket.accept()
print("Got a connection from", addr)
# Authority sends public data and welcome message to user
conn.send(bytes("---------------------------------",'utf-8'))
conn.send(bytes("Welcome to Central Authority",'utf-8'))
conn.send(bytes("---------------------------------\n",'utf-8'))
conn.send(bytes("|| Public Parameters || M = {} || ".format(auth.send_MPK()),'utf-8'))
conn.send(bytes("Please enter your identity",'utf-8'))
conn.send(bytes("{}".format(auth.send_MPK()),'utf-8'))
identity = conn.recv(1024).decode('utf-8')
if(identity=="CLOSE"):
conn.close()
else:
# identity recieved as string from the client
conn.send(bytes("---- Secret key for your use is sent || KEEP IT SAFE ----",'utf-8'))
secret_key = auth.keyGeneration(identity_string=identity)
print(secret_key)
conn.send(bytes(str(secret_key),'utf-8'))
conn.close()
ssl_socket.close()