Package tlslite :: Module tlsconnection :: Class TLSConnection |
[hide private]
[frames] | no frames]

# Class TLSConnection

source code

                       object --+
                                |
    tlsrecordlayer.TLSRecordLayer --+
                                    |
                                   TLSConnection

This class wraps a socket and provides TLS handshaking and data transfer.

To use this class, create a new instance, passing a connected socket into the constructor. Then call some handshake function. If the handshake completes without raising an exception, then a TLS connection has been negotiated. You can transfer data over this connection as if it were a socket.

This class provides both synchronous and asynchronous versions of its key functions. The synchronous versions should be used when writing single-or multi-threaded code using blocking sockets. The asynchronous versions should be used when performing asynchronous, event-based I/O with non-blocking sockets.

Asynchronous I/O is a complicated subject; typically, you should not use the asynchronous functions directly, but should use some framework like asyncore or Twisted which TLS Lite integrates with (see tlslite.integration.tlsasyncdispatchermixin.TLSAsyncDispatcherMixIn).

Instance Methods | [hide private]
__init__(self, sock) Create a new TLSConnection instance. | source code
_calcFinished(self, masterSecret, send=True) | source code
_clientAnonKeyExchange(self, settings, cipherSuite, clientRandom, serverRandom) | source code
_clientFinished(self, premasterSecret, clientRandom, serverRandom, cipherSuite, cipherImplementations, nextProto) | source code
_clientGetKeyFromChain(self, certificate, settings, tackExt=None) | source code
_clientGetServerHello(self, settings, clientHello) | source code
_clientRSAKeyExchange(self, settings, cipherSuite, clientCertChain, privateKey, certificateType, clientRandom, serverRandom, tackExt) | source code
_clientResume(self, session, serverHello, clientRandom, cipherImplementations, nextProto) | source code
_clientSRPKeyExchange(self, settings, cipherSuite, certificateType, srpUsername, password, clientRandom, serverRandom, tackExt) | source code
_clientSelectNextProto(self, nextProtos, serverHello) | source code
_clientSendClientHello(self, settings, session, srpUsername, srpParams, certParams, anonParams, serverName, nextProtos, reqTack) | source code
_getFinished(self, masterSecret, expect_next_protocol=False, nextProto=None) | source code
_handshakeClientAsync(self, srpParams=`(``)`, certParams=`(``)`, anonParams=`(``)`, session=None, settings=None, checker=None, nextProtos=None, serverName=`'````'`, reqTack=True) | source code
_handshakeClientAsyncHelper(self, srpParams, certParams, anonParams, session, settings, serverName, nextProtos, reqTack) | source code
_handshakeServerAsyncHelper(self, verifierDB, certChain, privateKey, reqCert, sessionCache, settings, reqCAs, tacks, activationFlags, nextProtos, anon) | source code
_handshakeWrapperAsync(self, handshaker, checker) | source code
_sendFinished(self, masterSecret, nextProto=None) | source code
_serverAnonKeyExchange(self, clientHello, serverHello, cipherSuite, settings) | source code
_serverCertKeyExchange(self, clientHello, serverHello, serverCertChain, privateKey, reqCert, reqCAs, cipherSuite, settings) | source code
_serverFinished(self, premasterSecret, clientRandom, serverRandom, cipherSuite, cipherImplementations, nextProtos) | source code
_serverGetClientHello(self, settings, certChain, verifierDB, sessionCache, anon) | source code
_serverSRPKeyExchange(self, clientHello, serverHello, verifierDB, cipherSuite, privateKey, serverCertChain) | source code
None or an iterable |
handshakeClientAnonymous(self, session=None, settings=None, checker=None, serverName=`'````'`, async=False) Perform an anonymous handshake in the role of client. | source code
None or an iterable |
handshakeClientCert(self, certChain=None, privateKey=None, session=None, settings=None, checker=None, nextProtos=None, reqTack=True, serverName=`'````'`, async=False) Perform a certificate-based handshake in the role of client. | source code
None or an iterable |
handshakeClientSRP(self, username, password, session=None, settings=None, checker=None, reqTack=True, serverName=`'````'`, async=False) Perform an SRP handshake in the role of client. | source code
handshakeServer(self, verifierDB=None, certChain=None, privateKey=None, reqCert=False, sessionCache=None, settings=None, checker=None, reqCAs=None, tacks=None, activationFlags=0, nextProtos=None, anon=False) Perform a handshake in the role of server. | source code
iterable |
handshakeServerAsync(self, verifierDB=None, certChain=None, privateKey=None, reqCert=False, sessionCache=None, settings=None, checker=None, reqCAs=None, tacks=None, activationFlags=0, nextProtos=None, anon=False) Start a server handshake operation on the TLS connection. | source code
Inherited from `tlsrecordlayer.TLSRecordLayer`: `clearReadBuffer`, `clearWriteBuffer`, `close`, `closeAsync`, `fileno`, `getCipherImplementation`, `getCipherName`, `getVersionName`, `getpeername`, `getsockname`, `gettimeout`, `makefile`, `read`, `readAsync`, `recv`, `recv_into`, `send`, `sendall`, `setsockopt`, `settimeout`, `shutdown`, `unread`, `write`, `writeAsync` Inherited from `tlsrecordlayer.TLSRecordLayer` (private): `_calcPendingStates`, `_calcSSLHandshakeHash`, `_changeReadState`, `_changeWriteState`, `_decrefAsync`, `_decref_socketios`, `_decryptRecord`, `_getMsg`, `_getNextRecord`, `_handshakeDone`, `_handshakeStart`, `_sendError`, `_sendMsg`, `_sendMsgs`, `_shutdown` Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Instance Variables | [hide private]
Inherited from `tlsrecordlayer.TLSRecordLayer`: `allegedSrpUsername`, `closeSocket`, `closed`, `ignoreAbruptClose`, `resumed`, `session`, `sock`, `version`

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, sock) (Constructor) | source code
Create a new TLSConnection instance. Parameters: `sock` (`socket.socket`) - The socket data will be transmitted on. The socket should already be connected. It may be in blocking or non-blocking mode. Overrides: object.__init__

