-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-https-pfx.bas
More file actions
367 lines (297 loc) · 8.64 KB
/
test-https-pfx.bas
File metadata and controls
367 lines (297 loc) · 8.64 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
call OpenLBNetDLL
input "press ENTER to begin.";a
hServSock = CreateListenSocket("27016")
if IsSocketInvalid(hServSock) then
print "CreateListenSocket() failed. - ";GetError()
goto [doEnd]
end if
print "CreateListenSocket() successful."
[awaitLoop]
timer 0
print "Checking if connection is available..."
ret = IsReadAvailable(hServSock, 0)
if ret = 0 then
print "No connections yet. Waiting..."
timer 1, [awaitLoop]
wait
end if
if ret = -1 then
print "Error with IsReadAvailable(). - ";GetError()
goto [doSockEnd]
end if
Print "Attempting to accept connection..."
bufLen = 46 'enough to hold an IPv6 address plus null-terminating character
buf$ = space$(bufLen)
hConn = AcceptConnection(hServSock, buf$, bufLen)
if hConn = -1 then
print "AcceptConnection() failed. - ";GetError()
goto [doSockEnd]
end if
print "IP Address: ";trim$(buf$)
print "Creating TLS context..."
hTLS = CreateTLSContext()
print "Acquiring TLS credentials..."
fileName$ = "CA-test\localhost\localhost.pfx"
ret = BeginTLSServerWithPFX(hTLS, "localhost", fileName$, "")
print "BeginTLSServerWithPFX return - ";ret
if ret <> 0 then
print "BeginTLSServer() failed. ret - ";ret;" -- Error - ";GetError()
Print dechex$( (abs(ret) XOR hexdec("FFFFFFFF")) + 1)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
Print "Finishing connection..."
[handshakeLoop]
timer 0
ret = IsReadAvailable(hConn, 0)
if ret = 0 then
'No data available this time. Wait.
timer 1, [handshakeLoop]
wait
end if
if ret = -1 then
Print "IsReadAvailable() failed. - ";GetError()
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
goto [awaitLoop]
end if
a = SetTLSSocket(hTLS, hConn)
ret = PerformServerHandshake(hTLS, 1, "", 0, 5000)
if ret <> 0 then
print "PerformServerHandshake() failed. - ";ret; " - Error: ";dechex$(GetError())
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
[bufLoop]
timer 0
ret = IsTLSReadAvailable(hTLS, 0)
if ret = 0 then
'No data waiting. Stop and wait.
timer 1, [bufLoop]
wait
end if
bufLen = 512
buf$ = space$(bufLen)
num = DecryptReceive(hTLS, buf$, bufLen, 5000)
If num = -1 then
Print "Socket error occurred. - ";GetError()
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
goto [awaitLoop]
End if
crlf$ = chr$(13) + chr$(10)
lf$ = chr$(10)
cmdBuf$ = leftOver$ + left$(buf$, num)
[lineLoop]
lineComplete = instr(cmdBuf$, crlf$)
if lineComplete = 0 then
lineComplete = instr(cmdBuf$, lf$)
if lineComplete = 0 then
leftOver$ = cmdBuf$
goto [bufLoop]
end if
CR = 0
else
CR = 1
end if
cmd$ = trim$(left$(cmdBuf$, lineComplete - 1))
Print "< ";cmd$
if cmdBuf$ <> crlf$ and cmdBuf$ <> lf$ then
cmdBuf$ = right$(cmdBuf$, len(cmdBuf$) - lineComplete - CR)
goto [lineLoop]
end if
responseStatus$ = "HTTP/1.0 200 OK"
responseHeaders$ = "Server: LB Test" + crlf$
responseHeaders$ = responseHeaders$ + "Content-Language: en" + crlf$
responseHeaders$ = responseHeaders$ + "Content-Type: text/html; charset=utf8" + crlf$
responseHeaders$ = responseHeaders$ + "Connection: close" + crlf$
open "test.html" for input as #file
content$ = input$(#file, lof(#file))
close #file
lenContent = len(content$)
responseHeaders$ = responseHeaders$ + "Content-Length: " + str$(lenContent) + crlf$
response$ = responseStatus$ + crlf$ + responseHeaders$ + crlf$ + content$
lenResponse = len(response$)
ret = EncryptSend(hTLS, response$, lenResponse)
print
print response$
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
[doSockEnd]
a = CloseSocket(hServSock)
[doEnd]
call CloseLBNetDLL
Function randNum(min, max)
randNum = int(rnd(1) * max) + min
End Function
'====================
'==Helper Functions==
'====================
Sub OpenLBNetDLL
open "Debug\LBNet.dll" for DLL as #LBNet
a = InitLBNet()
End Sub
Sub CloseLBNetDLL
a = EndLBNet()
close #LBNet
End Sub
Function InitLBNet()
CallDLL #LBNet, "InitLBNet",_
InitLBNet as long
End Function
Function EndLBNet()
CallDLL #LBNet, "EndLBNet",_
EndLBNet as long
End Function
Function CreateTLSContext()
CallDLL #LBNet, "CreateTLSContext",_
CreateTLSContext as ulong
End Function
Function DestroyTLSContext(hTLS)
CallDLL #LBNet, "DestroyTLSContext",_
DestroyTLSContext as long
End Function
Function BeginTLSClientNoValidation(hTLS)
CallDLL #LBNet, "BeginTLSClientNoValidation",_
hTLS as ulong,_
BeginTLSClientNoValidation as long
End Function
Function BeginTLSClient(hTLS)
CallDLL #LBNet, "BeginTLSClient",_
hTLS as ulong,_
BeginTLSClient as long
End Function
Function IsSocketInvalid(sock)
CallDLL #LBNet, "IsSocketInvalid",_
sock as ulong,_
IsSocketInvalid as long
End Function
Function BeginTLSServer(hTLS, serverName$)
CallDLL #LBNet, "BeginTLSServer",_
hTLS as ulong,_
serverName$ as ptr,_
BeginTLSServer as long
End Function
Function BeginTLSServerWithPFX(hTLS, serverName$, certFileName$, certPass$)
CallDLL #LBNet, "BeginTLSServerWithPFX",_
hTLS as ulong,_
serverName$ as ptr,_
certFileName$ as ptr,_
certPass$ as ptr,_
BeginTLSServerWithPFX as long
End Function
Function SetTLSSocket(hTLS, sock)
CallDLL #LBNet, "SetTLSSocket",_
hTLS as ulong,_
sock as long,_
SetTLSSock as long
End Function
Function PerformClientHandshake(hTLS, serverName$, msTimeout)
CallDLL #LBNet, "PerformClientHandshake",_
hTLS as ulong,_
serverName$ as ptr,_
msTimeout as long,_
PerformClientHandshake as long
End Function
Function PerformServerHandshake(hTLS, doInitialRead, initBuf$, initBufSize, msTimeout)
CallDLL #LBNet, "PerformServerHandshake",_
hTLS as ulong,_
doInitialRead as long,_
initBuf$ as ptr,_
initBufSize as long,_
msTimeout as long,_
PerformServerHandshake as long
End Function
Function CreateListenSocket(pService$)
CallDLL #LBNet, "CreateListenSocket",_
pService$ as ptr,_
CreateListenSocket as ulong
End Function
Function AcceptConnection(ServerSocket, byref buf$, bufLen)
CallDLL #LBNet, "AcceptConnection",_
ServerSocket as ulong,_
buf$ as ptr,_
bufLen as long,_
AcceptConnection as ulong
End Function
Function IsReadAvailable(socket, msTimeout)
CallDLL #LBNet, "IsReadAvailable",_
socket as ulong,_
msTimeout as long,_
IsReadAvailable as long
End Function
Function IsTLSReadAvailable(hTLS, msTimeout)
CallDLL #LBNet, "IsTLSReadAvailable",_
hTLS as ulong,_
msTimeout as long,_
IsTLSReadAvailable as long
End Function
Function PingHost(host$, packetSize, byref status, byref msResponse, msTimeout)
struct a, b as long
struct c, d as long
a.b.struct = status
c.d.struct = msResponse
CallDLL #LBNet, "PingHost",_
host$ as ptr,_
packetSize as long,_
a as struct,_
c as struct,_
msTimeout as long,_
PingHost as long
status = a.b.struct
msResponse = c.d.struct
End Function
Function Connect(host$, srv$, msTimeout)
CallDLL #LBNet, "Connect",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
Connect as long
End Function
Function CloseSocket(sock)
CallDLL #LBNet, "CloseSocket",_
sock as long,_
CloseSocket as long
End Function
Function GetError()
CallDLL #LBNet, "GetError",_
GetError as long
if GetError < 0 then
GetError = (abs(GetError) XOR hexdec("FFFFFFFF")) + 1
end if
End Function
Function Send(sock, msg$, msgLen)
CallDLL #LBNet, "Send",_
sock as long,_
msg$ as ptr,_
msgLen as long,_
Send as long
End Function
Function EncryptSend(hTLS, msg$, msgLen)
CallDLL #LBNet, "EncryptSend",_
hTLS as ulong,_
msg$ as ptr,_
msgLen as long,_
EncryptSend as long
End Function
Function Receive(sock, byref buf$, bufLen)
CallDLL #LBNet, "Receive",_
sock as long,_
buf$ as ptr,_
bufLen as long,_
Receive as long
End Function
Function DecryptReceive(hTLS, byref buf$, bufLen, msTimeout)
CallDLL #LBNet, "DecryptReceive",_
hTLS as ulong,_
buf$ as ptr,_
bufLen as long,_
msTimeout as long,_
DecryptReceive as long
End Function
Function EndTLSClientSession(hTLS)
CallDLL #LBNet, "EndTLSClientSession",_
hTLS as ulong,_
EndTLSClientSession as long
End Function