Skip to content

Commit

Permalink
Switched Polish from Transport to Transmission
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Brandon Wiley committed Nov 5, 2021
1 parent 0bf9f8d commit bb89700
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Sources/ReplicantSwift/Polish/Polish.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Crypto

import Foundation
import Transport
import Transmission

public protocol PolishConnection
{
Expand Down
1 change: 1 addition & 0 deletions Sources/ReplicantSwift/Polish/PolishErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ enum HandshakeError: Error
case clientKeyDataIncorrectSize
case unableToDecryptData
case dataCreationError
case writeError
}
70 changes: 30 additions & 40 deletions Sources/ReplicantSwift/Polish/Silver/SilverClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import Logging
import Transport
import Transmission
import Network

import Crypto
Expand Down Expand Up @@ -62,53 +62,43 @@ public class SilverClientConnection
// }
}

public enum PolishError: Error
{
case noData
case failedDecrypt
case writeError
case readError
}

extension SilverClientConnection: PolishConnection
{
public func handshake(connection: Connection, completion: @escaping (Error?) -> Void)
{
log.debug("\n🤝 Client handshake initiation.")
log.debug("\n🤝 Sending Public Key Data")
let paddedKeyData = controller.generatePaddedKeyData(publicKey: publicKey, chunkSize: chunkSize)
connection.send(content: paddedKeyData, contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed(
guard let paddedKeyData = controller.generatePaddedKeyData(publicKey: publicKey, chunkSize: chunkSize) else
{
completion(PolishError.noData)
return
}

guard connection.write(data: paddedKeyData) else
{
(maybeError) in

self.log.error("\n🤝 Handshake: Returned from sending our public key to the server.\n")
guard maybeError == nil
else
{
self.log.error("\n🤝 Received error from server when sending our key: \(maybeError!)")
completion(maybeError!)
return
}

let replicantChunkSize = Int(self.chunkSize)
connection.receive(minimumIncompleteLength: replicantChunkSize, maximumLength: replicantChunkSize, completion:
{
(maybeResponse1Data, maybeResponse1Context, _, maybeResponse1Error) in

self.log.debug("\n🤝 Callback from handshake network.receive called.")
guard maybeResponse1Error == nil
else
{
self.log.error("\n🤝 Received an error while waiting for response from server after sending key: \(maybeResponse1Error!)")
completion(maybeResponse1Error!)
return
}

// This data is meaningless it can be discarded
guard let reponseData = maybeResponse1Data
else
{
self.log.error("\n🤝 Server key response did not contain data.")
completion(nil)
return
}

self.log.debug("\n🤝 Received response data from the server during handshake: \(reponseData)\n")
completion(nil)
})
}))
completion(HandshakeError.writeError)
return
}

let replicantChunkSize = Int(self.chunkSize)
guard let responseData = connection.read(size: replicantChunkSize) else
{
self.log.debug("\n🤝 Callback from handshake network.receive called.")
completion(HandshakeError.writeError)
return
}

self.log.debug("\n🤝 Received response data from the server during handshake: \(responseData)\n")
completion(nil)
}

public func polish(inputData: Data) -> Data?
Expand Down
2 changes: 1 addition & 1 deletion Sources/ReplicantSwift/Polish/Silver/SilverServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import Transport
import Transmission
import Logging

import Crypto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public struct SilverServerConfig: PolishServerConfig, Codable
public let chunkSize: UInt16
public let chunkTimeout: Int


public func construct(logger: Logger) -> PolishServer?
{
let silverServer = SilverServer(logger: logger, chunkSize: chunkSize, chunkTimeout: chunkTimeout)
Expand Down
96 changes: 36 additions & 60 deletions Sources/ReplicantSwift/Polish/Silver/SilverServerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import Logging
import Transport
import Transmission
import Network

import Crypto
Expand Down Expand Up @@ -52,69 +52,45 @@ extension SilverServerConnection: PolishConnection
print("\n🤝 Replicant Server handshake called.")
let replicantChunkSize = chunkSize

//Call receive first
connection.receive(minimumIncompleteLength: Int(replicantChunkSize), maximumLength: Int(replicantChunkSize))
//Call read first
guard let clientPaddedData = connection.read(size: Int(replicantChunkSize)) else
{
(maybeResponse1Data, maybeResponse1Context, _, maybeResponse1Error) in

print("\n🤝 network.receive callback from handshake.")
print("\n🤝 Data received: \(String(describing: maybeResponse1Data?.bytes))")

// Parse received public key and store it
guard maybeResponse1Error == nil
else
{
print("\n\n🤝 Received an error while waiting for response from server acfter sending key: \(maybeResponse1Error!)\n")
completion(maybeResponse1Error!)
return
}

// Make sure we have data
guard let clientPaddedData = maybeResponse1Data
else
{
print("\nClient introduction did not contain data.\n")
completion(HandshakeError.noClientKeyData)
return
}

// Key data is the first chunk of keyDataSize
let clientKeyData = clientPaddedData[..<self.controller.compactKeySize]
print("\n\n🤝 Received an error while waiting for response from server acfter sending key\n")
completion(HandshakeError.noClientKeyData)
return
}


// Convert data to Key
//FIXME: Will decode key method account for leading 04?
guard let clientKey = self.controller.decodeKey(fromData: clientKeyData)
else
{
print("\nUnable to decode client key.\n")
completion(HandshakeError.invalidClientKeyData)
return
}

let derivedKey = self.controller.deriveSymmetricKey(receiverPublicKey: clientKey, senderPrivateKey: self.privateKey)
self.symmetricKey = derivedKey

let configChunkSize = Int(self.chunkSize)

//Generate random data of chunk size
let randomData = generateRandomBytes(count: configChunkSize)

//Send random data to client
connection.send(content: randomData, contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed(
{
(maybeError) in

guard maybeError == nil
else
{
print("\nReceived error from client when sending random data in handshake: \(maybeError!)")
completion(maybeError!)
return
}
}))
print("\n🤝 Data received: \(String(describing: clientPaddedData.bytes))")

// Key data is the first chunk of keyDataSize
let clientKeyData = clientPaddedData[..<self.controller.compactKeySize]

// Convert data to Key
//FIXME: Will decode key method account for leading 04?
guard let clientKey = self.controller.decodeKey(fromData: clientKeyData)
else
{
print("\nUnable to decode client key.\n")
completion(HandshakeError.invalidClientKeyData)
return
}

let derivedKey = self.controller.deriveSymmetricKey(receiverPublicKey: clientKey, senderPrivateKey: self.privateKey)
self.symmetricKey = derivedKey

let configChunkSize = Int(self.chunkSize)

//Generate random data of chunk size
let randomData = generateRandomBytes(count: configChunkSize)

guard connection.write(data: randomData) else
{
completion(HandshakeError.writeError)
return
}

completion(nil)
return
}

public func polish(inputData: Data) -> Data?
Expand Down

0 comments on commit bb89700

Please sign in to comment.