### handshakeClientAnonymous(self, session=None, settings=None, checker=None, serverName=`'````'`, async=False) | source code
Perform an anonymous handshake in the role of client. This function performs an SSL or TLS handshake using an anonymous Diffie Hellman ciphersuite. Like any handshake function, this can be called on a closed TLS connection, or on a TLS connection that is already open. If called on an open connection it performs a re-handshake. If the function completes without raising an exception, the TLS connection will be open and available for data transfer. If an exception is raised, the connection will have been automatically closed (if it was ever open). Parameters: `session` (tlslite.Session.Session) - A TLS session to attempt to resume. If the resumption does not succeed, a full handshake will be performed. `settings` (tlslite.HandshakeSettings.HandshakeSettings) - Various settings which can be used to control the ciphersuites, certificate types, and SSL/TLS versions offered by the client. `checker` (tlslite.Checker.Checker) - A Checker instance. This instance will be invoked to examine the other party's authentication credentials, if the handshake completes succesfully. `serverName` (string) - The ServerNameIndication TLS Extension. `async` (bool) - If False, this function will block until the handshake is completed. If True, this function will return a generator. Successive invocations of the generator will return 0 if it is waiting to read from the socket, 1 if it is waiting to write to the socket, or will raise StopIteration if the handshake operation is completed. Returns: None or an iterable If 'async' is True, a generator object will be returned. Raises: `socket.error` - If a socket error occurs. `tlslite.errors.TLSAbruptCloseError` - If the socket is closed without a preceding alert. `tlslite.errors.TLSAlert` - If a TLS alert is signalled. `tlslite.errors.TLSAuthenticationError` - If the checker doesn't like the other party's authentication credentials.

