Class: TTTLS13::Connection
- Inherits:
-
Object
- Object
- TTTLS13::Connection
- Includes:
- Logging
- Defined in:
- lib/tttls1.3/connection.rb
Overview
rubocop: disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#alert_wcipher ⇒ Object
Returns the value of attribute alert_wcipher.
-
#ap_rcipher ⇒ Object
Returns the value of attribute ap_rcipher.
-
#ap_wcipher ⇒ Object
Returns the value of attribute ap_wcipher.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #close ⇒ Object
- #eof? ⇒ Boolean
- #handle_received_alert(alert) ⇒ Object
-
#initialize(socket, side) ⇒ Connection
constructor
A new instance of Connection.
- #read(nst_process) ⇒ String
- #recv_message(receivable_ccs:, cipher:) ⇒ TTTLS13::Message::$Object, String
- #recv_record(cipher) ⇒ TTTLS13::Message::Record, Array of String
- #send_alert(symbol) ⇒ Object
- #send_application_data(message, cipher) ⇒ Object
- #send_ccs ⇒ Object
- #send_handshakes(type, messages, cipher) ⇒ Object
- #send_record(record) ⇒ Object
- #terminate(symbol) ⇒ Object
- #write(binary) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(socket, side) ⇒ Connection
Returns a new instance of Connection.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tttls1.3/connection.rb', line 16 def initialize(socket, side) @socket = socket @side = side @state = INITIAL @ap_wcipher = Cryptograph::Passer.new @ap_rcipher = Cryptograph::Passer.new @alert_wcipher = Cryptograph::Passer.new @message_queue = [] # Array of [TTTLS13::Message::$Object, String] @binary_buffer = '' # deposit Record.surplus_binary @send_record_size = Message::DEFAULT_RECORD_SIZE_LIMIT @recv_record_size = Message::DEFAULT_RECORD_SIZE_LIMIT end |
Instance Attribute Details
#alert_wcipher ⇒ Object
Returns the value of attribute alert_wcipher.
12 13 14 |
# File 'lib/tttls1.3/connection.rb', line 12 def alert_wcipher @alert_wcipher end |
#ap_rcipher ⇒ Object
Returns the value of attribute ap_rcipher.
12 13 14 |
# File 'lib/tttls1.3/connection.rb', line 12 def ap_rcipher @ap_rcipher end |
#ap_wcipher ⇒ Object
Returns the value of attribute ap_wcipher.
12 13 14 |
# File 'lib/tttls1.3/connection.rb', line 12 def ap_wcipher @ap_wcipher end |
#state ⇒ Object
Returns the value of attribute state.
12 13 14 |
# File 'lib/tttls1.3/connection.rb', line 12 def state @state end |
Instance Method Details
#close ⇒ Object
75 76 77 78 79 80 |
# File 'lib/tttls1.3/connection.rb', line 75 def close return if @state == EOF send_alert(:close_notify) @state = EOF end |
#handle_received_alert(alert) ⇒ Object
223 224 225 226 227 228 229 230 |
# File 'lib/tttls1.3/connection.rb', line 223 def handle_received_alert(alert) unless alert.description == Message::ALERT_DESCRIPTION[:close_notify] || alert.description == Message::ALERT_DESCRIPTION[:user_canceled] raise alert.to_error end @state = EOF end |
#read(nst_process) ⇒ String
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tttls1.3/connection.rb', line 34 def read(nst_process) # secure channel has not established yet raise Error::ConfigError \ unless (@side == :client && @state == ClientState::CONNECTED) || (@side == :server && @state == ServerState::CONNECTED) return '' if @state == EOF = nil loop do , = (receivable_ccs: false, cipher: @ap_rcipher) # At any time after the server has received the client Finished # message, it MAY send a NewSessionTicket message. break unless .is_a?(Message::NewSessionTicket) terminate(:unexpected_message) if @side == :server nst_process.call() end return '' if .nil? .fragment end |
#recv_message(receivable_ccs:, cipher:) ⇒ TTTLS13::Message::$Object, String
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tttls1.3/connection.rb', line 146 def (receivable_ccs:, cipher:) return @message_queue.shift unless @message_queue.empty? = nil orig_msgs = [] loop do record, orig_msgs = recv_record(cipher) case record.type when Message::ContentType::HANDSHAKE, Message::ContentType::APPLICATION_DATA = record. break unless .empty? when Message::ContentType::CCS terminate(:unexpected_message) unless receivable_ccs next when Message::ContentType::ALERT handle_received_alert(record..first) return nil else terminate(:unexpected_message) end end @message_queue += [1..].zip(orig_msgs[1..]) = .first orig_msg = orig_msgs.first if .is_a?(Message::Alert) handle_received_alert() return nil end [, orig_msg] end |
#recv_record(cipher) ⇒ TTTLS13::Message::Record, Array of String
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/tttls1.3/connection.rb', line 184 def recv_record(cipher) binary = @socket.read(5) record_len = Convert.bin2i(binary.slice(3, 2)) binary += @socket.read(record_len) begin buffer = @binary_buffer record, orig_msgs, surplus_binary = Message::Record.deserialize( binary, cipher, buffer, @recv_record_size ) @binary_buffer = surplus_binary rescue Error::ErrorAlerts => e terminate(e..to_sym) end # Received a protected ccs, peer MUST abort the handshake. if record.type == Message::ContentType::APPLICATION_DATA && record..any? { |m| m.is_a?(Message::ChangeCipherSpec) } terminate(:unexpected_message) end logger.info(Convert.obj2html(record)) [record, orig_msgs] end |
#send_alert(symbol) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tttls1.3/connection.rb', line 117 def send_alert(symbol) = Message::Alert.new( description: Message::ALERT_DESCRIPTION[symbol] ) type = Message::ContentType::ALERT type = Message::ContentType::APPLICATION_DATA \ if @alert_wcipher.is_a?(Cryptograph::Aead) alert_record = Message::Record.new( type:, legacy_record_version: Message::ProtocolVersion::TLS_1_2, messages: [], cipher: @alert_wcipher ) send_record(alert_record) end |
#send_application_data(message, cipher) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/tttls1.3/connection.rb', line 106 def send_application_data(, cipher) ap_record = Message::Record.new( type: Message::ContentType::APPLICATION_DATA, legacy_record_version: Message::ProtocolVersion::TLS_1_2, messages: [], cipher: ) send_record(ap_record) end |
#send_ccs ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/tttls1.3/connection.rb', line 94 def send_ccs ccs_record = Message::Record.new( type: Message::ContentType::CCS, legacy_record_version: Message::ProtocolVersion::TLS_1_2, messages: [Message::ChangeCipherSpec.new], cipher: Cryptograph::Passer.new ) send_record(ccs_record) end |
#send_handshakes(type, messages, cipher) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/tttls1.3/connection.rb', line 85 def send_handshakes(type, , cipher) record = Message::Record.new( type:, messages:, cipher: ) send_record(record) end |
#send_record(record) ⇒ Object
134 135 136 137 |
# File 'lib/tttls1.3/connection.rb', line 134 def send_record(record) logger.info(Convert.obj2html(record)) @socket.write(record.serialize(@send_record_size)) end |
#terminate(symbol) ⇒ Object
215 216 217 218 |
# File 'lib/tttls1.3/connection.rb', line 215 def terminate(symbol) send_alert(symbol) raise Error::ErrorAlerts, symbol end |
#write(binary) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/tttls1.3/connection.rb', line 65 def write(binary) # secure channel has not established yet raise Error::ConfigError \ unless (@side == :client && @state == ClientState::CONNECTED) || (@side == :server && @state == ServerState::CONNECTED) ap = Message::ApplicationData.new(binary) send_application_data(ap, @ap_wcipher) end |