Skip to content

Commit

Permalink
Merge pull request #5 from IanKeen/07-25
Browse files Browse the repository at this point in the history
07-25 update
  • Loading branch information
loganwright authored Jul 27, 2016
2 parents ef6f08c + 266a726 commit e979239
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0-PREVIEW-2
DEVELOPMENT-SNAPSHOT-2016-07-25-a
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
sudo apt-get install libssl-dev;
fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)";
fi
- eval "$(curl -sL swift.qutheory.io/travis)"
script:
# Build TLS
- swift build
Expand Down
23 changes: 9 additions & 14 deletions Sources/TLS/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import Foundation
be reused when creating multiple sockets.
*/

#if !os(Linux)
// Temporary workaround to name differences on Linux and Mac
typealias NSFileManager = FileManager
#endif

public final class Context {
public typealias CContext = UnsafeMutablePointer<SSL_CTX>
public let cContext: CContext
Expand Down Expand Up @@ -46,7 +41,7 @@ public final class Context {
let method = try Method(mode: mode)

guard let context = SSL_CTX_new(method.cMethod) else {
throw Error.contextCreation
throw TLSError.contextCreation
}

cContext = context
Expand Down Expand Up @@ -95,8 +90,8 @@ public final class Context {
Verifies that a file exists at the supplied path.
*/
public func verifyFile(_ filePath: String) throws {
guard NSFileManager.fileExists(at: filePath) else {
throw Error.file("\(filePath) doesn't exist.")
guard FileManager.fileExists(at: filePath) else {
throw TLSError.file("\(filePath) doesn't exist.")
}
}

Expand All @@ -123,7 +118,7 @@ public final class Context {
try verifyFile(caCertificateFile)

guard SSL_CTX_load_verify_locations(cContext, caCertificateFile, nil) == Result.OK else {
throw Error.loadCACertificate(error)
throw TLSError.loadCACertificate(error)
}
}

Expand All @@ -133,7 +128,7 @@ public final class Context {
*/
public func loadVerifyLocations(directory caCertificateDirectory: String) throws {
guard SSL_CTX_load_verify_locations(cContext, nil, caCertificateDirectory) == Result.OK else {
throw Error.loadCACertificate(error)
throw TLSError.loadCACertificate(error)
}
}

Expand All @@ -147,7 +142,7 @@ public final class Context {
try verifyFile(certificateFile)

guard SSL_CTX_use_certificate_file(cContext, certificateFile, SSL_FILETYPE_PEM) == Result.OK else {
throw Error.useCertificate(error)
throw TLSError.useCertificate(error)
}
}

Expand All @@ -161,7 +156,7 @@ public final class Context {
try verifyFile(chainFile)

guard SSL_CTX_use_certificate_chain_file(cContext, chainFile) == Result.OK else {
throw Error.useChain(error)
throw TLSError.useChain(error)
}
}

Expand All @@ -175,11 +170,11 @@ public final class Context {
try verifyFile(privateKeyFile)

guard SSL_CTX_use_PrivateKey_file(cContext, privateKeyFile, SSL_FILETYPE_PEM) == Result.OK else {
throw Error.usePrivateKey(error)
throw TLSError.usePrivateKey(error)
}

guard SSL_CTX_check_private_key(cContext) == Result.OK else {
throw Error.checkPrivateKey(error)
throw TLSError.checkPrivateKey(error)
}
}
}
2 changes: 1 addition & 1 deletion Sources/TLS/Error.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import COpenSSL

public enum Error: ErrorProtocol {
public enum TLSError: Error {
case methodCreation
case contextCreation
case loadCACertificate(String)
Expand Down
4 changes: 2 additions & 2 deletions Sources/TLS/Method.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class Method {
switch mode {
case .client:
guard let m = SSLv23_client_method() else {
throw Error.methodCreation
throw TLSError.methodCreation

}
method = m
case .server:
guard let m = SSLv23_server_method() else {
throw Error.methodCreation
throw TLSError.methodCreation
}
method = m
}
Expand Down
29 changes: 16 additions & 13 deletions Sources/TLS/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class Socket {
*/
public init(context: Context, descriptor: Int32) throws {
guard let ssl = SSL_new(context.cContext) else {
throw Error.socketCreation(error)
throw TLSError.socketCreation(error)
}

SSL_set_fd(ssl, descriptor)
Expand All @@ -40,7 +40,7 @@ public final class Socket {
public func connect() throws {
let result = SSL_connect(cSSL)
guard result == Result.OK else {
throw Error.connect(SocketError(result), error)
throw TLSError.connect(SocketError(result), error)
}
}

Expand All @@ -52,7 +52,7 @@ public final class Socket {
public func accept() throws {
let result = SSL_accept(cSSL)
guard result == Result.OK else {
throw Error.accept(SocketError(result), error)
throw TLSError.accept(SocketError(result), error)
}
}

Expand All @@ -62,16 +62,16 @@ public final class Socket {
- parameter max: The maximum amount of bytes to receive.
*/
public func receive(max: Int) throws -> [UInt8] {
let pointer = UnsafeMutablePointer<UInt8>.init(allocatingCapacity: max)
let pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: max)
defer {
pointer.deallocateCapacity(max)
pointer.deallocate(capacity: max)
}

let result = SSL_read(cSSL, pointer, max.int32)
let bytesRead = Int(result)

guard bytesRead >= 0 else {
throw Error.receive(SocketError(result), error)
throw TLSError.receive(SocketError(result), error)
}


Expand All @@ -90,22 +90,25 @@ public final class Socket {
let bytesSent = SSL_write(cSSL, buffer.baseAddress, bytes.count.int32)

guard bytesSent >= 0 else {
throw Error.send(SocketError(bytesSent), error)
throw TLSError.send(SocketError(bytesSent), error)
}
}

/**
Verifies the connection with the peer.

- throws: Error.invalidPeerCertificate(PeerCertificateError)
- throws: TLSError.invalidPeerCertificate(PeerCertificateError)
*/
public func verifyConnection() throws {
if case .server = context.mode where context.certificates.areSelfSigned {
if
case.server = context.mode,
context.certificates.areSelfSigned
{
return
}

guard let certificate = SSL_get_peer_certificate(cSSL) else {
throw Error.invalidPeerCertificate(.notPresented)
throw TLSError.invalidPeerCertificate(.notPresented)
}
defer {
X509_free(certificate)
Expand All @@ -117,10 +120,10 @@ public final class Socket {
break
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
if !context.certificates.areSelfSigned {
throw Error.invalidPeerCertificate(.noIssuerCertificate)
throw TLSError.invalidPeerCertificate(.noIssuerCertificate)
}
default:
throw Error.invalidPeerCertificate(.invalid)
throw TLSError.invalidPeerCertificate(.invalid)
}
}
}
6 changes: 3 additions & 3 deletions Sources/TLS/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ extension Int {
}
}

extension NSFileManager {
extension FileManager {
static func fileExists(at path: String) -> Bool {
#if os(Linux)
let manager = NSFileManager.defaultManager()
let manager = FileManager.default()
#else
let manager = NSFileManager.default
let manager = FileManager.default
#endif

var directory: ObjCBool = false
Expand Down

0 comments on commit e979239

Please sign in to comment.