2020-08-22 01:50:38 +03:00
|
|
|
module openssl
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2020-08-22 01:50:38 +03:00
|
|
|
import time
|
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// SSLConn is the current connection
|
|
|
|
pub struct SSLConn {
|
|
|
|
mut:
|
|
|
|
sslctx &C.SSL_CTX
|
|
|
|
ssl &C.SSL
|
|
|
|
handle int
|
|
|
|
duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// new_ssl_conn instance an new SSLCon struct
|
|
|
|
pub fn new_ssl_conn() &SSLConn {
|
|
|
|
return &SSLConn{
|
|
|
|
sslctx: 0
|
|
|
|
ssl: 0
|
|
|
|
handle: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select operation
|
|
|
|
enum Select {
|
|
|
|
read
|
|
|
|
write
|
|
|
|
except
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:33:10 +03:00
|
|
|
// shutdown closes the ssl connection and does cleanup
|
2020-08-22 01:50:38 +03:00
|
|
|
pub fn (mut s SSLConn) shutdown() ? {
|
|
|
|
if s.ssl != 0 {
|
|
|
|
mut res := 0
|
|
|
|
for {
|
2021-02-21 18:03:25 +03:00
|
|
|
res = C.SSL_shutdown(voidptr(s.ssl))
|
2020-08-22 01:50:38 +03:00
|
|
|
if res < 0 {
|
2021-07-03 02:56:00 +03:00
|
|
|
err_res := ssl_error(res, s.ssl) or {
|
2020-08-22 01:50:38 +03:00
|
|
|
break // We break to free rest of resources
|
|
|
|
}
|
|
|
|
if err_res == .ssl_error_want_read {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err_res == .ssl_error_want_write {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
2021-02-21 18:03:25 +03:00
|
|
|
unsafe { C.SSL_free(voidptr(s.ssl)) }
|
2020-11-21 16:45:45 +03:00
|
|
|
if s.sslctx != 0 {
|
|
|
|
C.SSL_CTX_free(s.sslctx)
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('unexepedted ssl error $err_res')
|
|
|
|
}
|
2020-11-21 16:45:45 +03:00
|
|
|
if s.ssl != 0 {
|
2021-02-21 18:03:25 +03:00
|
|
|
unsafe { C.SSL_free(voidptr(s.ssl)) }
|
2020-11-21 16:45:45 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
if s.sslctx != 0 {
|
|
|
|
C.SSL_CTX_free(s.sslctx)
|
|
|
|
}
|
|
|
|
return error('Could not connect using SSL. ($err_res),err')
|
|
|
|
} else if res == 0 {
|
|
|
|
continue
|
|
|
|
} else if res == 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-21 18:03:25 +03:00
|
|
|
C.SSL_free(voidptr(s.ssl))
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
if s.sslctx != 0 {
|
|
|
|
C.SSL_CTX_free(s.sslctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:33:10 +03:00
|
|
|
// connect to server using OpenSSL
|
2020-12-11 03:04:12 +03:00
|
|
|
pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? {
|
2020-08-22 01:50:38 +03:00
|
|
|
s.handle = tcp_conn.sock.handle
|
|
|
|
s.duration = tcp_conn.read_timeout()
|
2020-12-11 03:04:12 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
s.sslctx = unsafe { C.SSL_CTX_new(C.SSLv23_client_method()) }
|
2020-08-22 01:50:38 +03:00
|
|
|
if s.sslctx == 0 {
|
|
|
|
return error("Couldn't get ssl context")
|
|
|
|
}
|
2020-12-11 03:04:12 +03:00
|
|
|
|
|
|
|
// TODO: Fix option to enable/disable checks for valid
|
|
|
|
// certificates to allow both secure and self signed
|
2021-02-27 11:29:18 +03:00
|
|
|
// for now the checks are not done at all to comply
|
2020-12-11 03:04:12 +03:00
|
|
|
// to current autobahn tests
|
|
|
|
|
|
|
|
// C.SSL_CTX_set_verify_depth(s.sslctx, 4)
|
|
|
|
// flags := C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION
|
|
|
|
// C.SSL_CTX_set_options(s.sslctx, flags)
|
|
|
|
// mut res := C.SSL_CTX_load_verify_locations(s.sslctx, 'random-org-chain.pem', 0)
|
2021-02-21 18:03:25 +03:00
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
s.ssl = unsafe { &C.SSL(C.SSL_new(s.sslctx)) }
|
2020-08-22 01:50:38 +03:00
|
|
|
if s.ssl == 0 {
|
|
|
|
return error("Couldn't create OpenSSL instance.")
|
|
|
|
}
|
2020-12-11 03:04:12 +03:00
|
|
|
|
|
|
|
// preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
|
|
|
|
// mut res := C.SSL_set_cipher_list(s.ssl, preferred_ciphers.str)
|
|
|
|
// if res != 1 {
|
|
|
|
// println('http: openssl: cipher failed')
|
|
|
|
// }
|
2021-02-21 18:03:25 +03:00
|
|
|
|
|
|
|
mut res := C.SSL_set_tlsext_host_name(voidptr(s.ssl), voidptr(hostname.str))
|
2020-12-11 03:04:12 +03:00
|
|
|
if res != 1 {
|
|
|
|
return error('cannot set host name')
|
|
|
|
}
|
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
if C.SSL_set_fd(voidptr(s.ssl), tcp_conn.sock.handle) != 1 {
|
2020-08-22 01:50:38 +03:00
|
|
|
return error("Couldn't assign ssl to socket.")
|
|
|
|
}
|
|
|
|
for {
|
2021-02-21 18:03:25 +03:00
|
|
|
res = C.SSL_connect(voidptr(s.ssl))
|
2020-08-22 01:50:38 +03:00
|
|
|
if res != 1 {
|
2022-05-13 06:56:21 +03:00
|
|
|
err_res := ssl_error(res, s.ssl)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if err_res == .ssl_error_want_read {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err_res == .ssl_error_want_write {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return error('Could not connect using SSL. ($err_res),err')
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:25:45 +03:00
|
|
|
pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) ?int {
|
2020-08-22 01:50:38 +03:00
|
|
|
mut res := 0
|
|
|
|
for {
|
2021-02-21 18:03:25 +03:00
|
|
|
res = C.SSL_read(voidptr(s.ssl), buf_ptr, len)
|
2022-05-16 08:44:34 +03:00
|
|
|
if res >= 0 {
|
|
|
|
return res
|
|
|
|
} else {
|
2022-05-13 06:56:21 +03:00
|
|
|
err_res := ssl_error(res, s.ssl)?
|
2022-05-16 08:44:34 +03:00
|
|
|
match err_res {
|
|
|
|
.ssl_error_want_read {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)?
|
2022-05-16 08:44:34 +03:00
|
|
|
if !ready {
|
|
|
|
return net.err_timed_out
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 08:44:34 +03:00
|
|
|
.ssl_error_want_write {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)?
|
2022-05-16 08:44:34 +03:00
|
|
|
if !ready {
|
|
|
|
return net.err_timed_out
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 08:44:34 +03:00
|
|
|
.ssl_error_zero_return {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return error('Could not read using SSL. ($err_res)')
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut s SSLConn) read(mut buffer []u8) ?int {
|
2022-05-13 06:56:21 +03:00
|
|
|
res := s.socket_read_into_ptr(&u8(buffer.data), buffer.len)?
|
2020-08-22 01:50:38 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// write number of bytes to SSL connection
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (mut s SSLConn) write(bytes []u8) ?int {
|
2020-08-22 01:50:38 +03:00
|
|
|
unsafe {
|
2022-04-15 14:58:56 +03:00
|
|
|
mut ptr_base := &u8(bytes.data)
|
2020-08-22 01:50:38 +03:00
|
|
|
mut total_sent := 0
|
|
|
|
for total_sent < bytes.len {
|
|
|
|
ptr := ptr_base + total_sent
|
|
|
|
remaining := bytes.len - total_sent
|
2021-02-21 18:03:25 +03:00
|
|
|
mut sent := C.SSL_write(voidptr(s.ssl), ptr, remaining)
|
2020-08-22 01:50:38 +03:00
|
|
|
if sent <= 0 {
|
2022-05-13 06:56:21 +03:00
|
|
|
err_res := ssl_error(sent, s.ssl)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if err_res == .ssl_error_want_read {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err_res == .ssl_error_want_write {
|
|
|
|
for {
|
2022-05-13 06:56:21 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)?
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err_res == .ssl_error_zero_return {
|
|
|
|
return error('ssl write on closed connection') // Todo error_with_code close
|
|
|
|
}
|
2021-01-08 19:41:52 +03:00
|
|
|
return error_with_code('Could not write SSL. ($err_res),err', int(err_res))
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
total_sent += sent
|
|
|
|
}
|
2021-02-27 11:29:18 +03:00
|
|
|
return total_sent
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is basically a copy of Emily socket implementation of select.
|
|
|
|
This have to be consolidated into common net lib features
|
|
|
|
when merging this to V
|
|
|
|
*/
|
2021-07-03 02:56:00 +03:00
|
|
|
// [typedef]
|
|
|
|
// pub struct C.fd_set {
|
|
|
|
// }
|
2020-08-22 01:50:38 +03:00
|
|
|
|
|
|
|
// Select waits for an io operation (specified by parameter `test`) to be available
|
|
|
|
fn @select(handle int, test Select, timeout time.Duration) ?bool {
|
2021-02-21 18:03:25 +03:00
|
|
|
set := C.fd_set{}
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
C.FD_ZERO(&set)
|
|
|
|
C.FD_SET(handle, &set)
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
seconds := timeout.milliseconds() / 1000
|
|
|
|
microseconds := timeout - (seconds * time.second)
|
|
|
|
mut tt := C.timeval{
|
|
|
|
tv_sec: u64(seconds)
|
|
|
|
tv_usec: u64(microseconds)
|
|
|
|
}
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
mut timeval_timeout := &tt
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
// infinite timeout is signaled by passing null as the timeout to
|
|
|
|
// select
|
|
|
|
if timeout == net.infinite_timeout {
|
|
|
|
timeval_timeout = &C.timeval(0)
|
|
|
|
}
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
match test {
|
|
|
|
.read {
|
2022-05-13 06:56:21 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout))?
|
2021-02-21 18:03:25 +03:00
|
|
|
}
|
|
|
|
.write {
|
2022-05-13 06:56:21 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout))?
|
2021-02-21 18:03:25 +03:00
|
|
|
}
|
|
|
|
.except {
|
2022-05-13 06:56:21 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout))?
|
2021-02-21 18:03:25 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
return C.FD_ISSET(handle, &set)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|