Skip to content

Commit bfd3373

Browse files
committed
on_bio_stream_read returns -1 if read fails, calls eof before calling bytesavailable
on_bio_stream_write return -1 if write fails (bss_sock.c) This enables server side mode, however the logic in the bio still does not match what OpenSSL is doing.
1 parent 57f035d commit bfd3373

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

src/ssl.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ function bio_set_flags(bio::BIO, flags)
2929
(BIO, Cint),
3030
bio, flags)
3131
end
32+
3233
bio_set_read_retry(bio::BIO) = bio_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY)
3334
bio_clear_flags(bio::BIO) = bio_set_flags(bio, 0x00)
3435

3536
function on_bio_stream_read(bio::BIO, out::Ptr{Cchar}, outlen::Cint)
3637
try
3738
bio_clear_flags(bio)
3839
io = bio_get_data(bio)::IO
40+
eof(io)
3941
n = bytesavailable(io)
4042
if n == 0
4143
bio_set_read_retry(bio)
@@ -45,7 +47,7 @@ function on_bio_stream_read(bio::BIO, out::Ptr{Cchar}, outlen::Cint)
4547
return Cint(min(n, outlen))
4648
catch e
4749
# we don't want to throw a Julia exception from a C callback
48-
return Cint(0)
50+
return Cint(-1)
4951
end
5052
end
5153

@@ -56,7 +58,7 @@ function on_bio_stream_write(bio::BIO, in::Ptr{Cchar}, inlen::Cint)::Cint
5658
return Cint(written)
5759
catch e
5860
# we don't want to throw a Julia exception from a C callback
59-
return Cint(0)
61+
return Cint(-1)
6062
end
6163
end
6264

test/http_helpers.jl

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Dates
22
using OpenSSL
33
using Sockets
4+
using Test
45

56
function test_server()
67
x509_certificate = X509Certificate()
@@ -22,54 +23,47 @@ function test_server()
2223
sign_certificate(x509_certificate, evp_pkey)
2324

2425
server_socket = listen(5000)
25-
try
26-
accepted_socket = accept(server_socket)
26+
accepted_socket = accept(server_socket)
2727

28-
# Create and configure server SSLContext.
29-
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSServerMethod())
30-
_ = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
28+
# Create and configure server SSLContext.
29+
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSServerMethod())
30+
_ = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
3131

32-
OpenSSL.ssl_set_ciphersuites(ssl_ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256")
33-
OpenSSL.ssl_use_certificate(ssl_ctx, x509_certificate)
34-
OpenSSL.ssl_use_private_key(ssl_ctx, evp_pkey)
32+
OpenSSL.ssl_set_ciphersuites(ssl_ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256")
33+
OpenSSL.ssl_use_certificate(ssl_ctx, x509_certificate)
34+
OpenSSL.ssl_use_private_key(ssl_ctx, evp_pkey)
3535

36-
ssl = SSLStream(ssl_ctx, accepted_socket)
36+
ssl = SSLStream(ssl_ctx, accepted_socket)
3737

38-
OpenSSL.accept(ssl)
38+
OpenSSL.accept(ssl)
3939

40-
@test !eof(ssl)
41-
request = readavailable(ssl)
42-
reply = "reply: $(String(request))"
40+
@test !eof(ssl)
41+
request = readavailable(ssl)
42+
reply = "reply: $(String(request))"
43+
44+
# eof(ssl) will block
4345

44-
# eof(ssl) will block
46+
# Verify the are no more bytes available in the stream.
47+
@test bytesavailable(ssl) == 0
4548

46-
# Verify the are no more bytes available in the stream.
47-
@test bytesavailable(ssl) == 0
49+
unsafe_write(ssl, pointer(reply), length(reply))
4850

49-
write(ssl, reply)
51+
close(ssl)
52+
finalize(ssl_ctx)
5053

51-
try
52-
close(ssl)
53-
catch
54-
end
55-
finalize(ssl_ctx)
56-
finally
57-
close(server_socket)
58-
end
5954
return nothing
6055
end
6156

6257
function test_client()
6358
tcp_stream = connect(5000)
6459

6560
ssl_ctx = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
66-
ssl_options = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
61+
_ = OpenSSL.ssl_set_options(ssl_ctx, OpenSSL.SSL_OP_NO_COMPRESSION)
6762

6863
# Create SSL stream.
6964
ssl = SSLStream(ssl_ctx, tcp_stream)
7065

71-
#TODO expose connect
72-
OpenSSL.connect(ssl)
66+
connect(ssl; require_ssl_verification = false)
7367

7468
# Verify the server certificate.
7569
x509_server_cert = OpenSSL.get_peer_certificate(ssl)
@@ -87,12 +81,8 @@ function test_client()
8781

8882
response_str = String(readavailable(ssl))
8983

90-
@test response_str == "reply: $request_str"
84+
@test response_str == "reply: $(request_str)"
9185

92-
try
93-
close(ssl)
94-
catch
95-
end
86+
close(ssl)
9687
finalize(ssl_ctx)
97-
return nothing
9888
end

0 commit comments

Comments
 (0)