SSLTest.anubis
4.39 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
read tools/basis.anubis
read TestItem.anubis
define String
ssl_receive_message
(
SSL_Connection conn,
List(Int8) so_far
) =
if (Maybe(String))read(conn,1,60) is
{
failure then implode(reverse(so_far)),
success(str) then if nth(0,str) is
{
failure then implode(reverse(so_far)),
success(c) then
if c = '?' then implode(reverse([c . so_far])) else
if c = '!' then implode(reverse([c . so_far])) else
ssl_receive_message(conn,[c . so_far])
}
}.
define One
ssl_send_message
(
String me,
SSL_Connection conn,
String message,
Int32 i
) =
if write(conn,message) is
{
failure then print("\n"+me+": Cannot write into SSL connection."),
success(n) then
if n = length(message)
then unique
else print("\n"+me+": Was not able to write the whole message into SSL connection.")
}.
define One
ssl_server_handler
(
SSL_Connection conn
) =
with answer = "Very well, thank you !",
if local_SSL_address_and_port(conn) is (loc_a,loc_p) then
print("\nServer: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then
print("\nServer: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nServer: Waiting for a question.");
with question = ssl_receive_message(conn,[]),
print("\nServer: question is: '"+question+"'");
print("\nServer: Sending message: '"+answer+"'");
ssl_send_message("Server",conn,answer,0).
define One
ssl_client_handler
(
SSL_Connection conn,
Server server
) =
with question = "How do you do ?",
if local_SSL_address_and_port(conn) is (loc_a,loc_p) then
print("\nClient: Local address:port is "+ip_addr_to_string(loc_a)+":"+loc_p);
if remote_SSL_address_and_port(conn) is (rem_a,rem_p) then
print("\nClient: Remote address:port is "+ip_addr_to_string(rem_a)+":"+rem_p);
print("\nClient: Sending message: '"+question+"'");
ssl_send_message("Client",conn,question,0);
print("\nClient: Waiting for the answer.");
with answer = ssl_receive_message(conn,[]),
print("\nClient: Answer is: '"+answer+"'").
define Server
force_start_ssl_server
(
Int32 address,
Int32 port,
String server_name,
Server -> ((SSL_Connection) -> One) handler,
One -> One notify,
Int32 retry
) =
if start_ssl_server(address,port,server_name,handler,notify) is
{
cannot_create_the_socket then print("\nCannot create the listening socket."); alert,
cannot_bind_to_port then sleep(1000);
print("\rRetry number "+(retry+1)+" ");
force_start_ssl_server(address,port,server_name,handler,notify,retry+1),
cannot_listen_on_port then print("\nCannot listen on port "+port); alert,
ok(server) then server
}.
define (Maybe(X509)) -> Bool
policy
(
String server_name,
) =
(Maybe(X509) mb_cert) |->
if mb_cert is
{
failure then print("\nClient: No SSL certificate received from server."); true,
success(cert) then
print("\nClient Invalid server certificate:\n");
print(to_string(cert));
print("\n");
true
}.
public define TestItem
ssl_server_test
=
with server_name = "Shadoko",
ask_test_item("SSL client/server test",
(One u) |->
with port = (Int32)5432,
local_host = ip_address((127,0,0,1)),
print("\nStarting an SSL server on port number "+port+"\n");
with server = force_start_ssl_server(0,port,server_name,
(Server s) |-> ssl_server_handler,(One u) |-> u,0),
print("\nSSL server started.");
print("\nClient: Opening an SSL connection to the SSL server.");
if open_SSL_connection(server_name,local_host,port,policy(server_name)) is
{
error(msg) then print("\nClient: Cannot connect to SSL server."),
ok(conn) then
ssl_client_handler(conn,server);
print("\nShutting the server down.");
shutdown(server)
}
).