Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ TCPSocket.open("localhost", 5555) do |socket|
end
SSL::Socket.new_client(socket, ssl_ctx) do |client|
client.write("hello world".to_slice)
buf :: UInt8[512]
buf = uninitialized UInt8[512]
slice = buf.to_slice
len = client.read(slice)
puts "From server: #{String.new slice[0,len]}"
puts "From server: #{String.new slice[0, len]}"
end
end

5 changes: 2 additions & 3 deletions examples/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "../src/openssl"

include OpenSSL

tcp_server = TCPServer.new( 5555 )
tcp_server = TCPServer.new(5555)

ssl_ctx = SSL::Context.new(SSL::Method::SSLv23)

Expand All @@ -13,7 +13,7 @@ ssl_ctx.private_key_file = "my_key.pem"
loop do
client = tcp_server.accept
SSL::Socket.new_server(client, ssl_ctx) do |client|
buf :: UInt8[512]
buf = uninitialized UInt8[512]
loop do
len = client.read(buf.to_slice)
if len > 0
Expand All @@ -24,4 +24,3 @@ loop do
end
end
end

2 changes: 1 addition & 1 deletion spec/dsa_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe OpenSSL::PKey::DSA do

it "should be able to load DSA from pem" do
dsa = OpenSSL::PKey::DSA.generate(1024)
pem = MemoryIO.new
pem = IO::Memory.new
dsa.to_pem(pem)

pem.rewind
Expand Down
3 changes: 1 addition & 2 deletions spec/rsa_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe OpenSSL::PKey::RSA do

it "should be able to load RSA from pem" do
rsa = OpenSSL::PKey::RSA.generate(1024)
pem = MemoryIO.new
pem = IO::Memory.new
rsa.to_pem(pem)

pem.rewind
Expand All @@ -54,4 +54,3 @@ describe OpenSSL::PKey::RSA do
rsa.verify(digest, signature[0, 10], data).should be_false
end
end

4 changes: 2 additions & 2 deletions spec/x509_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EOC

describe Certificate do
it "should be able to load certificate from PEM" do
certificate = Certificate.from_pem(MemoryIO.new(CERT))
certificate = Certificate.from_pem(IO::Memory.new(CERT))
certificate.subject_name.name.should eq("CN=MyName")
certificate.fingerprint_hex.should eq("454ed8ec8b5a21f785de57edb5318381bccc98cd")
end
Expand All @@ -43,7 +43,7 @@ describe Generator do
certificate.subject_name.name.should eq("CN=MyName")
certificate.verify(pkey).should be_true

loaded_certificate = Certificate.from_pem(MemoryIO.new(certificate.to_pem))
loaded_certificate = Certificate.from_pem(IO::Memory.new(certificate.to_pem))
loaded_certificate.subject_name.name.should eq("CN=MyName")
loaded_certificate.fingerprint_hex.should eq(certificate.fingerprint_hex)
end
Expand Down
10 changes: 5 additions & 5 deletions src/bio/bio.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ struct OpenSSL::BIO
crystal_bio = LibCrypto::BioMethod.new
crystal_bio.name = "Crystal BIO".cstr

crystal_bio.bwrite = -> (bio : LibCrypto::BIO, data : UInt8*, len : Int32) do
crystal_bio.bwrite = ->(bio : LibCrypto::BIO, data : UInt8*, len : Int32) do
io = Box(IO).unbox(bio.value.ptr)
io.write Slice.new(data, len)
len
end

crystal_bio.bread = -> (bio : LibCrypto::BIO, buffer : UInt8*, len : Int32) do
crystal_bio.bread = ->(bio : LibCrypto::BIO, buffer : UInt8*, len : Int32) do
io = Box(IO).unbox(bio.value.ptr)
io.read(Slice.new(buffer, len)).to_i
end

crystal_bio.ctrl = -> (bio : LibCrypto::BIO, cmd : Int32, num : Int64, ptr : Void*) do
crystal_bio.ctrl = ->(bio : LibCrypto::BIO, cmd : Int32, num : Int64, ptr : Void*) do
io = Box(IO).unbox(bio.value.ptr)

