Browse Source

Application

master
aeris 2 months ago
parent
commit
ef008c7a08
12 changed files with 102 additions and 19 deletions
  1. +1
    -0
      lib/cryptcheck/engine.rb
  2. +24
    -0
      lib/cryptcheck/engine/buildable.rb
  3. +4
    -9
      lib/cryptcheck/engine/tls.rb
  4. +1
    -1
      lib/cryptcheck/engine/tls/alert.rb
  5. +22
    -0
      lib/cryptcheck/engine/tls/application.rb
  6. +1
    -1
      lib/cryptcheck/engine/tls/change_cipher_spec.rb
  7. +40
    -0
      lib/cryptcheck/engine/tls/context.rb
  8. +1
    -1
      lib/cryptcheck/engine/tls/handshake.rb
  9. +1
    -0
      lib/cryptcheck/engine/tls/record_header.rb
  10. +3
    -3
      spec/cryptcheck/engine/tls/alert_spec.rb
  11. +2
    -2
      spec/cryptcheck/engine/tls/change_cipher_spec_spec.rb
  12. +2
    -2
      spec/cryptcheck/engine/tls/handshake_spec.rb

+ 1
- 0
lib/cryptcheck/engine.rb View File

@@ -8,6 +8,7 @@ module Cryptcheck::Engine
Dir[fixtures].each { |f| load f }

autoload :Builder, 'cryptcheck/engine/builder'
autoload :Buildable, 'cryptcheck/engine/buildable'
autoload :Error, 'cryptcheck/engine/error'
autoload :DoubleHash, 'cryptcheck/engine/double_hash'
autoload :IdClasses, 'cryptcheck/engine/id_classes'


+ 24
- 0
lib/cryptcheck/engine/buildable.rb View File

@@ -0,0 +1,24 @@
module Cryptcheck::Engine
module Buildable
def self.included(klass)
klass.extend ClassMethod
end

private

module ClassMethod
def buildable(&block)
@@builder = Class.new do
include Builder
end
@@builder.instance_eval &block if block_given?
end

def build(&block)
builder = @@builder.new
builder.instance_eval &block if block_given?
builder.resolve
end
end
end
end

+ 4
- 9
lib/cryptcheck/engine/tls.rb View File

@@ -582,7 +582,8 @@ module Cryptcheck::Engine
raise ProtocolError, 'Unknown curve type %s' % type unless id
io.write_uint8 id
end

autoload :Context, 'cryptcheck/engine/tls/context'
autoload :RecordHeader, 'cryptcheck/engine/tls/record_header'
autoload :Handshake, 'cryptcheck/engine/tls/handshake'
autoload :ChangeCipherSpec, 'cryptcheck/engine/tls/change_cipher_spec'
@@ -591,15 +592,9 @@ module Cryptcheck::Engine
autoload :Application, 'cryptcheck/engine/tls/application'

def self.read(context, io)
header = RecordHeader.read context, io
header = RecordHeader.read context, io
record_type = header.type
record =
case (record_type)
when Application
record_type.read context, io, header.length
else
record_type.read context, io
end
record = record_type.read context, io, header.length
raise AlertError, record if record.is_a? Alert
[header, record]
end


+ 1
- 1
lib/cryptcheck/engine/tls/alert.rb View File

@@ -42,7 +42,7 @@ module Cryptcheck::Engine
@description = description
end

def self.read(_, io)
def self.read(_, io, _)
tmp = io.read_uint8
level = LEVELS[tmp]
raise ProtocolError, 'Unknown alert level 0x%02X' % tmp unless level


+ 22
- 0
lib/cryptcheck/engine/tls/application.rb View File

@@ -0,0 +1,22 @@
module Cryptcheck::Engine
module Tls
class Application
ID = 0x17

attr_reader :data

def initialize(data)
@data = data
end

def self.read(_, io, length)
data = io.read length
self.new data
end

def write(_, io)
io.write self.data
end
end
end
end

+ 1
- 1
lib/cryptcheck/engine/tls/change_cipher_spec.rb View File

@@ -4,7 +4,7 @@ module Cryptcheck::Engine
ID = 0x14
PAYLOAD = 0x01