### handshakeClientCert(self, certChain=None, privateKey=None, session=None, settings=None, checker=None, nextProtos=None, reqTack=True, serverName=`'````'`, async=False) | source code
Perform a certificate-based handshake in the role of client. This function performs an SSL or TLS handshake. The server will authenticate itself using an X.509 certificate chain. If the handshake succeeds, the server's certificate chain will be stored in the session's serverCertChain attribute. Unless a checker object is passed in, this function does no validation or checking of the server's certificate chain. If the server requests client authentication, the client will send the passed-in certificate chain, and use the passed-in private key to authenticate itself. If no certificate chain and private key were passed in, the client will attempt to proceed without client authentication. The server may or may not allow this. If the function completes without raising an exception, the TLS connection will be open and available for data transfer. If an exception is raised, the connection will have been automatically closed (if it was ever open). Parameters: `certChain` (tlslite.x509certchain.X509CertChain) - The certificate chain to be used if the server requests client authentication. `privateKey` (tlslite.utils.rsakey.RSAKey) - The private key to be used if the server requests client authentication. `session` (tlslite.session.Session) - A TLS session to attempt to resume. If the resumption does not succeed, a full handshake will be performed. `settings` (tlslite.handshakesettings.HandshakeSettings) - Various settings which can be used to control the ciphersuites, certificate types, and SSL/TLS versions offered by the client. `checker` (tlslite.checker.Checker) - A Checker instance. This instance will be invoked to examine the other party's authentication credentials, if the handshake completes succesfully. `nextProtos` (list of strings.) - A list of upper layer protocols ordered by preference, to use in the Next-Protocol Negotiation Extension. `reqTack` (bool) - Whether or not to send a "tack" TLS Extension, requesting the server return a TackExtension if it has one. `serverName` (string) - The ServerNameIndication TLS Extension. `async` (bool) - If False, this function will block until the handshake is completed. If True, this function will return a generator. Successive invocations of the generator will return 0 if it is waiting to read from the socket, 1 if it is waiting to write to the socket, or will raise StopIteration if the handshake operation is completed. Returns: None or an iterable If 'async' is True, a generator object will be returned. Raises: `socket.error` - If a socket error occurs. `tlslite.errors.TLSAbruptCloseError` - If the socket is closed without a preceding alert. `tlslite.errors.TLSAlert` - If a TLS alert is signalled. `tlslite.errors.TLSAuthenticationError` - If the checker doesn't like the other party's authentication credentials.

### handshakeClientSRP(self, username, password, session=None, settings=None, checker=None, reqTack=True, serverName=`'````'`, async=False) | source code
Perform an SRP handshake in the role of client. This function performs a TLS/SRP handshake. SRP mutually authenticates both parties to each other using only a username and password. This function may also perform a combined SRP and server-certificate handshake, if the server chooses to authenticate itself with a certificate chain in addition to doing SRP. If the function completes without raising an exception, the TLS connection will be open and available for data transfer. If an exception is raised, the connection will have been automatically closed (if it was ever open). Parameters: `username` (str) - The SRP username. `password` (str) - The SRP password. `session` (tlslite.session.Session) - A TLS session to attempt to resume. This session must be an SRP session performed with the same username and password as were passed in. If the resumption does not succeed, a full SRP handshake will be performed. `settings` (tlslite.handshakesettings.HandshakeSettings) - Various settings which can be used to control the ciphersuites, certificate types, and SSL/TLS versions offered by the client. `checker` (tlslite.checker.Checker) - A Checker instance. This instance will be invoked to examine the other party's authentication credentials, if the handshake completes succesfully. `reqTack` (bool) - Whether or not to send a "tack" TLS Extension, requesting the server return a TackExtension if it has one. `serverName` (string) - The ServerNameIndication TLS Extension. `async` (bool) - If False, this function will block until the handshake is completed. If True, this function will return a generator. Successive invocations of the generator will return 0 if it is waiting to read from the socket, 1 if it is waiting to write to the socket, or will raise StopIteration if the handshake operation is completed. Returns: None or an iterable If 'async' is True, a generator object will be returned. Raises: `socket.error` - If a socket error occurs. `tlslite.errors.TLSAbruptCloseError` - If the socket is closed without a preceding alert. `tlslite.errors.TLSAlert` - If a TLS alert is signalled. `tlslite.errors.TLSAuthenticationError` - If the checker doesn't like the other party's authentication credentials.

