2020-08-22 01:50:38 +03:00
|
|
|
module openssl
|
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
import io
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2020-08-22 01:50:38 +03:00
|
|
|
import time
|
2022-09-22 16:50:34 +03:00
|
|
|
import os
|
2020-08-22 01:50:38 +03:00
|
|
|
|
2021-07-03 02:56:00 +03:00
|
|
|
// SSLConn is the current connection
|
|
|
|
pub struct SSLConn {
|
2022-09-22 16:50:34 +03:00
|
|
|
config SSLConnectConfig
|
2021-07-03 02:56:00 +03:00
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
sslctx &C.SSL_CTX = unsafe { nil }
|
|
|
|
ssl &C.SSL = unsafe { nil }
|
2021-07-03 02:56:00 +03:00
|
|
|
handle int
|
|
|
|
duration time.Duration
|
2022-09-22 16:50:34 +03:00
|
|
|
|
|
|
|
owns_socket bool
|
|
|
|
}
|
|
|
|
|
|
|
|
[params]
|
|
|
|
pub struct SSLConnectConfig {
|
|
|
|
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
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// new_ssl_conn instance an new SSLCon struct
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn new_ssl_conn(config SSLConnectConfig) !&SSLConn {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln(@METHOD)
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
mut conn := &SSLConn{
|
|
|
|
config: config
|
2021-07-03 02:56:00 +03:00
|
|
|
sslctx: 0
|
|
|
|
ssl: 0
|
|
|
|
handle: 0
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
conn.init() or { return err }
|
|
|
|
return conn
|
2021-07-03 02:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select operation
|
|
|
|
enum Select {
|
|
|
|
read
|
|
|
|
write
|
|
|
|
except
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:33:10 +03:00
|
|
|
// shutdown closes the ssl connection and does cleanup
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) shutdown() ! {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln(@METHOD)
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
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-10-16 20:59:04 +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-10-16 20:59:04 +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)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('unexepedted ssl error ${err_res}')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
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)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Could not connect using SSL. (${err_res}),err')
|
2020-08-22 01:50:38 +03:00
|
|
|
} 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)
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
if s.owns_socket {
|
2023-01-25 13:32:05 +03:00
|
|
|
net.shutdown(s.handle)
|
|
|
|
net.close(s.handle)!
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut s SSLConn) init() ! {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln(@METHOD)
|
|
|
|
}
|
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
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
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)
|
|
|
|
}
|
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
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
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 != '' {
|
2022-10-16 09:28:57 +03:00
|
|
|
os.write_file(verify, s.config.verify)!
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
if s.config.cert != '' {
|
2022-10-16 09:28:57 +03:00
|
|
|
os.write_file(cert, s.config.cert)!
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
if s.config.cert_key != '' {
|
2022-10-16 09:28:57 +03:00
|
|
|
os.write_file(cert_key, s.config.cert_key)!
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('http: openssl: SSL_CTX_use_certificate_file failed, res: ${res}')
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('http: openssl: SSL_CTX_use_PrivateKey_file failed, res: ${res}')
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
|
|
|
|
res = C.SSL_set_cipher_list(voidptr(s.ssl), preferred_ciphers.str)
|
|
|
|
if s.config.validate && res != 1 {
|
|
|
|
println('net.openssl: set cipher failed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect to server using OpenSSL
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ! {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} hostname: ${hostname}')
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
s.handle = tcp_conn.sock.handle
|
|
|
|
s.duration = tcp_conn.read_timeout()
|
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.")
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
|
|
|
|
s.complete_connect() or { return err }
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial opens an ssl connection on hostname:port
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) dial(hostname string, port int) ! {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} hostname: ${hostname} | port: ${port}')
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
s.owns_socket = true
|
2022-11-15 16:53:13 +03:00
|
|
|
mut tcp_conn := net.dial_tcp('${hostname}:${port}') or { return err }
|
2022-09-22 16:50:34 +03:00
|
|
|
$if macos {
|
|
|
|
tcp_conn.set_blocking(true) or { return err }
|
|
|
|
}
|
|
|
|
s.connect(mut tcp_conn, hostname) or { return err }
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut s SSLConn) complete_connect() ! {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln(@METHOD)
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
for {
|
2022-09-22 16:50:34 +03:00
|
|
|
mut res := C.SSL_connect(voidptr(s.ssl))
|
2020-08-22 01:50:38 +03:00
|
|
|
if res != 1 {
|
2022-10-16 09:28:57 +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-10-16 09:28:57 +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-10-16 09:28:57 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)!
|
2020-08-22 01:50:38 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Could not connect using SSL. (${err_res}),err')
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
|
|
|
|
if s.config.validate {
|
|
|
|
for {
|
|
|
|
mut res := C.SSL_do_handshake(voidptr(s.ssl))
|
|
|
|
if res != 1 {
|
2022-10-16 09:28:57 +03:00
|
|
|
err_res := ssl_error(res, s.ssl)!
|
2022-09-22 16:50:34 +03:00
|
|
|
if err_res == .ssl_error_want_read {
|
|
|
|
for {
|
2022-10-16 09:28:57 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)!
|
2022-09-22 16:50:34 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err_res == .ssl_error_want_write {
|
|
|
|
for {
|
2022-10-16 09:28:57 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)!
|
2022-09-22 16:50:34 +03:00
|
|
|
if ready {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Could not validate SSL certificate. (${err_res}),err')
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +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
|
2023-01-15 16:49:31 +03:00
|
|
|
$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) }}`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
for {
|
2021-02-21 18:03:25 +03:00
|
|
|
res = C.SSL_read(voidptr(s.ssl), buf_ptr, len)
|
2022-09-22 16:50:34 +03:00
|
|
|
if res > 0 {
|
2022-05-16 08:44:34 +03:00
|
|
|
return res
|
2022-09-22 16:50:34 +03:00
|
|
|
} else if res == 0 {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: io.Eof')
|
|
|
|
}
|
2022-10-28 19:08:30 +03:00
|
|
|
return io.Eof{}
|
2022-05-16 08:44:34 +03:00
|
|
|
} else {
|
2022-10-16 09:28:57 +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-10-16 09:28:57 +03:00
|
|
|
ready := @select(s.handle, .read, s.duration)!
|
2022-05-16 08:44:34 +03:00
|
|
|
if !ready {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: net.err_timed_out .ssl_error_want_read')
|
|
|
|
}
|
2022-05-16 08:44:34 +03:00
|
|
|
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-10-16 09:28:57 +03:00
|
|
|
ready := @select(s.handle, .write, s.duration)!
|
2022-05-16 08:44:34 +03:00
|
|
|
if !ready {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: net.err_timed_out .ssl_error_want_write')
|
|
|
|
}
|
2022-05-16 08:44:34 +03:00
|
|
|
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 {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: 0 .ssl_error_zero_return')
|
|
|
|
}
|
2022-05-16 08:44:34 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
else {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: could not read, err_res: ${err_res}')
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Could not read using SSL. (${err_res})')
|
2022-05-16 08:44:34 +03:00
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-08-08 02:33:25 +03:00
|
|
|
pub fn (mut s SSLConn) read(mut buffer []u8) !int {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} buffer.len: ${buffer.len}')
|
|
|
|
}
|
|
|
|
return s.socket_read_into_ptr(&u8(buffer.data), buffer.len)
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
// write_ptr writes `len` bytes from `bytes` to the ssl connection
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) write_ptr(bytes &u8, len int) !int {
|
2023-01-15 16:49:31 +03:00
|
|
|
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-=-=-=-')
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
unsafe {
|
2022-09-22 16:50:34 +03:00
|
|
|
mut ptr_base := bytes
|
|
|
|
for total_sent < len {
|
2020-08-22 01:50:38 +03:00
|
|
|
ptr := ptr_base + total_sent
|
2022-09-22 16:50:34 +03:00
|
|
|
remaining := 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-10-16 09:28:57 +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-10-16 09:28:57 +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-10-16 09:28:57 +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 {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: ssl write on closed connection .ssl_error_zero_return')
|
|
|
|
}
|
2020-08-22 01:50:38 +03:00
|
|
|
return error('ssl write on closed connection') // Todo error_with_code close
|
|
|
|
}
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: could not write SSL, err_res: ${err_res}')
|
|
|
|
}
|
2022-11-15 16:53:13 +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
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 16:49:31 +03:00
|
|
|
return total_sent
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-22 16:50:34 +03:00
|
|
|
// write writes data from `bytes` to the ssl connection
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) write(bytes []u8) !int {
|
2023-01-15 16:49:31 +03:00
|
|
|
return s.write_ptr(&u8(bytes.data), bytes.len)!
|
2022-09-22 16:50:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write_string writes a string to the ssl connection
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut s SSLConn) write_string(str string) !int {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} str: ${str}')
|
|
|
|
}
|
2022-09-22 16:50:34 +03:00
|
|
|
return s.write_ptr(str.str, str.len)
|
|
|
|
}
|
|
|
|
|
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
|
2022-10-16 09:28:57 +03:00
|
|
|
fn @select(handle int, test Select, timeout time.Duration) !bool {
|
2023-01-15 16:49:31 +03:00
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} handle: ${handle}, timeout: ${timeout}')
|
|
|
|
}
|
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 {
|
2023-04-13 08:38:21 +03:00
|
|
|
timeval_timeout = &C.timeval(unsafe { nil })
|
2021-02-21 18:03:25 +03:00
|
|
|
}
|
2020-11-18 20:21:49 +03:00
|
|
|
|
2021-02-21 18:03:25 +03:00
|
|
|
match test {
|
|
|
|
.read {
|
2022-10-16 09:28:57 +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-10-16 09:28:57 +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-10-16 09:28:57 +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
|
|
|
|
2023-01-15 16:49:31 +03:00
|
|
|
res := C.FD_ISSET(handle, &set)
|
|
|
|
$if trace_ssl ? {
|
|
|
|
eprintln('${@METHOD} ---> res: ${res}')
|
|
|
|
}
|
|
|
|
return res
|
2020-08-22 01:50:38 +03:00
|
|
|
}
|