Skip to content

Commit

Permalink
Add SSL certificate and key
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni committed May 2, 2019
1 parent 4f5d2a6 commit 1f8e27c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Tests/KituraWebSocketTests/SSLConfig/SSLConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import SSLService

// MARK: SSLConfig
/// A struct that allows you to configure your SSL using a CA certificate file (Linux), a CA certificate directory (Linux) or a certificate chain file (MacOS).
public struct SSLConfig {

/// :nodoc:
public private(set) var config: SSLService.Configuration

// MARK: Lifecycle

#if os(Linux)
// MARK: For Linux
/// Initialize an `SSLService.Configuration` instance using a CA certificate file.
///
/// - Parameter caCertificateFilePath: Path to the PEM formatted CA certificate file.
/// - Parameter certificateFilePath: Path to the PEM formatted certificate file.
/// - Parameter keyFilePath: Path to the PEM formatted key file. If nil, `certificateFilePath` will be used.
/// - Parameter selfSigned: True if certs are *self-signed*, false otherwise. Defaults to true.
/// - Parameter cipherSuite: Unused.
/// - Returns: New `SSLConfig` instance.
public init(withCACertificateFilePath caCertificateFilePath: String?, usingCertificateFile certificateFilePath: String?, withKeyFile keyFilePath: String? = nil, usingSelfSignedCerts selfSigned: Bool = true, cipherSuite: String? = nil) {

config = SSLService.Configuration(withCACertificateFilePath: caCertificateFilePath, usingCertificateFile: certificateFilePath, withKeyFile: keyFilePath, usingSelfSignedCerts: selfSigned, cipherSuite: cipherSuite)
}

/// Initialize an `SSLService.Configuration` instance using a CA certificate directory.
///
/// *Note:* `caCertificateDirPath` - all certificates in the specified directory **must** be hashed using the NIOSSL Certificate Tool.
///
/// - Parameter caCertificateDirPath: Path to a directory containing CA certificates. *(see note above)*
/// - Parameter certificateFilePath: Path to the PEM formatted certificate file. If nil, `certificateFilePath` will be used.
/// - Parameter keyFilePath: Path to the PEM formatted key file (optional). If nil, `certificateFilePath` is used.
/// - Parameter selfSigned: True if certs are *self-signed*, false otherwise. Defaults to true.
/// - Parameter cipherSuite: Unused.
/// - Returns: New `SSLConfig` instance.
public init(withCACertificateDirectory caCertificateDirPath: String?, usingCertificateFile certificateFilePath: String?, withKeyFile keyFilePath: String? = nil, usingSelfSignedCerts selfSigned: Bool = true, cipherSuite: String? = nil) {

config = SSLService.Configuration(withCACertificateDirectory: caCertificateDirPath, usingCertificateFile: certificateFilePath, withKeyFile: keyFilePath, usingSelfSignedCerts: selfSigned, cipherSuite: cipherSuite)
}
#endif // os(Linux)
// MARK: For MacOS
/// Initialize an `SSLService.Configuration` instance using a certificate chain file.
///
/// *Note:* If using a certificate chain file, the certificates must be in PEM format and must be sorted starting with the subject's certificate (actual client or server certificate), followed by intermediate CA certificates if applicable, and ending at the highest level (root) CA.
///
/// For testing purposes you will most likely want to create and use some self-signed certificates. Follow the
/// instructions in our [Enabling SSL/TLS On Your Kitura Server](http://www.kitura.io/en/resources/tutorials/ssl.html) tutorial.
/// ### Usage Example: ###
/// This example initializes an `SSLConfig` instance and then associates this SSL configuration with the Kitura HTTP
/// server registration.
/// ```swift
/// let mySSLConfig = SSLConfig(withChainFilePath: "/tmp/Creds/Self-Signed/cert.pfx",
/// withPassword: "password",
/// usingSelfSignedCerts: true)
/// Kitura.addHTTPServer(onPort: 8080, with: router, withSSL: mySSLConfig)
/// ```
/// - Parameter chainFilePath: Path to the certificate chain file (optional). *(See note above)*
/// - Parameter password: Export password for the chain file (optional). This is required if using a certificate chain file.
/// - Parameter selfSigned: True if certs are *self-signed*, false otherwise. Defaults to true.
/// - Parameter cipherSuite: Unused.
/// - Returns: New `SSLConfig` instance.
public init(withChainFilePath chainFilePath: String? = nil, withPassword password: String? = nil, usingSelfSignedCerts selfSigned: Bool = true, cipherSuite: String? = nil) {

config = SSLService.Configuration(withChainFilePath: chainFilePath, withPassword: password, usingSelfSignedCerts: selfSigned, cipherSuite: cipherSuite)
}
}
24 changes: 24 additions & 0 deletions Tests/KituraWebSocketTests/SSLConfig/certificate.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-----BEGIN CERTIFICATE-----
MIIECTCCAvGgAwIBAgIJAOztRzGO7IBlMA0GCSqGSIb3DQEBCwUAMGAxCzAJBgNV
BAYTAlVTMQ4wDAYDVQQIEwVUZXhhczEPMA0GA1UEBxMGQXVzdGluMQwwCgYDVQQK
EwNJQk0xDjAMBgNVBAsTBVN3aWZ0MRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTcw
MTEwMjAzMjM0WhcNMjcwMTA4MjAzMjM0WjBgMQswCQYDVQQGEwJVUzEOMAwGA1UE
CBMFVGV4YXMxDzANBgNVBAcTBkF1c3RpbjEMMAoGA1UEChMDSUJNMQ4wDAYDVQQL
EwVTd2lmdDESMBAGA1UEAxMJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAyEKi6MV6ABBCl9hyTztcXNElkI3JBWpKROWt4ReNauvKrdA7
vD50o3MCfkDPOBCsx+mKxL66Nw4CquWk720fXoKZ8X8JFYJfkK6yEViw/wSnBb3A
XH7j2jrXBXK8ccY2yJy3XBNMNinMI969PItRysYhJO+yJG7/SyNAnnyvrIxmAgZX
qD7VaJq/XB8liynnxfdl08FprJDBjeXNHRNU5FtB9fastnVORQSKYOQRHk3vEArV
GX2BwBzQM8uXcXiRwDk7vrtusPrNqdyCJfD8HccNYB2AomZb5zukXrt8c3/fveke
kgqnWxVdOh0nFZGyq9PAQ9YTsarH/zVY4YoXbQIDAQABo4HFMIHCMB0GA1UdDgQW
BBQbyz6NOaU+R1f0OqfbN6KoFQT3WDCBkgYDVR0jBIGKMIGHgBQbyz6NOaU+R1f0
OqfbN6KoFQT3WKFkpGIwYDELMAkGA1UEBhMCVVMxDjAMBgNVBAgTBVRleGFzMQ8w
DQYDVQQHEwZBdXN0aW4xDDAKBgNVBAoTA0lCTTEOMAwGA1UECxMFU3dpZnQxEjAQ
BgNVBAMTCWxvY2FsaG9zdIIJAOztRzGO7IBlMAwGA1UdEwQFMAMBAf8wDQYJKoZI
hvcNAQELBQADggEBAJ8iFApNhHlhDM+VREPRbXRGP34MeCh3kJgQV10fKM5knpdu
TMG9HBq0yEM4bQUkj6ebK12FktdTAM9FNr0WdUCjW9mTqWDYHTuTIRVOmdywT2be
TVRwGmTFjGJM6BWvzlFIgV7bR+m/GXhbUzllvGqv4YqVCB2UKPlr3XSRzlF2s0pG
OfSa9Rj7d5S4RgVeoGkz/mhHgf8iUopjqOvehESPI+WRR/RppK0WJVah04VcY4tC
OLnLBLqUM/YmYWolO126qszz0kFPh33MFknHlAuFnXEhOp8PAf+yE/9fRaOHcGHP
ttQVP8ozLPje39zOuo0Zv1Zp3V0ElX5sv7ImdC8=
-----END CERTIFICATE-----
Binary file not shown.
27 changes: 27 additions & 0 deletions Tests/KituraWebSocketTests/SSLConfig/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAyEKi6MV6ABBCl9hyTztcXNElkI3JBWpKROWt4ReNauvKrdA7
vD50o3MCfkDPOBCsx+mKxL66Nw4CquWk720fXoKZ8X8JFYJfkK6yEViw/wSnBb3A
XH7j2jrXBXK8ccY2yJy3XBNMNinMI969PItRysYhJO+yJG7/SyNAnnyvrIxmAgZX
qD7VaJq/XB8liynnxfdl08FprJDBjeXNHRNU5FtB9fastnVORQSKYOQRHk3vEArV
GX2BwBzQM8uXcXiRwDk7vrtusPrNqdyCJfD8HccNYB2AomZb5zukXrt8c3/fveke
kgqnWxVdOh0nFZG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