# Path: src/net/tls/common.py
# class TLSError(Exception):
#     """
#     Base class for all TLS/SSL exceptions.
# 
#     Exception args is a message string.
#     """
#     pass
# 
# class TLSProtocolError(TLSError):
#     """
#     Raised when a protocol-related problem occurs, such as the remote end does
#     not speak TLS, or when no shared cipher could be established.
#     """
#     pass
# 
# class TLSVerificationError(TLSError):
#     """
#     Raised when the remote end's certificate did not verify correctly.
#     """
#     pass
# 
# class TLSSocketBase(kaa.Socket):
#     # list of suuported TLS authentication mechanisms (subclass must override)
#     supported_methods = []
#     
#     # Cached system-wide CA cert file (as detected), or None if none was found.
#     _cafile = False
# 
#     __kaasignals__ = {
#         'tls':
#             '''
#             Emitted when a TLS handshake has been successfully completed.
#             '''
#     }
# 
#     def __init__(self, cafile=None):
#         super(TLSSocketBase, self).__init__()
#         self._handshake = False
#         self._pre_handshake_write_queue = []
# 
#         if cafile:
#             self._cafile = cafile
#         elif TLSSocketBase._cafile is False:
#             self._cafile = None
#             for path in CA_SEARCH_PATH:
#                 if os.path.exists(path):
#                     cafile = path
#                     break
#             else:
#                 # Maybe locate(1) can help.
#                 # XXX: assumes this is fast.  Maybe this should be done async.
#                 path = os.popen('locate -l 1 ca-certificates.crt ca-bundle.crt 2>/dev/null').readline().strip()
#                 if os.path.exists(path):
#                     cafile = path
# 
#             TLSSocketBase._cafile = self._cafile = cafile


import logging
import os
    import tlslite.api as tlsapi
    import gdata.tlslite.api as tlsapi
import kaa
    from tlslite.errors import TLSAuthenticationError
    from gdata.tlslite.errors import TLSAuthenticationError
from .common import TLSError, TLSProtocolError, TLSVerificationError, TLSSocketBase
            yield self._prepare_tls()
            yield getattr(self, '_tls_' + mode)(*args, **kwargs)
            self.signals['tls'].emit()
        finally:
            self._update_read_monitor()
            self._handshake = False

    def starttls_client(self, *args, **kwargs):
        return self._starttls('client', *args, **kwargs)
        
    def starttls_server(self, *args, **kwargs):
        return self._starttls('server', *args, **kwargs)



    @kaa.coroutine()
    def _tls_client(self, session=None, key=None, srp=None, checker=None):
        """
        Start a certificate-based handshake in the role of a TLS client.
        Note: this function DOES NOT check the server key based on the
        key chain. Provide a checker callback to be called for verification.
        http://trevp.net/tlslite/docs/public/tlslite.Checker.Checker-class.html
        Every callable object can be used as checker.

        @param session: tlslite.Session object to resume
        @param key: TLSKey object for client authentication
        @param srp: username, password pair for SRP authentication
        @param checker: callback to check the credentials from the server
        """
        if not self._rmon:

# Path: src/net/tls/common.py
# class TLSError(Exception):
#     """
#     Base class for all TLS/SSL exceptions.
# 
#     Exception args is a message string.
#     """
#     pass
# 
# class TLSProtocolError(TLSError):
#     """
#     Raised when a protocol-related problem occurs, such as the remote end does
#     not speak TLS, or when no shared cipher could be established.
#     """
#     pass
# 
# class TLSVerificationError(TLSError):
#     """
#     Raised when the remote end's certificate did not verify correctly.
#     """
#     pass
# 
# class TLSSocketBase(kaa.Socket):
#     # list of suuported TLS authentication mechanisms (subclass must override)
#     supported_methods = []
#     
#     # Cached system-wide CA cert file (as detected), or None if none was found.
#     _cafile = False
# 
#     __kaasignals__ = {
#         'tls':
#             '''
#             Emitted when a TLS handshake has been successfully completed.
#             '''
#     }
# 
#     def __init__(self, cafile=None):
#         super(TLSSocketBase, self).__init__()
#         self._handshake = False
#         self._pre_handshake_write_queue = []
# 
#         if cafile:
#             self._cafile = cafile
#         elif TLSSocketBase._cafile is False:
#             self._cafile = None
#             for path in CA_SEARCH_PATH:
#                 if os.path.exists(path):
#                     cafile = path
#                     break
#             else:
#                 # Maybe locate(1) can help.
#                 # XXX: assumes this is fast.  Maybe this should be done async.
#                 path = os.popen('locate -l 1 ca-certificates.crt ca-bundle.crt 2>/dev/null').readline().strip()
#                 if os.path.exists(path):
#                     cafile = path
# 
#             TLSSocketBase._cafile = self._cafile = cafile


# Path: src/net/tls/tlslite.py
import logging
import os
    import tlslite.api as tlsapi
    import gdata.tlslite.api as tlsapi
import kaa
    from tlslite.errors import TLSAuthenticationError
    from gdata.tlslite.errors import TLSAuthenticationError
from .common import TLSError, TLSProtocolError, TLSVerificationError, TLSSocketBase

            yield self._prepare_tls()
            yield getattr(self, '_tls_' + mode)(*args, **kwargs)
            self.signals['tls'].emit()
        finally:
            self._update_read_monitor()
            self._handshake = False

    def starttls_client(self, *args, **kwargs):
        return self._starttls('client', *args, **kwargs)
        
    def starttls_server(self, *args, **kwargs):
        return self._starttls('server', *args, **kwargs)



    @kaa.coroutine()
    def _tls_client(self, session=None, key=None, srp=None, checker=None):
        """
        Start a certificate-based handshake in the role of a TLS client.
        Note: this function DOES NOT check the server key based on the
        key chain. Provide a checker callback to be called for verification.
        http://trevp.net/tlslite/docs/public/tlslite.Checker.Checker-class.html
        Every callable object can be used as checker.

        @param session: tlslite.Session object to resume
        @param key: TLSKey object for client authentication
        @param srp: username, password pair for SRP authentication
        @param checker: callback to check the credentials from the server
        """
        if not self._rmon:

            raise TLSError('Socket not connected')