-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.m
206 lines (158 loc) · 4.93 KB
/
client_test.m
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#import <ObjFW/ObjFW.h>
#import <WinBacktrace.h>
#import "MBEDSSLSocket.h"
#import "MBEDSSLConfig.h"
#import "MBEDCRL.h"
#import "MBEDX509Certificate.h"
#import "MBEDPKey.h"
#import "MBEDSSL.h"
@interface Test: OFObject<OFApplicationDelegate>
{
}
- (void)applicationDidFinishLaunching;
@end
OF_APPLICATION_DELEGATE(Test)
@implementation Test
- (void)applicationDidFinishLaunching
{
WinBacktrace* plugin = [OFPlugin pluginFromFile:@"WinBacktrace"];
of_log(@"Verification exception:\n\n");
bool connected = true;
MBEDSSLSocket* socket = [MBEDSSLSocket socket];
//socket.sslVersion = OBJMBED_SSLVERSION_TLSv1_2;
//socket.certificateProfile = kNextDefaultProfile;
socket.CA = [MBEDX509Certificate certificateWithFile:@"./GIAG2.crt"];
of_log(@"%@", socket.CA);
//socket.sslVersion = OBJMBED_SSLVERSION_TLSv1_2;
@try {
[socket connectToHost:@"173.194.222.139" port:443]; //exception expected
}@catch (id e) {
of_log(@"Expected exception - %@", e);
connected = false;
}
if (connected) {
[socket writeLine:@"GET / HTTP/1.0\r\n"];
while (!socket.isAtEndOfStream) {
OFString* l = [socket readLine];
of_log(@"%@", l);
}
[socket close];
}
connected = true;
socket.certificateVerificationEnabled = false;
of_log(@"Internal verification:\n\n");
@try {
[socket connectToHost:@"173.194.222.139" port:443]; //exception not expected
}@catch(id e) {
of_log(@"Expected exception - %@", e);
connected = false;
}
if (connected) {
of_log(@"Key: %@", socket.peerCertificate);
of_log(@"Key: %@", socket.peerCertificate.publicKey);
of_log(@"DER: %@", socket.peerCertificate.publicKey.DER);
of_log(@"PEM: \n%@", socket.peerCertificate.publicKey.PEM);
of_log(@"Next %@", [socket.peerCertificate next]);
[socket writeLine:@"GET / HTTP/1.0\r\n"];
while (!socket.isAtEndOfStream) {
OFString* l = [socket readLine];
of_log(@"%@", l);
}
[socket close];
}
connected = true;
socket.certificateVerificationEnabled = true;
of_log(@"Verification passed:\n\n");
@try {
[socket connectToHost:@"google.com" port:443]; //exception not expected
}@catch(id e) {
of_log(@"Not expected exception - %@", e);
[e printDebugBacktrace];
connected = false;
}
if (connected) {
of_log(@"Key: %@", socket.peerCertificate.publicKey);
of_log(@"DER: %@", socket.peerCertificate.publicKey.DER);
of_log(@"PEM: \n%@", socket.peerCertificate.publicKey.PEM);
of_log(@"Next %@", [socket.peerCertificate next]);
[socket writeLine:@"GET / HTTP/1.0\r\n"];
while (!socket.isAtEndOfStream) {
OFString* l = [socket readLine];
of_log(@"%@", l);
}
[socket close];
}
connected = true;
socket.certificateVerificationEnabled = false;
of_log(@"Internal verification passed:\n\n");
@try {
[socket connectToHost:@"google.com" port:443]; //exception not expected
}@catch(id e) {
of_log(@"Not expected exception - %@", e);
[e printDebugBacktrace];
connected = false;
}
if (connected) {
of_log(@"Key: %@", socket.peerCertificate.publicKey);
of_log(@"DER: %@", socket.peerCertificate.publicKey.DER);
of_log(@"PEM: \n%@", socket.peerCertificate.publicKey.PEM);
of_log(@"Next %@", [socket.peerCertificate next]);
[socket writeLine:@"GET / HTTP/1.0\r\n"];
while (!socket.isAtEndOfStream) {
OFString* l = [socket readLine];
of_log(@"%@", l);
}
[socket close];
}
of_log(@"SSL less connection:\n\n");
OFTCPSocket* sk = [OFTCPSocket socket];
of_log(@"Not SSL test");
[sk connectToHost:@"google.com" port:443];
[sk writeLine:@"GET / HTTP/1.0\r\n"];
while (!sk.isAtEndOfStream) {
OFString* l = [sk readLine];
of_log(@"%@", l);
}
[sk close];
of_log(@"Start TLS with connected socket:\n\n");
[sk connectToHost:@"google.com" port:443];
MBEDSSLSocket* sks = [[[MBEDSSLSocket alloc] initWithSocket:sk] autorelease];
[sk close]; //Closing original socket to check socket duplication
sks.certificateAuthorityFile = @"./GIAG2.crt";
[sks startTLSWithExpectedHost:@"google.com"];
[sks writeLine:@"GET / HTTP/1.0\r\n"];
while (!sks.isAtEndOfStream) {
OFString* l = [sks readLine];
of_log(@"%@", l);
}
[sks close];
of_log(@"Async connect:\n\n");
MBEDSSLSocket* con = [MBEDSSLSocket socket];
con.certificateAuthorityFile = @"./GIAG2.crt";
con.sslVersion = OBJMBED_SSLVERSION_TLSv1;
__block bool async_end = false;
[con asyncConnectToHost:@"google.com" port:443 block:^(OFTCPSocket *socket, OFException *_Nullable exception){
if (exception != nil) {
of_log(@"Async connect exception: %@", exception);
//[exception printDebugBacktrace];
return;
}
of_log(@"Async connection");
MBEDSSLSocket* sock = (MBEDSSLSocket*)socket;
[sock writeLine:@"GET / HTTP/1.0\r\n"];
while (!sock.isAtEndOfStream) {
OFString* l = [sock readLine];
of_log(@"%@", l);
}
[sock close];
async_end = true;
of_log(@"End async");
}];
[OFTimer scheduledTimerWithTimeInterval:0.5 repeats:true block:^(OFTimer *timer){
of_log(@"check");
if (async_end)
[OFApplication terminate];
return;
}];
}
@end