/
ssl_connection.c.v
452 lines (407 loc) · 11.6 KB
/
ssl_connection.c.v
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
module openssl
import io
import net
import time
import os
// SSLConn is the current connection
pub struct SSLConn {
pub:
config SSLConnectConfig
pub mut:
sslctx &C.SSL_CTX = unsafe { nil }
ssl &C.SSL = unsafe { nil }
handle int
duration time.Duration
owns_socket bool
}
@[params]
pub struct SSLConnectConfig {
pub:
verify string // the path to a rootca.pem file, containing trusted CA certificate(s)
cert string // the path to a cert.pem file, containing client certificate(s) for the request
cert_key string // the path to a key.pem file, containing private keys for the client certificate(s)
validate bool // set this to true, if you want to stop requests, when their certificates are found to be invalid
in_memory_verification bool // if true, verify, cert, and cert_key are read from memory, not from a file
}
// new_ssl_conn instance an new SSLCon struct
pub fn new_ssl_conn(config SSLConnectConfig) !&SSLConn {
$if trace_ssl ? {
eprintln(@METHOD)
}
mut conn := &SSLConn{
config: config
sslctx: unsafe { nil }
ssl: unsafe { nil }
handle: 0
}
conn.init() or { return err }
return conn
}
// Select operation
enum Select {
read
write
except
}
// shutdown closes the ssl connection and does cleanup
pub fn (mut s SSLConn) shutdown() ! {
$if trace_ssl ? {
eprintln(@METHOD)
}
if s.ssl != 0 {
deadline := time.now().add(s.duration)
for {
mut res := C.SSL_shutdown(voidptr(s.ssl))
if res == 1 {
break
}
err_res := ssl_error(res, s.ssl) or {
break // We break to free rest of resources
}
if err_res == .ssl_error_want_read {
s.wait_for_read(deadline - time.now())!
continue
} else if err_res == .ssl_error_want_write {
s.wait_for_write(deadline - time.now())!
continue
}
if s.ssl != 0 {
unsafe { C.SSL_free(voidptr(s.ssl)) }
}
if s.sslctx != 0 {
C.SSL_CTX_free(s.sslctx)
}
return error('Could not connect using SSL. (${err_res}),err')
}
C.SSL_free(voidptr(s.ssl))
}
if s.sslctx != 0 {
C.SSL_CTX_free(s.sslctx)
}
if s.owns_socket {
net.shutdown(s.handle)
net.close(s.handle)!
}
}
fn (mut s SSLConn) init() ! {
$if trace_ssl ? {
eprintln(@METHOD)
}
s.sslctx = unsafe { C.SSL_CTX_new(C.SSLv23_client_method()) }
if s.sslctx == 0 {
return error("Couldn't get ssl context")
}
if s.config.validate {
C.SSL_CTX_set_verify_depth(s.sslctx, 4)
C.SSL_CTX_set_options(s.sslctx, C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION)
}
s.ssl = unsafe { &C.SSL(C.SSL_new(s.sslctx)) }
if s.ssl == 0 {
return error("Couldn't create OpenSSL instance.")
}
mut res := 0
if s.config.validate {
mut verify := s.config.verify
mut cert := s.config.cert
mut cert_key := s.config.cert_key
if s.config.in_memory_verification {
now := time.now().unix().str()
verify = os.temp_dir() + '/v_verify' + now
cert = os.temp_dir() + '/v_cert' + now
cert_key = os.temp_dir() + '/v_cert_key' + now
if s.config.verify != '' {
os.write_file(verify, s.config.verify)!
}
if s.config.cert != '' {
os.write_file(cert, s.config.cert)!
}
if s.config.cert_key != '' {
os.write_file(cert_key, s.config.cert_key)!
}
}
if s.config.verify != '' {
res = C.SSL_CTX_load_verify_locations(voidptr(s.sslctx), &char(verify.str),
0)
if s.config.validate && res != 1 {
return error('http: openssl: SSL_CTX_load_verify_locations failed')
}
}
if s.config.cert != '' {
res = C.SSL_CTX_use_certificate_file(voidptr(s.sslctx), &char(cert.str), C.SSL_FILETYPE_PEM)
if s.config.validate && res != 1 {
return error('http: openssl: SSL_CTX_use_certificate_file failed, res: ${res}')
}
}
if s.config.cert_key != '' {
res = C.SSL_CTX_use_PrivateKey_file(voidptr(s.sslctx), &char(cert_key.str),
C.SSL_FILETYPE_PEM)
if s.config.validate && res != 1 {
return error('http: openssl: SSL_CTX_use_PrivateKey_file failed, res: ${res}')
}
}
preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
res = C.SSL_set_cipher_list(voidptr(s.ssl), &char(preferred_ciphers.str))
if s.config.validate && res != 1 {
println('net.openssl: set cipher failed')
}
}
}
// connect to server using OpenSSL
pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ! {
$if trace_ssl ? {
eprintln('${@METHOD} hostname: ${hostname}')
}
s.handle = tcp_conn.sock.handle
s.duration = tcp_conn.read_timeout()
mut res := C.SSL_set_tlsext_host_name(voidptr(s.ssl), voidptr(hostname.str))
if res != 1 {
return error('cannot set host name')
}
if C.SSL_set_fd(voidptr(s.ssl), tcp_conn.sock.handle) != 1 {
return error("Couldn't assign ssl to socket.")
}
s.complete_connect() or { return err }
}
// dial opens an ssl connection on hostname:port
pub fn (mut s SSLConn) dial(hostname string, port int) ! {
$if trace_ssl ? {
eprintln('${@METHOD} hostname: ${hostname} | port: ${port}')
}
s.owns_socket = true
mut tcp_conn := net.dial_tcp('${hostname}:${port}') or { return err }
s.connect(mut tcp_conn, hostname) or { return err }
}
fn (mut s SSLConn) complete_connect() ! {
$if trace_ssl ? {
eprintln(@METHOD)
}
deadline := time.now().add(s.duration)
for {
mut res := C.SSL_connect(voidptr(s.ssl))
if res == 1 {
break
}
err_res := ssl_error(res, s.ssl)!
if err_res == .ssl_error_want_read {
s.wait_for_read(deadline - time.now())!
continue
}
if err_res == .ssl_error_want_write {
s.wait_for_write(deadline - time.now())!
continue
}
return error('Could not connect using SSL. (${err_res}),err')
}
if s.config.validate {
for {
mut res := C.SSL_do_handshake(voidptr(s.ssl))
if res == 1 {
break
}
err_res := ssl_error(res, s.ssl)!
if err_res == .ssl_error_want_read {
s.wait_for_read(deadline - time.now())!
continue
} else if err_res == .ssl_error_want_write {
s.wait_for_write(deadline - time.now())!
continue
}
return error('Could not validate SSL certificate. (${err_res}),err')
}
pcert := C.SSL_get_peer_certificate(voidptr(s.ssl))
defer {
if pcert != 0 {
C.X509_free(pcert)
}
}
res := C.SSL_get_verify_result(voidptr(s.ssl))
if res != C.X509_V_OK {
return error('SSL handshake failed')
}
}
}
// peer_addr retrieves the ip address and port number used by the peer
pub fn (s &SSLConn) peer_addr() !net.Addr {
return net.peer_addr_from_socket_handle(s.handle)
}
pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) !int {
mut res := 0
$if trace_ssl ? {
defer {
if len > 0 {
eprintln('${@METHOD} res: ${res}: buf_ptr: ${voidptr(buf_ptr):x}, len: ${len}, hex: ${unsafe { buf_ptr.vbytes(len).hex() }} data: `${unsafe { buf_ptr.vstring_with_len(len) }}`')
}
}
}
deadline := time.now().add(s.duration)
// s.wait_for_read(deadline - time.now())!
for {
res = C.SSL_read(voidptr(s.ssl), buf_ptr, len)
if res > 0 {
return res
} else if res == 0 {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: io.Eof')
}
return io.Eof{}
} else {
err_res := ssl_error(res, s.ssl)!
match err_res {
.ssl_error_want_read {
s.wait_for_read(deadline - time.now()) or {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: ${err} .ssl_error_want_read')
}
return err
}
}
.ssl_error_want_write {
s.wait_for_write(deadline - time.now()) or {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: ${err} .ssl_error_want_write')
}
return err
}
}
.ssl_error_zero_return {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: 0 .ssl_error_zero_return')
}
return 0
}
else {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: could not read, err_res: ${err_res}')
}
return error('Could not read using SSL. (${err_res})')
}
}
}
}
// Dead code, for the compiler to pass
return error('Unknown error')
}
pub fn (mut s SSLConn) read(mut buffer []u8) !int {
$if trace_ssl ? {
eprintln('${@METHOD} buffer.len: ${buffer.len}')
}
return s.socket_read_into_ptr(&u8(buffer.data), buffer.len)
}
// write_ptr writes `len` bytes from `bytes` to the ssl connection
pub fn (mut s SSLConn) write_ptr(bytes &u8, len int) !int {
mut total_sent := 0
$if trace_ssl ? {
defer {
eprintln('${@METHOD} total_sent: ${total_sent}, bytes: ${voidptr(bytes):x}, len: ${len}, hex: ${unsafe { bytes.vbytes(len).hex() }}, data:-=-=-=-\n${unsafe { bytes.vstring_with_len(len) }}\n-=-=-=-')
}
}
deadline := time.now().add(s.duration)
unsafe {
mut ptr_base := bytes
for total_sent < len {
ptr := ptr_base + total_sent
remaining := len - total_sent
mut sent := C.SSL_write(voidptr(s.ssl), ptr, remaining)
if sent <= 0 {
err_res := ssl_error(sent, s.ssl)!
if err_res == .ssl_error_want_read {
s.wait_for_read(deadline - time.now())!
continue
} else if err_res == .ssl_error_want_write {
s.wait_for_write(deadline - time.now())!
continue
} else if err_res == .ssl_error_zero_return {
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: ssl write on closed connection .ssl_error_zero_return')
}
return error('ssl write on closed connection') // TODO: error_with_code close
}
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: could not write SSL, err_res: ${err_res}')
}
return error_with_code('Could not write SSL. (${err_res}),err', int(err_res))
}
total_sent += sent
}
}
return total_sent
}
// write writes data from `bytes` to the ssl connection
pub fn (mut s SSLConn) write(bytes []u8) !int {
return s.write_ptr(&u8(bytes.data), bytes.len)!
}
// write_string writes a string to the ssl connection
pub fn (mut s SSLConn) write_string(str string) !int {
$if trace_ssl ? {
eprintln('${@METHOD} str: ${str}')
}
return s.write_ptr(str.str, str.len)
}
// Select waits for an io operation (specified by parameter `test`) to be available
fn @select(handle int, test Select, timeout time.Duration) !bool {
$if trace_ssl ? {
eprintln('${@METHOD} handle: ${handle}, timeout: ${timeout}')
}
set := C.fd_set{}
C.FD_ZERO(&set)
C.FD_SET(handle, &set)
deadline := time.now().add(timeout)
mut remaining_time := timeout.milliseconds()
for remaining_time > 0 {
seconds := remaining_time / 1000
microseconds := (remaining_time % 1000) * 1000
tt := C.timeval{
tv_sec: u64(seconds)
tv_usec: u64(microseconds)
}
timeval_timeout := if timeout < 0 {
&C.timeval(unsafe { nil })
} else {
&tt
}
mut res := -1
match test {
.read {
res = net.socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout))!
}
.write {
res = net.socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout))!
}
.except {
res = net.socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout))!
}
}
if res < 0 {
if C.errno == C.EINTR {
// errno is 4, Spurious wakeup from signal, keep waiting
remaining_time = (deadline - time.now()).milliseconds()
continue
}
return error_with_code('Select failed: ${res}', C.errno)
} else if res == 0 {
return net.err_timed_out
}
res = C.FD_ISSET(handle, &set)
$if trace_ssl ? {
eprintln('${@METHOD} ---> res: ${res}')
}
return res != 0
}
return net.err_timed_out
}
// wait_for wraps the common wait code
fn wait_for(handle int, what Select, timeout time.Duration) ! {
ready := @select(handle, what, timeout)!
if ready {
return
}
return net.err_timed_out
}
// wait_for_write waits for a write io operation to be available
fn (mut s SSLConn) wait_for_write(timeout time.Duration) ! {
return wait_for(s.handle, .write, timeout)
}
// wait_for_read waits for a read io operation to be available
fn (mut s SSLConn) wait_for_read(timeout time.Duration) ! {
return wait_for(s.handle, .read, timeout)
}