case cmd
Expand All @@ -31,13 +31,13 @@ struct OpenSSL::BIO
end
end

crystal_bio.create = -> (bio : LibCrypto::BIO) do
crystal_bio.create = ->(bio : LibCrypto::BIO) do
bio.value.shutdown = 1
bio.value.init = 1
bio.value.num = -1
end

crystal_bio.destroy = -> (bio : LibCrypto::BIO) { bio.value.ptr = Pointer(Void).null; 1 }
crystal_bio.destroy = ->(bio : LibCrypto::BIO) { bio.value.ptr = Pointer(Void).null; 1 }

crystal_bio
end
Expand Down
75 changes: 38 additions & 37 deletions src/bio/mem_bio.cr
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
require "../openssl"

struct OpenSSL::MemBIO
include IO

class BIOError < OpenSSLError; end

def initialize(@bio : LibCrypto::BIO)
raise BIOError.new "Invalid handle" unless @bio
end

def initialize
initialize LibCrypto.bio_new(LibCrypto.bio_s_mem())
end

def read(data : Slice(UInt8))
LibCrypto.bio_read(self, data, data.size)
end

def write(data : Slice(UInt8))
LibCrypto.bio_write(self, data, data.size)
end

def reset
LibCrypto.bio_ctrl(self, LibCrypto::BIO_CTRL_RESET, 0_i64, nil)
end

def finalize
LibCrypto.bio_free_all(self)
end

def to_string
buf = MemoryIO.new
IO.copy(self, buf)
buf.to_s
end

def to_unsafe
@bio
module OpenSSL
class MemBIO
class BIOError < OpenSSLError
end

def initialize(@bio : LibCrypto::BIO)
raise BIOError.new "Invalid handle" unless @bio
end

def initialize
initialize LibCrypto.bio_new(LibCrypto.bio_s_mem)
end

def read(data : Slice(UInt8))
LibCrypto.bio_read(self, data, data.size)
end

def write(data : Slice(UInt8))
LibCrypto.bio_write(self, data, data.size)
end

def reset
LibCrypto.bio_ctrl(self, LibCrypto::BIO_CTRL_RESET, 0_i64, nil)
end

def finalize
LibCrypto.bio_free_all(self)
end

def to_string
buf = IO::Memory.new
IO.copy(self, buf)
buf.to_s
end

def to_unsafe
@bio
end
end
end
10 changes: 5 additions & 5 deletions src/digest/digest.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module OpenSSL

getter name

def initialize(@name, @ctx : LibCrypto::EVP_MD_CTX)
def initialize(@name : String, @ctx : LibCrypto::EVP_MD_CTX)
raise DigestError.new("Invalid EVP_MD_CTX") unless @ctx
end

Expand All @@ -35,7 +35,7 @@ module OpenSSL
unless md
raise "Unsupported digest algoritm: #{name}"
end
ctx = LibCrypto.evp_md_ctx_create()
ctx = LibCrypto.evp_md_ctx_new
unless ctx
raise OpenSSL::Digest::DigestError.new "Digest initialization failed."
end
Expand All @@ -50,13 +50,13 @@ module OpenSSL
end

def finalize
LibCrypto.evp_md_ctx_destroy(self)
LibCrypto.evp_md_ctx_free(self)
end

def clone
ctx = LibCrypto.evp_md_ctx_create()
ctx = LibCrypto.evp_md_ctx_new
if LibCrypto.evp_md_ctx_copy(ctx, @ctx) == 0
LibCrypto.evp_md_ctx_destroy(ctx)
LibCrypto.evp_md_ctx_free(ctx)
raise DigestError.new("Unable to clone digest")
end
Digest.new(@name, ctx)
Expand Down
2 changes: 1 addition & 1 deletion src/digest/hmac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OpenSSL::HMAC

def self.new(key, digest)
new.tap do |hmac|
LibCrypto.hmac_init_ex(hmac, key.to_unsafe as Pointer(Void), key.bytesize, digest.to_unsafe_md, nil)
LibCrypto.hmac_init_ex(hmac, key.to_unsafe.as(Pointer(Void)), key.bytesize, digest.to_unsafe_md, nil)
end
end

Expand Down
Loading