Currently, I have a client by using NWConnection for a socket connection to a server in local network. My server address is ***.***.***.***:YYYY
The client can connected to the server with the code below:
func connect() {
let connection = NWConnection(host: .init("***.***.***.***"), port: .init(integerLiteral: YYYY), using: NWParameters(tls: nil, tcp: .init()))
connection.stateUpdateHandler = { state in
print(state)
if state == .ready {
receiveData()
}
}
connection.start(queue: .global())
}
private func receiveData() {
self.connection?.receive(minimumIncompleteLength: 1, maximumLength: 8192) { [weak self] (data, context, isComplete, error) in
guard let self = self else { return }
if let error = error {
self.socketConnectionStateCallBack(.onError(connection: self, error: error))
return
}
if let connection = connection, connection.state == .ready && isComplete == false,
let data = data, !data.isEmpty {
self.socketConnectionStateCallBack(.onDataReceived(connection: self, data: data))
}
}
}
The stateUpdateHandler callback with state == .ready and there is a receive method in that block also, so the client receive an encrypted String from the server.
At this time, the client should do TSL handshake with server. (I have a certificate file)
I already tried configuring TLS in NWParameters:
func createTLSParameters(allowInsecure: Bool, queue: DispatchQueue) -> NWParameters {
let options = NWProtocolTLS.Options()
sec_protocol_options_set_verify_block(options.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in
let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue()
var error: CFError?
if SecTrustEvaluateWithError(trust, &error) {
sec_protocol_verify_complete(true)
} else {
if allowInsecure == true {
sec_protocol_verify_complete(true)
} else {
sec_protocol_verify_complete(false)
}
}
}, queue)
return NWParameters(tls: options)
}
but received the errors:
2023-06-26 13:44:52.793596+0700 TestNWConnection[8571:237696] [boringssl] boringssl_context_handle_fatal_alert(1991) [C1:4][0x7f9807c051f0] write alert, level: fatal, description: protocol version
2023-06-26 13:44:52.793784+0700 TestNWConnection[8571:237696] [boringssl] boringssl_context_error_print(1981) [C1:4][0x7f9807c051f0] Error: 140290895852456:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:/AppleInternal/Library/BuildRoots/9c39860a-c3e2-11ed-88f7-863efbbaf80d/Library/Caches/com.apple.xbs/Sources/boringssl/ssl/tls_record.cc:242:
2023-06-26 13:44:52.794547+0700 TestNWConnection[8571:237696] [boringssl] boringssl_session_handshake_incomplete(88) [C1:4][0x7f9807c051f0] SSL library error
2023-06-26 13:44:52.794617+0700 TestNWConnection[8571:237696] [boringssl] boringssl_session_handshake_error_print(43) [C1:4][0x7f9807c051f0] Error: 140290895852456:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER:/AppleInternal/Library/BuildRoots/9c39860a-c3e2-11ed-88f7-863efbbaf80d/Library/Caches/com.apple.xbs/Sources/boringssl/ssl/tls_record.cc:242:
2023-06-26 13:44:52.794660+0700 TestNWConnection[8571:237696] [boringssl] nw_protocol_boringssl_handshake_negotiate_proceed(771) [C1:4][0x7f9807c051f0] handshake failed at state 12288: not completed
waiting(-9836: bad protocol version)
2023-06-26 13:44:52.833700+0700 TestNWConnection[8571:238121] [tcp] tcp_input [C1:5] flags=[R.] seq=764001948, ack=1321044260, win=506 state=CLOSED rcv_nxt=764000508, snd_una=1321044252
So can you help me to perform TLS Handshake with NWConnection after connected TCP to Server? Many thanks!