### handshakeServer(self, verifierDB=None, certChain=None, privateKey=None, reqCert=False, sessionCache=None, settings=None, checker=None, reqCAs=None, tacks=None, activationFlags=0, nextProtos=None, anon=False) | source code
Perform a handshake in the role of server. This function performs an SSL or TLS handshake. Depending on the arguments and the behavior of the client, this function can perform an SRP, or certificate-based handshake. It can also perform a combined SRP and server-certificate handshake. Like any handshake function, this can be called on a closed TLS connection, or on a TLS connection that is already open. If called on an open connection it performs a re-handshake. This function does not send a Hello Request message before performing the handshake, so if re-handshaking is required, the server must signal the client to begin the re-handshake through some other means. If the function completes without raising an exception, the TLS connection will be open and available for data transfer. If an exception is raised, the connection will have been automatically closed (if it was ever open). Parameters: `verifierDB` (tlslite.verifierdb.VerifierDB) - A database of SRP password verifiers associated with usernames. If the client performs an SRP handshake, the session's srpUsername attribute will be set. `certChain` (tlslite.x509certchain.X509CertChain) - The certificate chain to be used if the client requests server certificate authentication. `privateKey` (tlslite.utils.rsakey.RSAKey) - The private key to be used if the client requests server certificate authentication. `reqCert` (bool) - Whether to request client certificate authentication. This only applies if the client chooses server certificate authentication; if the client chooses SRP authentication, this will be ignored. If the client performs a client certificate authentication, the sessions's clientCertChain attribute will be set. `sessionCache` (tlslite.sessioncache.SessionCache) - An in-memory cache of resumable sessions. The client can resume sessions from this cache. Alternatively, if the client performs a full handshake, a new session will be added to the cache. `settings` (tlslite.handshakesettings.HandshakeSettings) - Various settings which can be used to control the ciphersuites and SSL/TLS version chosen by the server. `checker` (tlslite.checker.Checker) - A Checker instance. This instance will be invoked to examine the other party's authentication credentials, if the handshake completes succesfully. `reqCAs` (list of `bytearray` of unsigned bytes) - A collection of DER-encoded DistinguishedNames that will be sent along with a certificate request. This does not affect verification. `nextProtos` (list of strings.) - A list of upper layer protocols to expose to the clients through the Next-Protocol Negotiation Extension, if they support it. Raises: `socket.error` - If a socket error occurs. `tlslite.errors.TLSAbruptCloseError` - If the socket is closed without a preceding alert. `tlslite.errors.TLSAlert` - If a TLS alert is signalled. `tlslite.errors.TLSAuthenticationError` - If the checker doesn't like the other party's authentication credentials.

### handshakeServerAsync(self, verifierDB=None, certChain=None, privateKey=None, reqCert=False, sessionCache=None, settings=None, checker=None, reqCAs=None, tacks=None, activationFlags=0, nextProtos=None, anon=False) | source code
Start a server handshake operation on the TLS connection. This function returns a generator which behaves similarly to handshakeServer(). Successive invocations of the generator will return 0 if it is waiting to read from the socket, 1 if it is waiting to write to the socket, or it will raise StopIteration if the handshake operation is complete. Returns: iterable A generator; see above for details.