def self.read(_, io)
def self.read(_, io, _)
payload = io.read_uint8
raise ProtocolError, 'Expect change cipher spec payload to be 0x%02X, got 0x%02X' % [PAYLOAD, payload] unless payload == PAYLOAD
self.new


+ 40
- 0
lib/cryptcheck/engine/tls/context.rb View File

@@ -0,0 +1,40 @@
module Cryptcheck::Engine
module Tls
class Context
include Buildable
buildable do
attributes :ip, :port, :hostname, :version
lists :compression, :cipher
build do
client = Client.new @ip, @port, @hostname, @version, @compressions, @cipher
Context.new client
end
end

attr_reader :server, :client

private

def initialize(client)
@client = client
@server = Server.new
end

class Client
attr_accessor :random

def initialize(ip, port, hostname, version, compressions, ciphers)
@ip = ip
@port = port
@hostname = hostname
@version = version
@compressions = compressions
@ciphers = ciphers
end
end

class Server
end
end
end
end

+ 1
- 1
lib/cryptcheck/engine/tls/handshake.rb View File

@@ -27,7 +27,7 @@ module Cryptcheck::Engine
ClientKeyExchange, # 0x10
).freeze

def self.read(context, io)
def self.read(context, io, _)
tmp = io.read_uint8
type = TYPES[tmp]
raise ProtocolError, 'Unknown handshake type 0x%02X' % tmp unless type


+ 1
- 0
lib/cryptcheck/engine/tls/record_header.rb View File

@@ -7,6 +7,7 @@ module Cryptcheck::Engine
Handshake, # 0x16
ChangeCipherSpec, # 0x14
Alert, # 0x15
Application, # 0x17
).freeze

def self.read(_, io)


+ 3
- 3
spec/cryptcheck/engine/tls/alert_spec.rb View File

@@ -6,7 +6,7 @@ module Cryptcheck::Engine
describe '::read' do
it 'must read record' do
io.init '01 0A'
alert = klass.read nil, io
alert = klass.read nil, io, nil
expect(io).to be_read 2
expect(alert).to be_a Alert
expect(alert.level).to eq :warning
@@ -15,12 +15,12 @@ module Cryptcheck::Engine

it 'must reject unknown level' do
io.init 'FF 0A'
expect { klass.read nil, io }.to raise_error ProtocolError, 'Unknown alert level 0xFF'
expect { klass.read nil, io, nil }.to raise_error ProtocolError, 'Unknown alert level 0xFF'
end

it 'must reject unknown description' do
io.init '01 FF'
expect { klass.read nil, io }.to raise_error ProtocolError, 'Unknown alert description 0xFF'
expect { klass.read nil, io, nil }.to raise_error ProtocolError, 'Unknown alert description 0xFF'
end
end



+ 2
- 2
spec/cryptcheck/engine/tls/change_cipher_spec_spec.rb View File

@@ -6,14 +6,14 @@ module Cryptcheck::Engine
describe '::read' do
it 'must read record' do
io.init '01'
change_cipher_spec = klass.read nil, io
change_cipher_spec = klass.read nil, io, nil
expect(io).to be_read 1
expect(change_cipher_spec).to be_a ChangeCipherSpec
end

it 'must reject unexpected payload' do
io.init '02'
expect { klass.read nil, io }.to raise_error ProtocolError, 'Expect change cipher spec payload to be 0x01, got 0x02'
expect { klass.read nil, io, nil }.to raise_error ProtocolError, 'Expect change cipher spec payload to be 0x01, got 0x02'
end
end



+ 2
- 2
spec/cryptcheck/engine/tls/handshake_spec.rb View File

@@ -6,7 +6,7 @@ module Cryptcheck::Engine
describe '::read' do
it 'must read record' do
io.init '00 000000'
handshake = klass.read nil, io
handshake = klass.read nil, io, nil
expect(io).to be_read 4
expect(handshake).to be_a Handshake
expect(handshake.record).to be_a klass::HelloRequest
@@ -14,7 +14,7 @@ module Cryptcheck::Engine

it 'must reject unknown record' do
io.init 'FF 000000'
expect { klass.read nil, io }.to raise_error ProtocolError, 'Unknown handshake type 0xFF'
expect { klass.read nil, io, nil }.to raise_error ProtocolError, 'Unknown handshake type 0xFF'
end
end



Loading…
Cancel
Save