# Path: src/net/tls/common.py
# 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 sys
import os
import socket
import logging
import kaa
import gnutls.connection
from .common import TLSSocketBase
from gnutls.connection import X509Certificate, X509PrivateKey, X509Certificate, X509CRL, X509Credentials
# 
# gnutls_srp_allocate_client_credentials = c_gnutls.gnutls_srp_allocate_client_credentials
# gnutls_srp_allocate_client_credentials.restype = ctypes.c_int
# gnutls_srp_allocate_client_credentials.argtypes = [ctypes.POINTER(gnutls_srp_credentials_t)]
# gnutls_srp_set_client_credentials = c_gnutls.gnutls_srp_set_client_credentials
# gnutls_srp_set_client_credentials.restype = ctypes.c_int
# gnutls_srp_set_client_credentials.argtypes = [gnutls_srp_credentials_t]
# gnutls_srp_free_client_credentials = c_gnutls.gnutls_srp_free_client_credentials
# gnutls_srp_free_client_credentials.restype = None
# gnutls_srp_free_client_credentials.argtypes = [gnutls_srp_credentials_t]
# 
# class SRPCredentials(object):
# 
#     def __new__(cls, *args, **kwargs):
#         c_object = gnutls_srp_credentials_t()
#         gnutls_srp_allocate_client_credentials(ctypes.byref(c_object))
#         instance = object.__new__(cls)
#         instance._c_object = c_object
#         return instance
# 
#     def __init__(self, username, password):
#         self._type = ctypes.c_int(3)
#         self.username = username
#         self.password = password
#         gnutls_srp_set_client_credentials(self._c_object, username, password);
#     
#     def __del__(self):
#         gnutls_srp_free_client_credentials(self._c_object)



# Path: src/net/tls/common.py
# 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/gnutls.py
import sys
import os
import socket
import logging
import kaa
import gnutls.connection
from .common import TLSSocketBase
from gnutls.connection import X509Certificate, X509PrivateKey, X509Certificate, X509CRL, X509Credentials

# 
# gnutls_srp_allocate_client_credentials = c_gnutls.gnutls_srp_allocate_client_credentials
# gnutls_srp_allocate_client_credentials.restype = ctypes.c_int
# gnutls_srp_allocate_client_credentials.argtypes = [ctypes.POINTER(gnutls_srp_credentials_t)]
# gnutls_srp_set_client_credentials = c_gnutls.gnutls_srp_set_client_credentials
# gnutls_srp_set_client_credentials.restype = ctypes.c_int
# gnutls_srp_set_client_credentials.argtypes = [gnutls_srp_credentials_t]
# gnutls_srp_free_client_credentials = c_gnutls.gnutls_srp_free_client_credentials
# gnutls_srp_free_client_credentials.restype = None
# gnutls_srp_free_client_credentials.argtypes = [gnutls_srp_credentials_t]
# 
# class SRPCredentials(object):
# 
#     def __new__(cls, *args, **kwargs):
#         c_object = gnutls_srp_credentials_t()
#         gnutls_srp_allocate_client_credentials(ctypes.byref(c_object))
#         instance = object.__new__(cls)
#         instance._c_object = c_object
#         return instance
# 
#     def __init__(self, username, password):
#         self._type = ctypes.c_int(3)
#         self.username = username
#         self.password = password
#         gnutls_srp_set_client_credentials(self._c_object, username, password);
#     
#     def __del__(self):
#         gnutls_srp_free_client_credentials(self._c_object)



class GNUTLSSocket(TLSSocketBase):