OpenSSL.jl/src/types.jl

62 lines
1.6 KiB
Julia
Raw Normal View History

2020-10-24 22:35:19 +02:00
# Returned from C as Ptr
struct SSL_Method ptr end
struct BIO_Method ptr end
struct SSL_Context ptr end
2020-10-25 10:54:48 +01:00
@enum TLSMode begin
ClientMode
ServerMode
end
2020-10-25 10:55:15 +01:00
struct CAChain
cert
key
end
2020-10-23 19:34:36 +02:00
mutable struct SSLContext <: IO
data::Vector{UInt8}
2020-10-24 22:35:19 +02:00
ptr::Ptr{SSL_Context}
2020-10-25 10:54:48 +01:00
mode::TLSMode
2020-10-24 22:35:19 +02:00
"Construct the SSLContext object, initializing its relatvie SSL_Context
pointer.
2020-10-23 19:34:36 +02:00
2020-10-24 22:35:19 +02:00
Might throw ErrorException if the ccall fails"
2020-10-25 10:54:48 +01:00
function SSLContext(; mode::TLSMode = ClientMode)
2020-10-23 19:34:36 +02:00
ssl_context = new()
2020-10-25 10:54:48 +01:00
ssl_context.mode = mode
method = mode == ServerMode ? TLS_server_method() : TLS_client_method()
2020-10-24 22:35:19 +02:00
ssl_context.ptr = SSL_CTX_new(method)
if ssl_context.ptr == C_NULL
# TODO: check error stack and report the right exception
throw(ErrorException("Could not create SSL context"))
end
ssl_context.data = UInt8[]
2020-10-23 19:34:36 +02:00
ssl_context
end
end
mutable struct SSLClient{T}
rbio::Ptr{Cvoid}
wbio::Ptr{Cvoid}
context::SSLContext
ssl::Ptr{Cvoid}
io_on_read
sock::T
write_buf::Vector{UInt8}
encrypt_buf::Vector{UInt8}
function SSLClient(ctx::SSLContext, io::T) where T
client = new{T}()
client.context = ctx
client.ssl = SSL_new(ctx)
client.io_on_read = (data) -> append!(client.context.data, data)
2020-10-25 10:54:48 +01:00
(ctx.mode == ServerMode ? SSL_accept_state : SSL_connect_state)(client)
2020-10-23 19:34:36 +02:00
set_bio!(client, bio_new(), bio_new())
client.write_buf = UInt8[]
client.encrypt_buf = UInt8[]
client.sock = io
2020-10-24 22:35:19 +02:00
# finalizer(free, client)
2020-10-23 19:34:36 +02:00
client
end
end