Skip to content

Commit 9f9900a

Browse files
committed
feat: update to work with openssl 3.x
1 parent ba9ef5a commit 9f9900a

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
source 'https://rubygems.org'
22
gemspec
3-
4-
gem "rubysl", :platform => :rbx

lib/ruby-tls/ssl.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ module SSL
3434

3535
SSL_ST_OK = 0x03
3636
begin
37-
attach_function :SSL_library_init, [], :int
3837
attach_function :SSL_load_error_strings, [], :void
3938
attach_function :ERR_load_crypto_strings, [], :void
39+
attach_function :SSL_library_init, [], :int
4040

4141
attach_function :SSL_state, [:ssl], :int
4242
def self.SSL_is_init_finished(ssl)
@@ -76,7 +76,9 @@ def self.SSL_is_init_finished(ssl)
7676
attach_function :BIO_free, [:bio], :int
7777

7878
# GetPeerCert
79-
attach_function :SSL_get_peer_certificate, [:ssl], :x509
79+
attach_function :SSL_get1_peer_certificate, [:ssl], :x509
80+
81+
attach_function :SSL_CTX_set_security_level, [:ssl, :int], :void
8082

8183

8284
# PutPlaintext
@@ -207,6 +209,8 @@ def self.SSL_CTX_set_tlsext_servername_callback(ctx, callback)
207209
attach_function :SSL_CTX_use_PrivateKey_file, [:ssl_ctx, :string, :int], :int, :blocking => true
208210
attach_function :SSL_CTX_use_PrivateKey, [:ssl_ctx, :pointer], :int
209211
attach_function :ERR_print_errors_fp, [:pointer], :void # Pointer == File Handle
212+
attach_function :ERR_get_error, [], :long
213+
attach_function :ERR_reason_error_string, [:ulong], :string
210214
attach_function :SSL_CTX_use_certificate_chain_file, [:ssl_ctx, :string], :int, :blocking => true
211215
attach_function :SSL_CTX_use_certificate, [:ssl_ctx, :x509], :int
212216
attach_function :SSL_CTX_set_cipher_list, [:ssl_ctx, :string], :int
@@ -310,13 +314,18 @@ def self.SSL_CTX_set_tlsext_servername_callback(ctx, callback)
310314
@init_required ||= false
311315
unless @init_required
312316
if OPENSSL_V1_1
313-
self.OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, ::FFI::Pointer::NULL)
317+
# self.OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, ::FFI::Pointer::NULL)
314318
else
315-
self.SSL_load_error_strings
316-
self.SSL_library_init
317-
self.ERR_load_crypto_strings
319+
# self.SSL_load_error_strings
320+
# self.SSL_library_init
321+
# self.ERR_load_crypto_strings
318322
end
319323

324+
# self.SSL_load_error_strings
325+
# self.SSL_library_init
326+
# self.ERR_load_crypto_strings
327+
self.OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, ::FFI::Pointer::NULL)
328+
320329
# Setup multi-threaded support
321330
#SSL_LOCKS = []
322331
#num_locks = self.CRYPTO_num_locks
@@ -325,7 +334,6 @@ def self.SSL_CTX_set_tlsext_servername_callback(ctx, callback)
325334
#self.CRYPTO_set_locking_callback(LockingCB)
326335
#self.CRYPTO_set_id_callback(ThreadIdCB)
327336

328-
329337
bio = self.BIO_new_mem_buf(PrivateMaterials, PrivateMaterials.bytesize)
330338

331339
# Get the private key structure
@@ -384,11 +392,13 @@ def initialize(server, options = {})
384392

385393
if @is_server
386394
@ssl_ctx = SSL.SSL_CTX_new(SSL.TLS_server_method)
395+
SSL.SSL_CTX_set_security_level(@ssl_ctx, 0)
387396
set_private_key(options[:private_key] || SSL::DEFAULT_PRIVATE)
388397
set_certificate(options[:cert_chain] || SSL::DEFAULT_CERT)
389398
set_client_ca(options[:client_ca])
390399
else
391400
@ssl_ctx = SSL.SSL_CTX_new(SSL.TLS_client_method)
401+
SSL.SSL_CTX_set_security_level(@ssl_ctx, 0)
392402
end
393403

394404
SSL.SSL_CTX_set_options(@ssl_ctx, SSL::SSL_OP_ALL)
@@ -517,8 +527,10 @@ def set_certificate(cert)
517527
end
518528

519529
if err <= 0
530+
error_code = SSL.ERR_get_error
531+
reason = SSL.ERR_reason_error_string(error_code)
520532
cleanup
521-
raise 'invalid certificate or file not found'
533+
raise "invalid certificate or file not found: #{err} #{reason}"
522534
end
523535
end
524536

@@ -616,7 +628,7 @@ def remove_host(host_name)
616628

617629
def get_peer_cert
618630
return '' unless @ready
619-
SSL.SSL_get_peer_certificate(@ssl)
631+
SSL.SSL_get1_peer_certificate(@ssl)
620632
end
621633

622634
def negotiated_protocol
@@ -657,7 +669,7 @@ def encrypt(data)
657669
def decrypt(data)
658670
return unless @ready
659671

660-
put_cipher_text data
672+
result = put_cipher_text data
661673

662674
if not SSL.SSL_is_init_finished(@ssl)
663675
resp = @is_server ? SSL.SSL_accept(@ssl) : SSL.SSL_connect(@ssl)
@@ -754,7 +766,8 @@ def get_plain_text(buffer, ready)
754766
if size >= 0
755767
size
756768
else
757-
SSL.SSL_get_error(@ssl, size) == SSL_ERROR_WANT_READ ? 0 : -1
769+
error_code = SSL.SSL_get_error(@ssl, size)
770+
error_code == SSL_ERROR_WANT_READ ? 0 : -1
758771
end
759772
end
760773

@@ -795,7 +808,7 @@ def put_cipher_text(data)
795808
SSL_ERROR_WANT_WRITE = 3
796809
def put_plain_text(data)
797810
@write_queue.push(data) if data
798-
return 0 unless SSL.SSL_is_init_finished(@ssl)
811+
# return 0 unless SSL.SSL_is_init_finished(@ssl)
799812

800813
fatal = false
801814
did_work = false

lib/ruby-tls/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RubyTls
4-
VERSION = '2.4.0'
4+
VERSION = '3.0.0'
55
end

spec/comms_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ def handshake_cb(protocol)
101101
@server = Server1.new(@client, @server_data, @interleaved)
102102
@client.server = @server
103103

104-
105104
@client.ssl.start
106105
@client.ssl.cleanup
107106
@server.ssl.cleanup
108107

109-
110108
# Calls to encrypt should not cause crashes after cleanup
111109
@server.ssl.encrypt('server response')
112110
@client.ssl.encrypt('client request')
113111

112+
expect(@server.stop).to eq(nil)
113+
114+
expect(@interleaved).to eq(['server ready', 'client ready', 'client request', 'server response'])
114115
expect(@server_data).to eq(['ready', 'client request'])
115116
expect(@client_data).to eq(['ready', 'server response'])
116-
expect(@interleaved).to eq(['server ready', 'client ready', 'client request', 'server response'])
117117
end
118118
end
119119
end

0 commit comments

Comments
 (0)