Is the code in 'Building a custom peer-to-peer protocol' insecure?

I'm new to Networking, so forgive me if this is a silly question:

In the sample code, Building a custom peer-to-peer protocol, TLS is configured as follows:

	// Create TLS options using a passcode to derive a pre-shared key.
	private static func tlsOptions(passcode: String) -> NWProtocolTLS.Options {
		let tlsOptions = NWProtocolTLS.Options()

		let authenticationKey = SymmetricKey(data: passcode.data(using: .utf8)!)
		var authenticationCode = HMAC<SHA256>.authenticationCode(for: "TicTacToe".data(using: .utf8)!, using: authenticationKey)

		let authenticationDispatchData = withUnsafeBytes(of: &authenticationCode) { (ptr: UnsafeRawBufferPointer) in
			DispatchData(bytes: ptr)
		}

		sec_protocol_options_add_pre_shared_key(tlsOptions.securityProtocolOptions,
												authenticationDispatchData as __DispatchData,
												stringToDispatchData("TicTacToe")! as __DispatchData)
		sec_protocol_options_append_tls_ciphersuite(tlsOptions.securityProtocolOptions,
													tls_ciphersuite_t(rawValue: TLS_PSK_WITH_AES_128_GCM_SHA256)!)
		return tlsOptions
	}

The sample code touts the connection as secure ("...uses Bonjour and TLS to establish secure connections between nearby devices"), but to my untrained eye it doesn't seem so.

My reasoning is as follows: If I adapt this code as-is, so connections between two instances of my app use SymmetricKeys derived from the four-digit passcode, then wouldn't my encryption be easy to break by an adversary who sends 0000...9999 and records corresponding changes in the encryption, exposing my app to all sorts of attacks?

The sample uses the passcode to validate the connection (host user shows client user the passcode, which is manually entered), which is a feature I would like to keep in some form or another, which is why this is causing so many headaches.

Generally speaking, is there a way to secure a local peer-to-peer connection over Network.framework that doesn't involve certificates? If certificates are the only way, are there good resources you can recommend?

I’ve been discussing your concerns with colleagues internally and we’ve got to the point where we’d like to get a bug on file so that we can officially respond. Please file a bug against the sample.

IMPORTANT Reply here with the bug number so that I see it. Don’t use a comment, because I may not see that.

Thanks!

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I was away but I finally filed a report: FB13589481

Since it seems the code that was touted as secure is questionable at best, are there any alternatives? To get around some issues I'm attaching passwords to the Bonjour context and using a single, shared PSK (not stored as a string) to secure connections, but this is just a stopgap measure.

Also, I reposted my comment to your reply to my other thread on wired data transfer between devices.. I'd be grateful if you would be kind enough to take a second look at my thread.

I was away but I finally filed a report: FB13589481

Thanks.

are there any alternatives?

I’m reticent to promote a solution as ‘secure’ here on DevForums, where I have little time to spend research. If you’d like a better answer, my advice is that you open up a DTS tech support incident, which will give me more time to look into this.

If you do open a TSI, make sure to reference this thread.

Also, I reposted my comment to your reply to my other thread

I already have that open in a tab (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn,

I'm eagerly anticipating the official response you mentioned; is there an approximate 'when' and 'where' I can look forward to seeing this?

You may receive a response via your bug report; that’s not something I have direct input on.

If you really want to drive this forward, I recommend that you open a DTS TSI per my reply above.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn,

I decided to take your advice and submitted a TSI referencing this thread (ID 7462655).

I look forward to seeing your solutions!

Is the code in 'Building a custom peer-to-peer protocol' insecure?
 
 
Q