Motivation: C++ structures emit a number of symbols into their binaries that are named based on the name of the structure. Most notably, these are the three (or five, depending on mode) magic structure methods: copy constructor, copy assignment operator, destructor. By and large this isn't an issue for us as BoringSSL already namespaces most of its structures. However, a few structures are the target of opaque typedefs in the public header files. These need to use the names that would appear in the internal source. If they didn't, the internal source wouldn't compile! As a result they need to appear outside the inner prefixing namespace. We knew about this limitation at the time we did this work, but hoped we'd get along just fine without changing anything. In fact we did, for nearly three years. But the chickens have come home to roost in https://github.com/grpc/grpc-swift/issues/1134. So it's time to fix it. Fortunately for us, we never write down the names of the inner types in our source code. That means we don't actually care what that name _is_. This gives us our escape hatch: we can namespace the names of the underlying C++ types using the same preprocessor trick as the function names. This will have the effect of also namespacing their magic methods. Modifications: This patch attempts to make this change defensively, by assuming that other structures may be added that meet this pattern. To do it, we compile the binary _again_ and search for any C++ methods that haven't already been namespaced and that don't belong to Swift. We then extract the structure name from them and add a `#define` for it. This forcibly renames the structure in all cases. Result: No more colliding symbols.
SwiftNIO SSL
SwiftNIO SSL is a Swift package that contains an implementation of TLS based on BoringSSL. This package allows users of SwiftNIO to write protocol clients and servers that use TLS to secure data in flight.
The name is inspired primarily by the names of the library this package uses (BoringSSL), and not because we don't know the name of the protocol. We know the protocol is TLS!
To get started, check out the API docs.
Using SwiftNIO SSL
SwiftNIO SSL provides two ChannelHandlers to use to secure a data stream: the NIOSSLClientHandler and the NIOSSLServerHandler. Each of these can be added to a Channel to secure the communications on that channel.
Additionally, we provide a number of low-level primitives for configuring your TLS connections. These will be shown below.
To secure a server connection, you will need a X.509 certificate chain in a file (either PEM or DER, but PEM is far easier), and the associated private key for the leaf certificate. These objects can then be wrapped up in a TLSConfiguration object that is used to initialize the ChannelHandler.
For example:
let configuration = TLSConfiguration.forServer(certificateChain: try NIOSSLCertificate.fromPEMFile("cert.pem").map { .certificate($0) },
privateKey: .file("key.pem"))
let sslContext = try NIOSSLContext(configuration: configuration)
let server = ServerBootstrap(group: group)
.childChannelInitializer { channel in
// important: The handler must be initialized _inside_ the `childChannelInitializer`
let handler = try NIOSSLServerHandler(context: sslContext)
[...]
channel.pipeline.addHandler(handler)
[...]
}
For clients, it is a bit simpler as there is no need to have a certificate chain or private key (though clients may have these things). Setup for clients may be done like this:
let configuration = TLSConfiguration.forClient()
let sslContext = try NIOSSLContext(configuration: configuration)
let client = ClientBootstrap(group: group)
.channelInitializer { channel in
// important: The handler must be initialized _inside_ the `channelInitializer`
let handler = try NIOSSLClientHandler(context: sslContext)
[...]
channel.pipeline.addHandler(handler)
[...]
}