0001    // SSLSession.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDINbG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    import COpenSSL
0026    
0027    public class Session
ClientStream.swift:32
    private let ssl: Session
ServerStream.swift:34
	private let ssl: Session
{ 0028 0029 public enum Error
Session.swift:56
            throw Error.Session(description: lastSSLErrorDescription)
Session.swift:110
                throw Error.WantRead(description: lastSSLErrorDescription)
Session.swift:112
                throw Error.WantWrite(description: lastSSLErrorDescription)
Session.swift:114
                throw Error.Session(description: lastSSLErrorDescription)
Session.swift:136
                throw Error.WantRead(description: lastSSLErrorDescription)
Session.swift:138
                throw Error.WantWrite(description: lastSSLErrorDescription)
Session.swift:140
                throw Error.ZeroReturn(description: lastSSLErrorDescription)
Session.swift:142
                throw Error.Session(description: lastSSLErrorDescription)
: ErrorType { 0030 case Session
Session.swift:56
            throw Error.Session(description: lastSSLErrorDescription)
Session.swift:114
                throw Error.Session(description: lastSSLErrorDescription)
Session.swift:142
                throw Error.Session(description: lastSSLErrorDescription)
(description: String) 0031 case WantRead
ClientStream.swift:70
            } catch Session.Error.WantRead {
ClientStream.swift:91
            } catch Session.Error.WantRead {
ServerStream.swift:61
            } catch Session.Error.WantRead {}
ServerStream.swift:73
            } catch Session.Error.WantRead {
Session.swift:110
                throw Error.WantRead(description: lastSSLErrorDescription)
Session.swift:136
                throw Error.WantRead(description: lastSSLErrorDescription)
(description: String) 0032 case WantWrite
Session.swift:112
                throw Error.WantWrite(description: lastSSLErrorDescription)
Session.swift:138
                throw Error.WantWrite(description: lastSSLErrorDescription)
(description: String) 0033 case ZeroReturn
ClientStream.swift:81
            } catch Session.Error.ZeroReturn {
Session.swift:140
                throw Error.ZeroReturn(description: lastSSLErrorDescription)
(description: String) 0034 } 0035 0036 public enum State
Session.swift:78
        let state = State(rawValue: stateNumber)
Session.swift:76
	public var state: State {
: Int32 { 0037 case Connect = 0x1000 0038 case Accept = 0x2000 0039 case Mask = 0x0FFF 0040 case Init = 0x3000 0041 case Before = 0x4000 0042 case OK = 0x03 0043 case Renegotiate = 0x3004 0044 case Error = 0x05 0045 case Unknown
Session.swift:79
		return state ?? .Unknown
= -1 0046 } 0047 0048 var ssl
Session.swift:53
        ssl = SSL_new(context.context)
Session.swift:55
        if ssl == nil {
Session.swift:65
        SSL_set_accept_state(ssl)
Session.swift:69
        SSL_set_connect_state(ssl)
Session.swift:73
        return String.fromCString(SSL_state_string_long(ssl))!
Session.swift:77
		let stateNumber = SSL_state(ssl)
Session.swift:83
		let certificate = SSL_get_peer_certificate(ssl)
Session.swift:97
        SSL_set_bio(ssl, readIO.bio, writeIO.bio)
Session.swift:101
        return SSL_state(ssl) == SSL_ST_OK
Session.swift:105
        let result = SSL_do_handshake(ssl)
Session.swift:108
            switch SSL_get_error(ssl, result) {
Session.swift:150
		SSL_shutdown(ssl)
: UnsafeMutablePointer<SSL> 0049 0050 public init(context: Context) throws { 0051 OpenSSL.initialize() 0052 0053 ssl = SSL_new(context.context) 0054 0055 if ssl == nil { 0056 throw Error.Session(description: lastSSLErrorDescription) 0057 } 0058 } 0059 0060 deinit { 0061 shutdown() 0062 } 0063 0064 public func setAcceptState() { 0065 SSL_set_accept_state(ssl) 0066 } 0067 0068 public func setConnectState() { 0069 SSL_set_connect_state(ssl) 0070 } 0071 0072 public var stateDescription: String { 0073 return String.fromCString(SSL_state_string_long(ssl))! 0074 } 0075 0076 public var state: State { 0077 let stateNumber = SSL_state(ssl) 0078 let state = State(rawValue: stateNumber) 0079 return state ?? .Unknown 0080 } 0081 0082 public var peerCertificate: Certificate? { 0083 let certificate = SSL_get_peer_certificate(ssl) 0084 0085 guard certificate != nil else { 0086 return nil 0087 } 0088 0089 defer { 0090 X509_free(certificate) 0091 } 0092 0093 return Certificate(certificate: certificate) 0094 } 0095 0096 public func setIO(readIO readIO: IO, writeIO: IO) { 0097 SSL_set_bio(ssl, readIO.bio, writeIO.bio) 0098 } 0099 0100 var initializationFinished
ClientStream.swift:88
        while !ssl.initializationFinished {
ClientStream.swift:96
                if ssl.initializationFinished {
ServerStream.swift:58
        while !ssl.initializationFinished {
: Bool { 0101 return SSL_state(ssl) == SSL_ST_OK 0102 } 0103 0104 public func handshake
ClientStream.swift:90
                try ssl.handshake()
ServerStream.swift:60
                try ssl.handshake()
() throws { 0105 let result = SSL_do_handshake(ssl) 0106 0107 if result <= 0 { 0108 switch SSL_get_error(ssl, result) { 0109 case SSL_ERROR_WANT_READ: 0110 throw Error.WantRead(description: lastSSLErrorDescription) 0111 case SSL_ERROR_WANT_WRITE: 0112 throw Error.WantWrite(description: lastSSLErrorDescription) 0113 default: 0114 throw Error.Session(description: lastSSLErrorDescription) 0115 } 0116 } 0117 } 0118 0119 public func write(data: Data) { 0120 data.withUnsafeBufferPointer { 0121 SSL_write(ssl, $0.baseAddress, Int32($0.count)) 0122 } 0123 } 0124 0125 public func read() throws -> Data { 0126 var data = Data.bufferWithSize(DEFAULT_BUFFER_SIZE) 0127 0128 let result = data.withUnsafeMutableBufferPointer { 0129 SSL_read(ssl, $0.baseAddress, Int32($0.count)) 0130 } 0131 0132 if result <= 0 { 0133 let error = SSL_get_error(ssl, result) 0134 switch error { 0135 case SSL_ERROR_WANT_READ: 0136 throw Error.WantRead(description: lastSSLErrorDescription) 0137 case SSL_ERROR_WANT_WRITE: 0138 throw Error.WantWrite(description: lastSSLErrorDescription) 0139 case SSL_ERROR_ZERO_RETURN: 0140 throw Error.ZeroReturn(description: lastSSLErrorDescription) 0141 default: 0142 throw Error.Session(description: lastSSLErrorDescription) 0143 } 0144 } 0145 0146 return data.prefix(Int(result)) 0147 } 0148 0149 public func shutdown
Session.swift:61
		shutdown()
() { 0150 SSL_shutdown(ssl) 0151 } 0152 } 0153