Skip to content

Commit

Permalink
Apply swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed May 15, 2022
1 parent 145c977 commit 2a85b45
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 75 deletions.
12 changes: 4 additions & 8 deletions Sources/NIOSSH/Child Channels/ChildChannelUserEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public enum SSHChannelRequestEvent {
terminalRowHeight: Int,
terminalPixelWidth: Int,
terminalPixelHeight: Int,
terminalModes: SSHTerminalModes)
{
terminalModes: SSHTerminalModes) {
self = .init(wantReply: wantReply,
term: term,
terminalCharacterWidth: UInt32(terminalCharacterWidth),
Expand All @@ -81,8 +80,7 @@ public enum SSHChannelRequestEvent {
terminalRowHeight: UInt32,
terminalPixelWidth: UInt32,
terminalPixelHeight: UInt32,
terminalModes: SSHTerminalModes)
{
terminalModes: SSHTerminalModes) {
self.wantReply = wantReply
self.term = term
self._terminalCharacterWidth = terminalCharacterWidth
Expand Down Expand Up @@ -245,8 +243,7 @@ public enum SSHChannelRequestEvent {
public init(terminalCharacterWidth: Int,
terminalRowHeight: Int,
terminalPixelWidth: Int,
terminalPixelHeight: Int)
{
terminalPixelHeight: Int) {
self = .init(terminalCharacterWidth: UInt32(terminalCharacterWidth),
terminalRowHeight: UInt32(terminalRowHeight),
terminalPixelWidth: UInt32(terminalPixelWidth),
Expand All @@ -256,8 +253,7 @@ public enum SSHChannelRequestEvent {
internal init(terminalCharacterWidth: UInt32,
terminalRowHeight: UInt32,
terminalPixelWidth: UInt32,
terminalPixelHeight: UInt32)
{
terminalPixelHeight: UInt32) {
self._terminalCharacterWidth = terminalCharacterWidth
self._terminalRowHeight = terminalRowHeight
self._terminalPixelWidth = terminalPixelWidth
Expand Down
6 changes: 2 additions & 4 deletions Sources/NIOSSH/Child Channels/SSHChildChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ final class SSHChildChannel {
initializer: Initializer?,
localChannelID: UInt32,
targetWindowSize: Int32,
initialOutboundWindowSize: UInt32)
{
initialOutboundWindowSize: UInt32) {
self.init(allocator: allocator,
parent: parent,
multiplexer: multiplexer,
Expand All @@ -128,8 +127,7 @@ final class SSHChildChannel {
initializer: Initializer?,
initialState: ChildChannelStateMachine,
targetWindowSize: Int32,
initialOutboundWindowSize: UInt32)
{
initialOutboundWindowSize: UInt32) {
self.allocator = allocator
self.closePromise = parent.eventLoop.makePromise()
self.parent = parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ struct SSHConnectionStateMachine {
}

mutating func processInboundMessage(allocator: ByteBufferAllocator,
loop: EventLoop) throws -> StateMachineInboundProcessResult?
{
loop: EventLoop) throws -> StateMachineInboundProcessResult? {
switch self.state {
case .idle:
preconditionFailure("Received messages before sending our first message.")
Expand Down Expand Up @@ -697,8 +696,7 @@ struct SSHConnectionStateMachine {
mutating func processOutboundMessage(_ message: SSHMessage,
buffer: inout ByteBuffer,
allocator: ByteBufferAllocator,
loop: EventLoop) throws
{
loop: EventLoop) throws {
switch self.state {
case .idle(var state):
switch message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ extension SSHConnectionStateMachine {
var userAuthStateMachine: UserAuthenticationStateMachine

init(keyExchangeState state: KeyExchangeState,
loop: EventLoop)
{
loop: EventLoop) {
self.role = state.role
self.parser = state.parser
self.serializer = state.serializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ extension SSHConnectionStateMachine {
var userAuthStateMachine: UserAuthenticationStateMachine

init(keyExchangeState state: KeyExchangeState,
loop: EventLoop)
{
loop: EventLoop) {
self.role = state.role
self.parser = state.parser
self.serializer = state.serializer
Expand Down
3 changes: 1 addition & 2 deletions Sources/NIOSSH/Key Exchange/EllipticCurveKeyExchange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ extension EllipticCurveKeyExchange {
serverHostKey: NIOSSHPrivateKey,
initialExchangeBytes: inout ByteBuffer,
allocator: ByteBufferAllocator,
expectedKeySizes: ExpectedKeySizes) throws -> (KeyExchangeResult, NIOSSHKeyExchangeServerReply)
{
expectedKeySizes: ExpectedKeySizes) throws -> (KeyExchangeResult, NIOSSHKeyExchangeServerReply) {
precondition(self.ourRole.isServer, "Only servers may receive a client key exchange packet!")

// With that, we have enough to finalize the key exchange.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ public struct NIOSSHCertifiedPublicKey {
criticalOptions: [String: String],
extensions: [String: String],
signatureKey: NIOSSHPublicKey,
signature: NIOSSHSignature) throws
{
signature: NIOSSHSignature) throws {
self.backing = try Backing(nonce: nonce,
serial: serial,
type: type,
Expand Down Expand Up @@ -270,8 +269,7 @@ extension NIOSSHCertifiedPublicKey {
public func validate(principal: String,
type: CertificateType,
allowedAuthoritySigningKeys: [NIOSSHPublicKey],
acceptableCriticalOptions: [String] = []) throws -> [String: String]
{
acceptableCriticalOptions: [String] = []) throws -> [String: String] {
// Before we do any computation on values in this certificate, we first need to do the cryptographic
// validation, to avoid the cryptographic doom principle. First, check if the signing key is in our allowed
// set: second, validate the signature.
Expand Down Expand Up @@ -487,8 +485,7 @@ extension NIOSSHCertifiedPublicKey {
criticalOptions: [String: String],
extensions: [String: String],
signatureKey: NIOSSHPublicKey,
signature: NIOSSHSignature) throws
{
signature: NIOSSHSignature) throws {
// These two contrains are _very important_: without them, a number of NIOSSHPublicKey operations become infinitely
// recursive.
if case .certified = key.backingKey {
Expand Down
18 changes: 9 additions & 9 deletions Sources/NIOSSH/Keys And Signatures/NIOSSHPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ extension NIOSSHPublicKey {
}

static var customPublicKeyAlgorithms: [NIOSSHPublicKeyProtocol.Type] {
return _CustomAlgorithms.publicKeyAlgorithmsLock.withLock {
return _CustomAlgorithms.publicKeyAlgorithms
_CustomAlgorithms.publicKeyAlgorithmsLock.withLock {
_CustomAlgorithms.publicKeyAlgorithms
}
}

static var customSignatures: [NIOSSHSignatureProtocol.Type] {
return _CustomAlgorithms.signaturesLock.withLock {
return _CustomAlgorithms.signatures
_CustomAlgorithms.signaturesLock.withLock {
_CustomAlgorithms.signatures
}
}
}
Expand Down Expand Up @@ -267,18 +267,18 @@ public enum NIOSSHAlgorithms {
}

internal var customTransportProtectionSchemes: [NIOSSHTransportProtection.Type] {
return _CustomAlgorithms.transportProtectionSchemesLock.withLock {
return _CustomAlgorithms.transportProtectionSchemes
_CustomAlgorithms.transportProtectionSchemesLock.withLock {
_CustomAlgorithms.transportProtectionSchemes
}
}

internal var customKeyExchangeAlgorithms: [NIOSSHKeyExchangeAlgorithmProtocol.Type] {
return _CustomAlgorithms.keyExchangeAlgorithmsLock.withLock {
return _CustomAlgorithms.keyExchangeAlgorithms
_CustomAlgorithms.keyExchangeAlgorithmsLock.withLock {
_CustomAlgorithms.keyExchangeAlgorithms
}
}

fileprivate enum _CustomAlgorithms {
private enum _CustomAlgorithms {
static var transportProtectionSchemesLock = Lock()
static var transportProtectionSchemes = [NIOSSHTransportProtection.Type]()
static var keyExchangeAlgorithmsLock = Lock()
Expand Down
12 changes: 6 additions & 6 deletions Sources/NIOSSH/Keys And Signatures/NIOSSHSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ extension ByteBuffer {
// For ECDSA-P256, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
else {
return nil
}
Expand All @@ -288,8 +288,8 @@ extension ByteBuffer {
// For ECDSA-P384, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
else {
return nil
}
Expand All @@ -307,8 +307,8 @@ extension ByteBuffer {
// For ECDSA-P521, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString()
else {
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/NIOSSH/SSHClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public struct SSHClientConfiguration {

public init(userAuthDelegate: NIOSSHClientUserAuthenticationDelegate,
serverAuthDelegate: NIOSSHClientServerAuthenticationDelegate,
globalRequestDelegate: GlobalRequestDelegate? = nil)
{
globalRequestDelegate: GlobalRequestDelegate? = nil) {
self.userAuthDelegate = userAuthDelegate
self.serverAuthDelegate = serverAuthDelegate
self.globalRequestDelegate = globalRequestDelegate ?? DefaultGlobalRequestDelegate()
Expand Down
2 changes: 1 addition & 1 deletion Sources/NIOSSH/SSHMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ extension ByteBuffer {
mutating func readUserAuthBannerMessage() -> SSHMessage.UserAuthBannerMessage? {
self.rewindReaderOnNil { `self` in
guard let message = self.readSSHStringAsString(),
let languageTag = self.readSSHStringAsString()
let languageTag = self.readSSHStringAsString()
else {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/NIOSSH/TransportProtection/AESGCM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class AESGCMTransportProtection {

required init(initialKeys: NIOSSHSessionKeys) throws {
guard initialKeys.outboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8,
initialKeys.inboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8
initialKeys.inboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8
else {
throw NIOSSHError.invalidKeySize
}
Expand All @@ -65,7 +65,7 @@ extension AESGCMTransportProtection: NIOSSHTransportProtection {

func updateKeys(_ newKeys: NIOSSHSessionKeys) throws {
guard newKeys.outboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8,
newKeys.inboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8
newKeys.inboundEncryptionKey.bitCount == Self.keySizes.encryptionKeySize * 8
else {
throw NIOSSHError.invalidKeySize
}
Expand All @@ -89,9 +89,9 @@ extension AESGCMTransportProtection: NIOSSHTransportProtection {
// The first 4 bytes are the length. The last 16 are the tag. Everything else is ciphertext. We expect
// that the ciphertext is a clean multiple of the block size, and to be non-zero.
guard let lengthView = source.readSlice(length: 4)?.readableBytesView,
let ciphertextView = source.readSlice(length: source.readableBytes - 16)?.readableBytesView,
let tagView = source.readSlice(length: 16)?.readableBytesView,
ciphertextView.count > 0, ciphertextView.count % Self.cipherBlockSize == 0
let ciphertextView = source.readSlice(length: source.readableBytes - 16)?.readableBytesView,
let tagView = source.readSlice(length: 16)?.readableBytesView,
ciphertextView.count > 0, ciphertextView.count % Self.cipherBlockSize == 0
else {
// The only way this fails is if the payload doesn't match this encryption scheme.
throw NIOSSHError.invalidEncryptedPacketLength
Expand Down
3 changes: 1 addition & 2 deletions Sources/NIOSSHClient/PortForwardingServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ final class PortForwardingServer {
init(group: EventLoopGroup,
bindHost: Substring,
bindPort: Int,
_ forwardingChannelConstructor: @escaping (Channel) -> EventLoopFuture<Void>)
{
_ forwardingChannelConstructor: @escaping (Channel) -> EventLoopFuture<Void>) {
self.serverLoop = group.next()
self.group = group
self.forwardingChannelConstructor = forwardingChannelConstructor
Expand Down
3 changes: 1 addition & 2 deletions Tests/NIOSSHTests/SSHKeyExchangeStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,7 @@ private extension SSHKeyExchangeStateMachineTests {
currentStage: SSHKeyExchangeStateMachineTests.HandshakeStages,
stateMachine: inout SSHKeyExchangeStateMachine,
file: StaticString = #file,
line: UInt = #line) throws
{
line: UInt = #line) throws {
if allowedStages.contains(currentStage) {
// If this stage is allowed, don't try to send the message. Assume it's covered in the mainline.
return
Expand Down
10 changes: 4 additions & 6 deletions Tests/NIOSSHTests/UserAuthenticationStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ final class UserAuthenticationStateMachineTests: XCTestCase {
// For signed methods we need to be a bit careful: we can't assume that the signature will have a bitwise match, so we have to validate it
// instead.
if case .some(.publicKey(.known(let expectedKey, _))) = expectedMessage.map({ $0.method }),
case .some(.publicKey(.known(let actualKey, let actualSignature))) = request.map({ $0.method }),
let userAuthPayload = userAuthPayload
{
case .some(.publicKey(.known(let actualKey, let actualSignature))) = request.map({ $0.method }),
let userAuthPayload = userAuthPayload {
XCTAssertEqual(expectedMessage!.username, request!.username)
XCTAssertEqual(expectedMessage!.service, request!.service)
XCTAssertEqual(expectedKey, actualKey)
Expand Down Expand Up @@ -174,9 +173,8 @@ final class UserAuthenticationStateMachineTests: XCTestCase {
// For signed methods we need to be a bit careful: we can't assume that the signature will have a bitwise match, so we have to validate it
// instead.
if case .some(.publicKey(.known(let expectedKey, _))) = expectedMessage.map({ $0.method }),
case .some(.publicKey(.known(let actualKey, let actualSignature))) = request.map({ $0.method }),
let userAuthPayload = userAuthPayload
{
case .some(.publicKey(.known(let actualKey, let actualSignature))) = request.map({ $0.method }),
let userAuthPayload = userAuthPayload {
XCTAssertEqual(expectedMessage!.username, request!.username)
XCTAssertEqual(expectedMessage!.service, request!.service)
XCTAssertEqual(expectedKey, actualKey)
Expand Down
28 changes: 14 additions & 14 deletions Tests/NIOSSHTests/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ enum InsecureEncryptionAlgorithm {
var plaintext = plaintext
var key = key
guard let k0 = key.readInteger(as: UInt32.self),
let k1 = key.readInteger(as: UInt32.self),
let k2 = key.readInteger(as: UInt32.self),
let k3 = key.readInteger(as: UInt32.self)
let k1 = key.readInteger(as: UInt32.self),
let k2 = key.readInteger(as: UInt32.self),
let k3 = key.readInteger(as: UInt32.self)
else {
preconditionFailure("check key size")
}

while var block = plaintext.readSlice(length: 16) {
guard var v0 = block.readInteger(as: UInt32.self),
var v1 = block.readInteger(as: UInt32.self),
var v2 = block.readInteger(as: UInt32.self),
var v3 = block.readInteger(as: UInt32.self)
var v1 = block.readInteger(as: UInt32.self),
var v2 = block.readInteger(as: UInt32.self),
var v3 = block.readInteger(as: UInt32.self)
else {
preconditionFailure("check block size")
}
Expand All @@ -78,18 +78,18 @@ enum InsecureEncryptionAlgorithm {
var ciphertext = ciphertext
var key = key
guard let k0 = key.readInteger(as: UInt32.self),
let k1 = key.readInteger(as: UInt32.self),
let k2 = key.readInteger(as: UInt32.self),
let k3 = key.readInteger(as: UInt32.self)
let k1 = key.readInteger(as: UInt32.self),
let k2 = key.readInteger(as: UInt32.self),
let k3 = key.readInteger(as: UInt32.self)
else {
preconditionFailure("check key size")
}

while var block = ciphertext.readSlice(length: 16) {
guard var v0 = block.readInteger(as: UInt32.self),
var v1 = block.readInteger(as: UInt32.self),
var v2 = block.readInteger(as: UInt32.self),
var v3 = block.readInteger(as: UInt32.self)
var v1 = block.readInteger(as: UInt32.self),
var v2 = block.readInteger(as: UInt32.self),
var v3 = block.readInteger(as: UInt32.self)
else {
preconditionFailure("check block size")
}
Expand Down Expand Up @@ -171,7 +171,7 @@ class TestTransportProtection: NIOSSHTransportProtection {
let index = source.readerIndex

guard let ciphertextView = source.viewBytes(at: index, length: Self.cipherBlockSize),
ciphertextView.count > 0, ciphertextView.count % Self.cipherBlockSize == 0
ciphertextView.count > 0, ciphertextView.count % Self.cipherBlockSize == 0
else {
// The only way this fails is if the payload doesn't match this encryption scheme.
throw NIOSSHError.invalidEncryptedPacketLength
Expand Down Expand Up @@ -200,7 +200,7 @@ class TestTransportProtection: NIOSSHTransportProtection {
// First block is expected to be decoded by decodeFirstBlock
if source.readableBytes > self.macBytes {
guard let ciphertext = source.readSlice(length: source.readableBytes - 32),
ciphertext.readableBytes > 0, ciphertext.readableBytes % Self.cipherBlockSize == 0
ciphertext.readableBytes > 0, ciphertext.readableBytes % Self.cipherBlockSize == 0
else {
// The only way this fails is if the payload doesn't match this encryption scheme.
throw NIOSSHError.invalidEncryptedPacketLength
Expand Down

0 comments on commit 2a85b45

Please sign in to comment.